Thread: Easy problem with while!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    20

    Easy problem with while!

    Hi,

    I have some problems with syntax when i have this inside my code:

    Code:
    	string val;
    	
    	while(val != "1" && != "2"){
    ....
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, so what in particular do you not want to equal "2"?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    20
    i get these syntax errors

    C:\Program Files\Microsoft Visual Studio\MyProjects\44\44.cpp(16) : error C2059: syntax error : '!='

    C:\Program Files\Microsoft Visual Studio\MyProjects\44\44.cpp(16) : error C2143: syntax error : missing ';' before '{'

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We know.

    You need to say what, in particular, you want to not equal 2. Right now you are saying that you don't want __________ to equal 2, but that doesn't help; you need to fill in the blank.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    20
    Here is the code so far for an easy phone book. While the string variable is not equal to either 1 or 2 i would like the program to loop the first phase of the options 1 or 2.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    struct telefon{
    
    	string namn;
    	string nummer;
    };
    
    int main(){
    	
    	string val;
    	
    	while(val != "1" && != "2"){
    	
    		cout << "Enter 1 or 2 for either option..." << endl << endl;
    
    		cout << "1." << '\t' << "login to Adams phonebook" << endl;
    		cout << "2." << '\t' << "exit program" << endl << endl;
    		cout << "Enter choice: ";
    		getline(cin, val);
    
    		if (val == "1"){
    		
    			string pass;
    	
    			cout << "Welcome to Adams phonebook, please insert password to continue..." << endl << endl;
    			cout << "password: ";
    			getline(cin, pass);
    	
    			if(pass == "bajskorv"){}
    			
    			else{
    			cout << endl << endl << "worng password please try again...";
    			}
    
    		}
    	}
    
    	if (val == "2"){
    	exit(1);
    	}
    
    
    	
    	
    	
    return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So the line
    Code:
    while(val != "1" && != "2")
    means "while val doesn't equal 1 and ____________ doesn't equal 2". You need to fill in the blank with the variable you want to not equal 2.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    20
    thank you!!!! Silly me by doing such a miss !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. Relatively easy math problem...
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-18-2005, 08:48 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Math Problem....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 04:37 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM