Problem in the menu

This is a discussion on Problem in the menu within the C++ Programming forums, part of the General Programming Boards category; My program compiles and works perfectly but the the menu doesn't work? I couldn't know what's the problem with it ...

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

    Problem in the menu

    My program compiles and works perfectly but the the menu doesn't work? I couldn't know what's the problem with it ?


    Here is my menu
    Code:
    	int tester;
    	while ( (tester>=1)&&(tester<4) )
    	{  		
    			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){
          		                cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    		   }
    			
    			if (tester==2){
    				cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    				rect.printCode(length);
    			}
    			
    			if (tester==3){
    				break;
    			}
    			else{
    				cout << "ERROR!!! You must choose one of the following options 1 to 4";

    Code:
    
    #include <iostream>
    using namespace std;
    
    // declaring a class
    class CRectangle 
    {
    	//Three methods in use
    	public:
        
        	void set_values(int, string);
    		string check();
    	 	void printCode(int);
    
    	private:
    	
    		int x,flag,flag1,flag2,flag3,counter,counter1,counter2,counter4;
    		string y,mystr;
    		size_t found,found1,found2,found3,found4,found5,found6,found7,found8,found9,found10,found11;	
    		string billy[];		
    		
    };
    
    
    //printing an array
    void CRectangle::printCode (int a)
    {
    	counter4=0;
    	while(counter <= a)
    	{
    		cout << billy[counter]<<endl;
    	}
    }
    
    //set the values 
    void CRectangle::set_values (int a, string b)
    {
      	x = a;
      	y = b;
    }
    
    //check the four conditions
    string CRectangle::check()
    {
       found = y.find('4'); 
    	found1 = y.find('5'); 
    	found2 = y.find('8'); 
    	found3 = y.find('9'); 
    	found4 = y.find('A'); 
    	found5 = y.find('L'); 
    	found6 = y.find('P'); 
    	found7 = y.find('R');
    	found8 = y.find('T'); 
    	found9 = y.find('V'); 
    	found10 = y.find('W'); 
    	found11 = y.find('X');
    	flag =0;
    	flag1=0;
    	flag2=0;
    	flag3=0;
    	counter=0;
    	counter1=4;
    	counter2=8;
    	mystr = y;
    	billy[12];
    	if(x != 12)
    	{
    		cout << "false"<< endl;
    	}
    		
    	else if ((found==string::npos)||(found1==string::npos))
    				cout << "Error: The digits 4 and 5 must appear at least once."<<endl;
    		  else if ((found2==string::npos)&&(found3==string::npos))
    			    	cout << "Error: Either the digit 8 and 9 must appear at least once." << endl;
    				 else  					
    						while(counter<4)
    						{
    							if ( (found4!=string::npos) || (found5!=string::npos) || (found6!=string::npos) || (found7!=string::npos)|| (found8!=string::npos) || (found9!=string::npos) || (found10!=string::npos)||(found1!=string::npos))
    							{
    								flag=1;
    								break;
    							}
    							counter++;	
    	
    						}
    								  			
    						if(flag){
    							while(counter1<8)
    							{
    								if ( (found4!=string::npos) || (found5!=string::npos) || (found6!=string::npos) || (found7!=string::npos)|| (found8!=string::npos) || (found9!=string::npos) || (found10!=string::npos)||(found11!=string::npos))
    								{
    									flag1=1;
    									break;		
    								}							
    							
    								counter1++;
    							}
    							if(flag1){
    								while(counter2<12)
    								{
    									if ( (found4!=string::npos) || (found5!=string::npos) || (found6!=string::npos) || (found7!=string::npos)|| (found8!=string::npos) || (found9!=string::npos) || (found10!=string::npos)||(found11!=string::npos))
    									{
    										flag2=1;
    										break;
    
    									}							
    
    									counter2++;
    								}
    								if(flag2){
    									while(counter<12)
    									{
    										if ( (mystr[counter] == 'Q') || (mystr[counter] == 'E') || (mystr[counter] == 'Y') || (mystr[counter] == 'U') || (mystr[counter] == 'I') || (mystr[counter] == 'O')||(mystr[counter] == 'S')||(mystr[counter] == 'D')||(mystr[counter] == 'F')||(mystr[counter] == 'G')||(mystr[counter] == 'H')||(mystr[counter] == 'J')||(mystr[counter] == 'K')||(mystr[counter] == 'Z')||(mystr[counter] == 'C')||(mystr[counter] == 'B')||(mystr[counter] == 'N')||(mystr[counter] == 'M')||(mystr[counter] == 'q')||(mystr[counter] == 'w')||(mystr[counter] == 'e')||(mystr[counter] == 'r') || (mystr[counter] == 't') || (mystr[counter] == 'y') || (mystr[counter] == 'u') || (mystr[counter] == 'i') || (mystr[counter] == 'o') || (mystr[counter] == 'p') || (mystr[counter] == 'a') || (mystr[counter] == 's') || (mystr[counter] == 'd') || (mystr[counter] == 'f') || (mystr[counter] == 'g') || (mystr[counter] == 'h') || (mystr[counter] == 'j') || (mystr[counter] == 'k') || (mystr[counter] == 'l') || (mystr[counter] == 'z') || (mystr[counter] == 'x') || (mystr[counter] == 'c') || (mystr[counter] == 'v') || (mystr[counter] == 'b') || (mystr[counter] == 'n') || (mystr[counter] == 'm') )
    										{
    												flag3=1;
    												break;
    										}
    										counter++;
    									}
    
    									   
    								}				
    						   }
    						}	  
    			
    if(flag3)
    {
    		cout << "You are mising one or more of the following:\n";
    		cout << "   1. The only valid letters are A,L,P,R,T,V,W, and X.\n";
    		cout << "   2. Each four-character subsquence (1-4, 5-8, 9-12) must contain at least one digit and at least one letter.\n";
    		cout << "  \n"<<endl;
    }
    else
    {
    	cout << "The code is correct:" << y <<endl;
    	while(counter<12)
    	{
    			billy[counter]=mystr[counter];
    			counter++;
    	}
    }
    								
    	return  y;
    }
    
    
    int main () {
      	
    	int length;
    	string codeNumber;
            string mystr = codeNumber;
    	CRectangle rect;
    
    	int tester;
    	while ( (tester>=1)&&(tester<4) )
    	{  		
    			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){
          		cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    		   }
    			
    			if (tester==2){
    				cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    				rect.printCode(length);
    			}
    			
    			if (tester==3){
    				break;
    			}
    			else{
    				cout << "ERROR!!! You must choose one of the following options 1 to 4";
    			}
       }
       	return 0;
    }
    Last edited by aama100; 02-05-2008 at 03:59 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by aama100 View Post
    My program compiles and works perfectly but the the menu doesn't work?...
    apparently your program doesnt work perfectly then!

    what exactly is the problem in that the 'menu doesnt work'? whats supposed to happen? what is happening instead? highlight any relevant lines (ie menu code?) in your code tags so we dont have to search for it.

    remember we are here on our own time trying to help so please help us too! thanks

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	int tester;
    	while ( (tester>=1)&&(tester<4) )
    	{  		
    			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){
          		                cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    		   }
    			
    			if (tester==2){
    				cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    				rect.printCode(length);
    			}
    			
    			if (tester==3){
    				break;
    			}
    			else{
    				cout << "ERROR!!! You must choose one of the following options 1 to 4";
    Your menu is inconsistent, but I don't see anything directly wrong with it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Quote Originally Posted by nadroj View Post
    apparently your program doesnt work perfectly then!

    what exactly is the problem in that the 'menu doesnt work'? whats supposed to happen? what is happening instead? highlight any relevant lines (ie menu code?) in your code tags so we dont have to search for it.

    remember we are here on our own time trying to help so please help us too! thanks
    The problem is: Once I run my program the menu has to appear first and so I select one of the three options to preform a task, but What is appearantly going in my program it is directly goes to option one.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Your menu is inconsistent, but I don't see anything directly wrong with it.

    I tried either way but it seems it doesn't do anything

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    on a side note, i just noticed you _did_ put your menu code separate at the very top, sorry!

    you should use a do...while loop rather than a while loop. with your while loop, and more importantly, having not initialized tester, you dont know what value it has when your program runs. it may have something in the range of 1,2,3, in which case it will execute the while block. however if tester is initialized (randomly) to 54, it wont execute the while block.

    changing your code to a do while loop should fix it, as it will ensure tester is initialized
    Code:
    do
    {
       // all of your normal code here thats in your current while loop
    } while (tester > 0 && tester < 4);
    hope it helps
    Last edited by nadroj; 02-05-2008 at 04:32 PM.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Quote Originally Posted by nadroj View Post
    on a side note, i just noticed you _did_ put your menu code separate at the very top, sorry!

    you should use a do...while loop rather than a while loop. with your while loop, and more importantly, having not initialized tester, you dont know what value it has when your program runs. it may have something in the range of 1,2,3, in which case it will execute the while block. however if tester is initialized (randomly) to 54, it wont execute the while block.

    changing your code to a do while loop should fix it, as it will ensure tester is initialized
    Code:
    do
    {
       // all of your normal code here thats in your current while loop
    } while (tester > 0 && tester < 4);
    hope it helps
    it still not working. I did exactly what you have just said!

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    it still not working
    please! always explain how/what. even if its the same issue say so. now, is it the same issue? when i ran it on my machine, it didnt do 'option one' as you said it does. it simply exits, because tester is not in the range.

    can you please post your updated code?

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Okay! take a look to this program. It's the same like the one I posted earlier and it does the same job. Do not care about the indentation for now just copy the program and compile it.

    Code:
    #include <iostream>
    #include <cctype>
    #include<string>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    
    void newCode();
    void printCode();
    void errorCode();
    
    string billy [12];
    
    int main()
    {
    	int tester;
    		while((tester >= 1) || (tester < 4))
    		{
    			cout << "select one of these options\n";
     			cout << "(1) Insert a new code\n";
     			cout << "(2) Print the data\n";
    			cout << "(3) Program requirments\n";
    			cout << "(4) Exit the program\n";
    
    			cin >> tester;
    			
    			if(tester==1)
    				newCode();		
    			else if (tester==2)
    				printCode();
    		 	else if (tester==3)
    				errorCode();	
    			else if (tester==4)
    				break;
    			else
    				cout << "ERROR!!! You must choose one of the following options 1 to 4";
    		}
    return 0;
    
    }  
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    void newCode()
    {
    	string serial_number;
    	int length;	
      	string mystrArray [12];
       cout<< "please enter a serial number of 12 characters:";
    	cin >> serial_number;
    	length = serial_number.length();
    	string mystr = serial_number;
    	int flag=0;
    		
    	if (length!=12 )
    		cout << "There must be 12 characters exactly\n";
    	else for(int i=0;i<length;i++)
    			{
    				if (mystr[i]=='4')
    				{
    					flag=1;
    					break;
    				}
    			}
    			if(flag)
    			{
    				int flag1 =0;
    				for(int i=0;i<length;i++)
    				{
    					if (mystr[i]=='5')
    					{
    						flag1=1;
    						break;
    					}
    				}
    				if(flag1)
    				{
    					int flag2=0;
    					for(int i=0;i<length;i++)
    					{
    						if (mystr[i]=='8' || mystr[i]=='9')
    						{
    							flag2=1;
    							break;
    						}
    					}
    					if(flag2)
    					{
    						int flag3=0;
    						for(int i = 0; i< 4; i++)
    						{
    							if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    							{
    								flag3=1;
    								break;
    							}
    						}
    						if(flag3)
    						{
    							int flag4=0;
    							for(int i = 4; i< 8; i++)
    							{
    								if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    								{
    									flag4=1;
    									break;
    								}
    							}
    							if(flag4)
    							{
    								int flag5=0;
    								for(int i = 8; i< 12; i++)
    								{
    									if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    									{
    										flag5=1;
    										break;
    									}
    								}
    								if(flag5)
    								{
    									int flag6=0;
    									for(int i = 0; i< 12; i++)
    									{
    										if ((mystr[i] == 'Q') || (mystr[i] == 'E') || (mystr[i] == 'Y') || (mystr[i] == 'U') || (mystr[i] == 'I') || (mystr[i] == 'O')||(mystr[i] == 'S')||(mystr[i] == 'D')||(mystr[i] == 'F')||(mystr[i] == 'G')||(mystr[i] == 'H')||(mystr[i] == 'J')||(mystr[i] == 'K')||(mystr[i] == 'Z')||(mystr[i] == 'C')||(mystr[i] == 'B')||(mystr[i] == 'N')||(mystr[i] == 'M')||(mystr[i] == '~') || (mystr[i] == '@') || (mystr[i] == '#') || (mystr[i] == '$') || (mystr[i] == '&#37;') || (mystr[i] == '^')||(mystr[i] == '&')||(mystr[i] == '*')||(mystr[i] == '(')||(mystr[i] == ')')||(mystr[i] == '_')||(mystr[i] == '`')||(mystr[i] == '-')||(mystr[i] == '+')||(mystr[i] == '=')||(mystr[i] == 'q')||(mystr[i] == 'w')||(mystr[i] == 'e')||(mystr[i] == 'r') || (mystr[i] == 't') || (mystr[i] == 'y') || (mystr[i] == 'u') || (mystr[i] == 'i') || (mystr[i] == 'o')||(mystr[i] == 'p')||(mystr[i] == 'a')||(mystr[i] == 's')||(mystr[i] == 'd')||(mystr[i] == 'f')||(mystr[i] == 'g')||(mystr[i] == 'h')||(mystr[i] == 'j')||(mystr[i] == 'k')||(mystr[i] == 'l')||(mystr[i] == 'z')||(mystr[i] == 'x')||(mystr[i] == 'c')||(mystr[i] == 'v')||(mystr[i] == 'b')||(mystr[i] == 'n')||(mystr[i] == 'm')||(mystr[i] == '[')||(mystr[i] == ']')||(mystr[i] == '{')||(mystr[i] == '}')||(mystr[i] == ';')||(mystr[i] == '<')||(mystr[i] == '>')||(mystr[i] == ',')||(mystr[i] == '.')||(mystr[i] == '?')||(mystr[i] == '/'))
    										{
    											flag6=1;
    										}
    									}
    									if(flag6)
    									{
    										cout << "you have used not valid characters\n"<<endl;
    									}
    									else
    									{
    										cout << "This code is correct\n"<<endl;
    										cout << "Click on 2 to print the code\n"<<endl;
    										for ( int i=0;i<length;i++)
    										{
    											billy[i]=mystr[i];
    										}
    									}
    								}
    								else
    								{
    									cout << "The only valid letters are A,L,P,R,T,V,W, and X from 9 to 12 digits.\n"<<endl;
    								}
    									
    							}
    							else
    							{
    								cout << "The only valid letters are A,L,P,R,T,V,W, and X from 5 to 8 digits.\n"<<endl;
    							}
    						}
    						
    						
    						else 
    						{
    							cout << "The only valid letters are A,L,P,R,T,V,W, and X from 1 to 4 digits.\n"<<endl;
    						}
    					}
    					else
    					{
    						cout << "Either the digit 8 and 9 must appear at least once.\n"<<endl;
    					}
    				}
    				else
    					cout << "The digits 4 and 5 must appear at least once.\n"<<endl;
    			}
    			else
    			{
    				cout << "The digits 4 and 5 must appear at least once.\n"<<endl;
    			}
    }
    							
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    void printCode()
    {
    	for(int i = 0; i< 13; i++)
    	{
    		cout<<billy[i]<<endl;
    	}
    }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    void errorCode()
    {	
    
    //	cout << "You are mising one or more of the following:\n";
    	cout << "   1. The digits 4 and 5 must appear at least once.\n";
    	cout << "   2. Either the digit 8 and 9 must appear at least once.\n";
    	cout << "   3. The only valid letters are A,L,P,R,T,V,W, and X.\n";
    	cout << "   3. Each four-character subsquence (1-4, 5-8, 9-12) must contaon at least one digit and at least one letter.\n";
    	cout << "  \n"<<endl;
    }


    however, this is my new updated program which I did what you have said. Take a look at it
    In this program the only different between the previous one is that I am using the Object Oriented Design. I tested the program what it works .. It is just not showing the menu and I run it.

    Code:
    
    #include <iostream>
    using namespace std;
    
    // declaring a class
    class CRectangle 
    {
    	//Three methods in use
    	public:
        
        	void set_values(int, string);
    		string check();
    	 	void printCode(int);
    
    	private:
    	
    		int x,flag,flag1,flag2,flag3,counter,counter1,counter2,counter4;
    		string y,mystr;
    		size_t found,found1,found2,found3,found4,found5,found6,found7,found8,found9,found10,found11;	
    		string billy[];		
    		
    };
    
    
    //printing an array
    void CRectangle::printCode (int a)
    {
    	counter4=0;
    	while(counter <= a)
    	{
    		cout << billy[counter]<<endl;
    	}
    }
    
    //set the values 
    void CRectangle::set_values (int a, string b)
    {
      	x = a;
      	y = b;
    }
    
    //check the four conditions
    string CRectangle::check()
    {
       found = y.find('4'); 
    	found1 = y.find('5'); 
    	found2 = y.find('8'); 
    	found3 = y.find('9'); 
    	found4 = y.find('A'); 
    	found5 = y.find('L'); 
    	found6 = y.find('P'); 
    	found7 = y.find('R');
    	found8 = y.find('T'); 
    	found9 = y.find('V'); 
    	found10 = y.find('W'); 
    	found11 = y.find('X');
    	flag =0;
    	flag1=0;
    	flag2=0;
    	flag3=0;
    	counter=0;
    	counter1=4;
    	counter2=8;
    	mystr = y;
    	billy[12];
    	if(x != 12)
    	{
    		cout << "false"<< endl;
    	}
    		
    	else if ((found==string::npos)||(found1==string::npos))
    				cout << "Error: The digits 4 and 5 must appear at least once."<<endl;
    		  else if ((found2==string::npos)&&(found3==string::npos))
    			    	cout << "Error: Either the digit 8 and 9 must appear at least once." << endl;
    				 else  					
    						while(counter<4)
    						{
    							if ( (found4!=string::npos) || (found5!=string::npos) || (found6!=string::npos) || (found7!=string::npos)|| (found8!=string::npos) || (found9!=string::npos) || (found10!=string::npos)||(found1!=string::npos))
    							{
    								flag=1;
    								break;
    							}
    							counter++;	
    	
    						}
    								  			
    						if(flag){
    							while(counter1<8)
    							{
    								if ( (found4!=string::npos) || (found5!=string::npos) || (found6!=string::npos) || (found7!=string::npos)|| (found8!=string::npos) || (found9!=string::npos) || (found10!=string::npos)||(found11!=string::npos))
    								{
    									flag1=1;
    									break;		
    								}							
    							
    								counter1++;
    							}
    							if(flag1){
    								while(counter2<12)
    								{
    									if ( (found4!=string::npos) || (found5!=string::npos) || (found6!=string::npos) || (found7!=string::npos)|| (found8!=string::npos) || (found9!=string::npos) || (found10!=string::npos)||(found11!=string::npos))
    									{
    										flag2=1;
    										break;
    
    									}							
    
    									counter2++;
    								}
    								if(flag2){
    									while(counter<12)
    									{
    										if ( (mystr[counter] == 'Q') || (mystr[counter] == 'E') || (mystr[counter] == 'Y') || (mystr[counter] == 'U') || (mystr[counter] == 'I') || (mystr[counter] == 'O')||(mystr[counter] == 'S')||(mystr[counter] == 'D')||(mystr[counter] == 'F')||(mystr[counter] == 'G')||(mystr[counter] == 'H')||(mystr[counter] == 'J')||(mystr[counter] == 'K')||(mystr[counter] == 'Z')||(mystr[counter] == 'C')||(mystr[counter] == 'B')||(mystr[counter] == 'N')||(mystr[counter] == 'M')||(mystr[counter] == 'q')||(mystr[counter] == 'w')||(mystr[counter] == 'e')||(mystr[counter] == 'r') || (mystr[counter] == 't') || (mystr[counter] == 'y') || (mystr[counter] == 'u') || (mystr[counter] == 'i') || (mystr[counter] == 'o') || (mystr[counter] == 'p') || (mystr[counter] == 'a') || (mystr[counter] == 's') || (mystr[counter] == 'd') || (mystr[counter] == 'f') || (mystr[counter] == 'g') || (mystr[counter] == 'h') || (mystr[counter] == 'j') || (mystr[counter] == 'k') || (mystr[counter] == 'l') || (mystr[counter] == 'z') || (mystr[counter] == 'x') || (mystr[counter] == 'c') || (mystr[counter] == 'v') || (mystr[counter] == 'b') || (mystr[counter] == 'n') || (mystr[counter] == 'm') )
    										{
    												flag3=1;
    												break;
    										}
    										counter++;
    									}
    
    									   
    								}				
    						   }
    						}	  
    			
    if(flag3)
    {
    		cout << "You are mising one or more of the following:\n";
    		cout << "   1. The only valid letters are A,L,P,R,T,V,W, and X.\n";
    		cout << "   2. Each four-character subsquence (1-4, 5-8, 9-12) must contain at least one digit and at least one letter.\n";
    		cout << "  \n"<<endl;
    }
    else
    {
    	cout << "The code is correct:" << y <<endl;
    	while(counter<12)
    	{
    			billy[counter]=mystr[counter];
    			counter++;
    	}
    }
    								
    	return  y;
    }
    
    
    int main () {
      	
    	int length;
    	string codeNumber;
       string mystr = codeNumber;
    	CRectangle rect;
    	
    	int tester =0;
    	
    do
    {
    
    			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){
          		cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    		   }
    			
    			if (tester==2){
    		
    				rect.printCode(length);
    			}
    			
    			if (tester==3){
    				break;
    			}
    			else{
    				cout << "ERROR!!! You must choose one of the following options 1 to 4";
    			}
    } while (tester > 0 && tester < 4);
       
       	return 0;
    }

  10. #10
    coder
    Join Date
    Feb 2008
    Posts
    127
    Try to imagine that someone had posted all these codes, and you are trying to read it for the first time.
    How can we help you???

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok, running the last piece of code it works after a tweak. notice this portion of code in your do while loop in main()
    Code:
    if(tester==1){
          		cout << "Insert a code of 12 characters:";
    				cin >> codeNumber;
    				length = codeNumber.length();
    				rect.set_values (length,codeNumber);
    				rect.check();
    		   }
    			
    			if (tester==2){
    		
    				rect.printCode(length);
    			}
    			
    			if (tester==3){
    				break;
    			}
    			else{
    				cout << "ERROR!!! You must choose one of the following options 1 to 4";
    			}
    your if statements must be all together, like 'if...else if... else if... then'. once changing that, things seem to work. if you enter 1, it does option 1. if you enter 3, it exits. if you enter anything else > 3 or < 1 it prints error message and exits. if you enter 2, however, it doesnt appear to work. youll notice, again (!) that you pass an uninitialized variable length.. fix that up and add the else if's and i think it should work.

  12. #12
    coder
    Join Date
    Feb 2008
    Posts
    127
    A "switch"statement might work way better

    Code:
    switch (tester) {
       case 1:
          ...
          break;
       case 2:
          ...
          break;
       case 3:
          ...
          break;
       default:   // not 1, not 2, not 3
          ...
          break;
    }
    http://www.cprogramming.com/tutorial/lesson5.html

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes, i meant to mention that but forgot while typing. carlorfeo has a good point.

    also, im pretty sure that ive had problems mixing string input with non-string input (ie integers, in your case). if you notice a problem, i think the solution is to add cin.ignore() after every cin call.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    You really shouldn't ignore indentation.
    Your code looks messy and difficult to read. Even if copying n' compiling, we must still understand the code to debug it and you're making it very difficult.
    I at least suggest you take a tutorial in indentation: http://cpwiki.sf.net/User:Elysia/Indentation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    You may also want to add a cin.ignore() after each of your calls to std::cin.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Simple Menu Problem
    By DanC in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2006, 12:33 PM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 01:10 AM
  4. Window - Menu problem...
    By FromHolland in forum Windows Programming
    Replies: 1
    Last Post: 02-26-2004, 02:49 PM
  5. MDI and MENU Problem
    By Marc in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2004, 05:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21