Like Tree1Likes

Help! Looping problem.

This is a discussion on Help! Looping problem. within the C++ Programming forums, part of the General Programming Boards category; Why can't I stop the loop even after I put while ("answer" != 0); on the answer input I press ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    Help! Looping problem.

    Why can't I stop the loop even after I put

    while ("answer" != 0);

    on the answer input I press 0, but the loop still continue.

    Please help.

    Code:
    //User input poll of "Do you like _______ ?"
    
    
    //3 possible poll responds; r1, r2, r3.
    
    
    #include <iostream>;
    #include <string>;
    
    
    using namespace std;
    
    
    int main()
    {   //question
        string poll;
        //answers
        string answer;
        string y;
        string n;
        
        int count1;//yes
        int count2;//no
        int count3;//no idea
    
    
    
    
    
    
        cout << "NOTE: There are only 3 possibles answers to this poll:\n";
        cout << "1.yes.\n";
        cout << "2.no. \n";
        cout << "3.no idea \n";
    
    
        cout << "Press the answer's number and then ENTER to add poll.\n";
        cout << "Example: press 1 to answer yes.\n";
    
    
        cout << "To end poll, press 0.\n";
    
    
        cout << "What would you like to ask? (Type below) \n\n";
        getline(cin, poll);
        
        do
        {
            
                cout << "Vote for yes? y/n \n";
                cin >> answer;
    
    
                  if ( answer != "y" || answer != "n")
                  { cout << "Invalid choice.\n"; }
                
            
    
    
        }
    
    
        while ( "answer" != 0);
    
    
        system("pause");
        return 0;
    }
    Last edited by Meerul264; 12-13-2012 at 12:50 AM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    ohh nevermind... I should make it (answer != "0") instead of ("answer" != 0)..

    but Im new to this.. is the one in the quotation mark indicate that it is an input to be compared?
    Lesshardtofind likes this.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    try
    Code:
    while( answer != "0")
    While you are at it look at

    Code:
    if ( "answer" != y || "answer" != n)
    And finally think about how they apply to each other.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    yeah I just realised that too... thanks.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    I also asked to make a poll. Can you please give me an idea?

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    You could start by getting the input for the poll and then compare the input to a constant and finally increase the correct variable.
    Further you could add an error check or a while loop to ensure you get an answer that you want.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Haven't made the poll yet. But Is this the correct way to obtain input for the polls?

    Code:
    //User input poll of "Do you like _______ ?"
    
    
    //3 possible poll responds; r1, r2, r3.
    
    
    #include <iostream>;
    #include <string>;
    
    
    using namespace std;
    
    
    int main()
    {   //question
    	string poll;
    	//answers
    	string answer;
    	string answer2;
    	string answer3;
    	string y;
    	string n;
    	
    	
    	int count1;//yes
    	int count2;//no
    	int count3;//no idea
    
    
    
    
    
    
        cout << "NOTE: There are only 3 possibles answers to this poll:\n";
    	cout << "1.yes.\n";
    	cout << "2.no. \n";
    	cout << "3.no idea \n";
    
    
    	cout << "Press the answer's number and then ENTER to add poll.\n";
    	cout << "Example: press 1 to answer yes.\n";
    
    
    	cout << "To end poll, press 0.\n";
    
    
    	cout << "What would you like to ask? (Type below) \n\n";
    	getline(cin, poll);
    	
    	do
    	{
    		
    		    cout << "Vote for yes? y/n \n";
    			cin >> answer;
    
    
    			cout << "Vote for no? y/n \n";
    			cin >> answer2;
    
    
    			cout << "Voute for no idea? y/n \n";
    			cin >> answer3;
    			
    
    
    			  
    			
    			
    			  
                 if ( answer != "y" && answer != "n" || answer2 != "y" && answer2 != "n" || answer3 != "y" && answer3 != "n")
    			  { cout << "Invalid choice.\n"; }
    
    
    
    
    
    
    
    
    			 
    
    
    			
    			
    		
    
    
    	}
    
    
    	while ( answer != "0");
    	 
    
    
    
    
    
    
    
    
    
    
    	  
    
    
    
    
    	 
    
    
    	system("pause");
    	return 0;
    }

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    You are going to want to add () around the compound comparisons.
    Code:
     if ((answer != "y" && answer != "n") || (answer2 != "y" && answer2 != "n") || (answer3 != "y" && answer3 != "n"))
    are you compiling this as you go? Your compiler should be giving you errors for these, or at least warnings. It would be alot more useful for you if you posted your compiler errors with your code and I helped you understand what they mean.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    I'm not really what you mean by "compiling this as I go". But all I know is that I'm using a debugger in Microsoft Visual C++ Express.

    All I ever do since I start learning this a week ago, is learning the language, and then debug (pressing F5), to see the output.

    I don't think I receive any errors, though.

    Anyway, here's what comes out after I debug it:

    'User input poll.exe': Loaded 'C:\Users\user\Documents\Visual Studio 2010\Projects\User input poll\Debug\User input poll.exe', Symbols loaded.
    'User input poll.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    'User input poll.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
    'User input poll.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
    'User input poll.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
    'User input poll.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.

  10. #10
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Yea debugging is equivalent. So if it is running what is the holdup now?

    http://www.kirupa.com/forum/showthre...first-practise

    addresses the Cannot find or Open the PDB file
    Last edited by Lesshardtofind; 12-13-2012 at 02:21 AM.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    You could start by getting the input for the poll
    I've done this part, right?

    and then compare the input to a constant
    I need you to teach me about this please. I'm not sure how.

    and finally increase the correct variable.
    I can use for loops, do while loops and while loops for this, right?
    Correct me if I'm wrong please.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Quote Originally Posted by Meerul264 View Post
    ohh nevermind... I should make it (answer != "0") instead of ("answer" != 0)..

    but Im new to this.. is the one in the quotation mark indicate that it is an input to be compared?
    Anything in string literal ("hello world") is a string (C string to be precise). Thus, in the second one, you compare a string literal to an integer. This doesn't make, but unfortunately works due to the nature of C style strings (I hope we get "real" C++ string literals in the future).
    The first compares the variable answer to the string literal "0", which is what you want.
    That is why it works, and not the first.

    Also, since you seem to be new to loops, you should not ask people "how do I do this?". There are a thousand variants, and you cannot learn them all.
    Ever heard of flowcharts? Try making one for what you want, then translating that into code. Flowcharts help you build your logic and loops.

    As for the output you see in the output window, it's normal and nothing you have to worry about.
    Essentially, a pdb file is a file that contains debugging information. Any executable you make have dependencies on some Windows components, and by default, these files have no debugging information associated with them. That's why you get "cannot find or open pdb file."
    But since you are not going to debug those files, you don't need that information, so you can ignore it. The important thing is that it loads the pdb (debugging info) from your own executable, which it is in this case.
    Last edited by Elysia; 12-13-2012 at 02:57 AM.
    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.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Hmm should I make a flowchart that follows my book? Like one flowchart for each chapter?

    And by "you cannot learn them all", did you meant it by "I cannot learn them all at once", or that "I shouldn't learn them all, because only the important things are needed"?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    There are an infinity number of different combinations of loops. You cannot learn them all.
    You should make a flowchart if you cannot mentally picture how to make a loop required for the task you are doing.
    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 2012
    Location
    Brunei
    Posts
    77
    I'm using flowchart and I think I can depict the code now. Thanks... I'll try to post the code here, I hope you can help later with it if I have any problem.

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

Similar Threads

  1. Looping problem
    By vutek0328 in forum C Programming
    Replies: 8
    Last Post: 09-22-2006, 01:45 AM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. problem with looping
    By bajanstar in forum C Programming
    Replies: 7
    Last Post: 03-09-2005, 12:40 PM
  4. looping problem
    By swagatob in forum C Programming
    Replies: 7
    Last Post: 10-12-2004, 11:50 PM
  5. Looping problem
    By romeoz in forum C++ Programming
    Replies: 8
    Last Post: 09-01-2003, 08:38 PM

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