Results 1 to 9 of 9

Thread: Pepsi machine

  1. #1
    Join Date
    Oct 2004
    Location
    USA
    Posts
    13

    Pepsi machine

    If the player type one character != to ' Y ' the goto is working fine it will keep asking the question. but if the player type 33 for instance the computer will type the questions twice like this :

    Would you like a Pepsi ?
    Please type Y for yes and N to exit
    Would you like a Pepsi ?
    Please type Y for yes and N to exit

    and if he types 6546 it will answer 4 times and so on.

    How do I fix it ?
    thx.

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	char no = 'N' | 'n';
    	char yes = 'Y' | 'y';
    	char answer;
    start:	
    	cout << "Would you like a Pepsi ?" << endl;
    	cout << "Please type Y for yes and N to exit " << endl;
    	cin >> answer;
    
    	if (answer == no)
    		cout << "Have a nice day" << endl;
    
    	else if (answer == yes)
    		cout << "Here is your Pepsi" << endl;
    	else if (answer != yes)
    		goto start;
    				
    }

  2. #2
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    as has been stated countless times before: avoid goto!

    in this case, try replacing it by using a while loop.

  3. #3
    Join Date
    Nov 2005
    Location
    Europe
    Posts
    136
    Quote Originally Posted by ComicSansMS View Post
    in this case, try replacing it by using a while loop.
    Yeah, specifically, a "Do while"-loop seems best in this case (1 to many times).
    Or use a switch-case.
    Last edited by Chriiz; 07-18-2009 at 03:51 PM.

  4. #4
    Join Date
    Oct 2004
    Location
    USA
    Posts
    13

    while loop is typing it all day long

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	char no = 'N' | 'n';
    	char yes = 'Y' | 'y';
    	char answer;
    
    	cout << "Would you like a Pepsi ?" << endl;
    	cout << "Please type Y for yes and N to exit " << endl;
    	cin >> answer;
    
    	if (answer == no)
    		cout << "Have a nice day" << endl;
    
    	else if (answer == yes)
    		cout << "Here is your Pepsi" << endl;
    	while (answer != yes)
    	{
    		cout << "Would you like a Pepsi ?" << endl;
    		cout << "Please type Y for yes and N to exit " << endl;
    	}
    			
    }

  5. #5
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    well, that's obviously bogus. all it does now is output the same two lines of text all over again, without any opportunity for the user to exit or interact with the program.

    think again about what parts of your program you want to be repeated by the loop and, more important, what parts must be repeated in order to keep your program functional.

  6. #6
    Join Date
    Nov 2007
    Posts
    45
    Code:
    	char no = 'N' | 'n';
    	char yes = 'Y' | 'y';
    I don't think this is doing what you think it is. I might be wrong.. but isn't this just bit-wise or-ing between the integer value of N and n. I did the math and for all letters, Capital | Lowercase returns the lowercase. It seems that any capital ascii letter |ed with 0x20 returns its lower case which I had never realized and thought was kinda cool.

    So this might look like its working (since n and y DO work..) but N and Y wont.

    (Unless I'm missing something which is entirely possible.)

    -Nums

  7. #7
    Join Date
    Oct 2004
    Location
    USA
    Posts
    13

    char n and N

    first I am new to C++, so I am trying to learn something by playing around with things and see what happens, why I have called char no = 'N' | 'n';
    because I have tried it and if I called char no = 'N'; which is Capital letter and the user type " n " which is small letter the computer is not recognising it as ' N' unless I am missing something since I am new to c++ and there is another way to set it up for both Capital letter and Small letter .
    Now regarding using the while loop instead of the goto, I allready tried it again and it is typing it none stop. all I want is that if the user did answer anything else besides Y or N so the computer will ask him the question again

    cout << "Would you like a Pepsi ?" << endl;
    cout << "Please type Y for yes and N to exit " << endl;

    if someone can copy and paste the full codes and fix it for me the right way and repost it here for me in the forums I would apreciate.

  8. #8
    Join Date
    Jan 2004
    Posts
    76
    'n' | 'N' certainly doesn't work for that purpose.
    The | ors the two chars bitwise wich will give you 'n' back in this case.
    The ASCII values are
    n 01101110
    N 01001110
    so it doesn't have any effect at all.

    For your while loop problem, just put it the same way as you did with the goto.
    The while loop doesn't solve your initial problem though.
    Put a cin.ignore(INT_MAX, '\n'); after cin << answer;
    if you don't it will keep eating the other characters off till the input stream is empty.

  9. #9
    Join Date
    Oct 2004
    Location
    USA
    Posts
    13
    thank you Da Hui
    cin.ignore(INT_MAX, '\n'); IS what i needed !!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •