Loop back to the start of a program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • millman
    New Member
    • Oct 2011
    • 5

    Loop back to the start of a program?

    Code:
    #include <StdAfx.h>
    #pragma hdrstop
    #include <conio.h>
    #include <ctype.h>
    #include <iostream>
    
    //----------------------------------------------------------------------
    
    using namespace std;
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main ( ) { 
    int i;
    int x;
    
    std::cout << "\n Please enter an even number:";
    cin >> x;
    if(x%2==1)
            std::cout << "\n Number entered was not even.";
    		//This is where i need the loop back to start
    else
        std::cout << "\n Number entered was " <<x<< ".";  
    	std::cout << "\n\n Next ten even values: ";
    for (i=0; i<=8; i++) 
    {
    	 x=x+2;
         std::cout <<x<< " "; 
    }
    x=x+2;
    std::cout <<x<< ".";
    getch();
    return 0;
    }
    I'll be grateful for any ideas to add to the program. Also, if it helps i'm using Microsoft Visual C++ 2010. The coding i need it to restart the program from the specified point, and if it's possible i'd like to have a y/n option. Thanks :)
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    I deleted my post because I thought you were asking for something else. The answer was irrelevant. Sorry for that.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      before line 20 write:
      Code:
      while(x%2==1){
      
      }

      Comment

      • millman
        New Member
        • Oct 2011
        • 5

        #4
        no problem thanks anyway alexis
        And to solve the problem that when you enter an odd number into the running program it won't show the next ten alternate numbers. But i still need a code to loop back to the start. Anyway, this is the corrected program.
        Code:
        #include <StdAfx.h>
        #pragma hdrstop
        #include <conio.h>
        #include <ctype.h>
        #include <iostream>
        
        //----------------------------------------------------------------------
        
        using namespace std;
        using std::cout;
        using std::endl;
        using std::cin;
        
        int main ( ) { 
        int i;
        int x; 
        
        std::cout << "\n Please enter an even number: ";
        cin >> x;
        if(x%2==1)
                std::cout << "\n Number entered was not even.";
        		//This is where i need the loop back to start
        
        if(x%2==0)   
        		std::cout << "\n Number entered was " <<x<< ".";  
        		
        if(x%2==0)
        		std::cout << "\n\n Next ten even values: ";
        	
        if(x%2==0)	
        	for (i=0; i<=9; i++) 
        	{
        		x=x+2;
        		std::cout <<x<< " "; 
        	}
        getch();
        return 0;
        }

        Comment

        Working...