Thread: loop in menu

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    loop in menu

    guys, I need my program to repeat itself again after we do any step.EX Once I run my program the menu will pops up, I will press 1 and I will enter the code than I need the menu to pops up again without running the program again. I don't know how to use the while loop?

    Code:
    #include <iostream>
    #include <cctype>
    #include<string>
    using namespace std;
    
    void newCode();
     void printCode();		
    
    int main()
    {
    	int tester;
    			cout << "select one of these options\n";
     			cout << "(1) Insert a new code\n";
     			cout << "(2) Print the data\n";
     			cout << "(3) Exit the program\n";
    			cin >> tester;
    	
    	
    		if(tester==1)
    			newCode();		
    		else if (tester==2)
    			printCode();
    	/*	else if (tester==3)
    		
    		else
    			cout << "ERROR!!! You must choose one of the options from 1 to 3";*/
       	
      	return 0;
    }  
    
    void newCode()
    {
    	string serial_number;
    	int length;	
    	
       cout<< "please enter a serial number of 12 characters:";
    	cin >> serial_number;
    	length = serial_number.length();
    
    	if (length!=12 )
    		cout << "The code you have entered cannot be processed because the code must 12 characters exactly";
    	else 
    		cout << "The serial number is:/n/" << serial_number;
    }	
    
     
     void printCode()
     {
     	string serial_number;
     	cout<< serial_number;
     }

  2. #2
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Code:
    int tester = 99;
    while(tester != 3)
    {
     
    }
    Just put your menu between the brackets. It will continue to run untill "tester" equals "3", which is your option to exit the program. Make sure you give tester a value before you use it in the while loop (any number will do as long as it isn't 3).

    Edit*
    You could also use a do while loop:
    Code:
    do
    {
    
    }while(tester != 3);
    and you wouldn't have to assign "tester" a value.
    Last edited by Cpro; 01-24-2008 at 12:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Delet Frequency in linked list
    By swishiat.com in forum C Programming
    Replies: 16
    Last Post: 12-13-2003, 02:05 AM