Simple program I made - Question about code

This is a discussion on Simple program I made - Question about code within the C++ Programming forums, part of the General Programming Boards category; Hello I was messing around with Microsoft Visual C++ 2008 Express. I am new to programming and am trying to ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    Simple program I made - Question about code

    Hello
    I was messing around with Microsoft Visual C++ 2008 Express.
    I am new to programming and am trying to learn C++
    So here is a simple program I made:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
    	int HottestGirl=1;
    
    	cout << "Who is the hottest girl?\n";
    	cout << "1)Claudia Lynx\n";
    	cout << "2)Megan Fox\n";
    	cout << "3)Audrina Patridge\n";
    	cout << "\n";
    	while(HottestGirl=1)
    	{
    
    		cout << "Selection:";
    		cin >> HottestGirl;
    		switch (HottestGirl) 
    		{
    			case 1:
    				cout << "Claudia Lynx is the hottest girl!\n\n";
    				break;
    			case 2:
    				cout << "Megan Fox is the hottest girl!\n\n";
    				break;
    			case 3:
    				cout << "Audrina Patridge is the hottest girl!\n\n";
    				break;
    			default:
    				cout << "Invalid Selection\n\n";
    				HottestGirl=4;
    				break;
    		}
    	}
    	cin.ignore();
    	cin.get();
    }
    At first, I had the 14th line say: while(HottestGirl=1||2||3||4)
    Therefore, no matter what number the user enters, it will still go in a loop and ask for Selection again.

    But then I changed it to: while(HottestGirl=1)
    Just to see what happens
    The problem is, it still goes in a loop and asks for the selection! Why is this?
    Shouldn't it only loop when the user enters 1 for the variable HottestGirl?


    I also have another minor question if anyone would like to help
    I was going along with the tutorial and it tells me to put cin.ignore(); after having the user input a value and to also have cin.get(); at the end of the program so i can see the results of the program. Could someone explain this a little bit more for me please. I mean I kinda understand how the cin.ignore(); removes the enter and the cin.get(); waits for the user to press enter, but its still kinda bothering me, I don't really get it.
    Also, are there any other ways to be able to see the results of the program without it just running the code then auto closing. I think I saw someone you can do like System Pause or something a long time ago.

    Thanks for any help in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Can you tell the difference between this:
    Code:
    HottestGirl=1
    and this:
    Code:
    HottestGirl=1
    ? Neither can the compiler. So presumably they mean the same thing, right? Assigning one to the variable HottestGirl, regardless of whether it appears here
    Code:
    int HottestGirl=1
    or here
    Code:
    while (HottestGirl=1)
    And yes, you will want to do things like assign to variables inside the (parens) of a while loop -- it's a common idiom for reading everything from a file, for instance.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    So when I put:
    Code:
    while(HottestGirl=1)
    It is assigning 1 to the variable HottestGirl?

    How would I make then so it is like if the variable HottestGirl equals 1, then go through the loop?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Comparison is "==".

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by tabstop View Post
    Comparison is "==".
    ooooooOOOOOOOOOOOOOOOooooOOOooo ahhaha I feel so stupid for forgetting that
    Thanks!

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by PersianStyle View Post
    So when I put:
    Code:
    while(HottestGirl=1)
    It is assigning 1 to the variable HottestGirl?

    How would I make then so it is like if the variable HottestGirl equals 1, then go through the loop?
    = assigns; == compares

    also,

    while(HottestGirl=1||2||3||4)
    because 1 is not zero the statement 1||2||3||4 will be evaluated to a Boolean "true" which will be converted to a non zero integer (usually 1) and assigned to the variable HottestGirl, even if you used == it wouldn't do what you seem to want it to do

    you would need
    Code:
    while(HottestGirl == 1 || HottestGirl == 2 || HottestGirl == 3 || HottestGirl == 4)
    Last edited by ಠ_ಠ; 07-08-2009 at 03:51 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by ಠ_ಠ View Post
    because 1 is not zero the statement 1||2||3||4 will be evaluated to a Boolean "true" which will be converted to a non zero integer (usually 1) and assigned to the variable HottestGirl, even if you used == it wouldn't do what you seem to want it to do

    you would need
    Code:
    while(HottestGirl == 1 || HottestGirl == 2 || HottestGirl == 3 || HottestGirl == 4)
    I don't understand.
    I have:
    Code:
    while(HottestGirl==1||2||3||4)
    And it does what I want it to do, which is keep asking for the selection no matter what the user enters.

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by PersianStyle View Post
    I don't understand.
    I have:
    Code:
    while(HottestGirl==1||2||3||4)
    And it does what I want it to do, which is keep asking for the selection no matter what the user enters.
    then just do while(1), your statement will evaluate to that anyway because 2, 3, and 4 are non zero and therefor "true"
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by PersianStyle View Post
    I don't understand.
    I have:
    Code:
    while(HottestGirl==1||2||3||4)
    And it does what I want it to do, which is keep asking for the selection no matter what the user enters.
    She does what you meant, but not what you said.

    For logic operators, both operands must be of type bool, if they are not - typecasting happens.

    What you said was:
    Code:
    while (HottestGirl == 1 || true || true || true) {
        // will always execute
    }

  10. #10
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by PersianStyle View Post
    I also have another minor question if anyone would like to help
    I was going along with the tutorial and it tells me to put cin.ignore(); after having the user input a value and to also have cin.get(); at the end of the program so i can see the results of the program. Could someone explain this a little bit more for me please. I mean I kinda understand how the cin.ignore(); removes the enter and the cin.get(); waits for the user to press enter, but its still kinda bothering me, I don't really get it.
    operator>> performs formatted input. That is, it interprets the data read in context of its right-hand operand. It skips over and discards whitespaces and stops at newline but leaves it in the input buffer where it [newline] waits to be read. The next read from input buffer with operator>> will discard the newline left in there, but other member functions of cin that perform unformatted input will read it, resulting in unexpected behavior.

    Try:
    Code:
    #include <iostream>
    
    int main(int argc, char** argv)
    {
    	char name[256];
    	int age;
    	
    	std::cout << "Enter your age: ";
    	std::cin >> age;
    	
    	std::cout << "Enter your name: ";
    	std::cin.getline(name, 256); //default delimiter is '\n', so this will result in a null string
    	
    	std::cout << "Your name is " << name << ", your age is "
    			  << age << ".\n";
    	
    	return 0;
    }
    Adding std::cin.ignore() after std::cin >> age; will read and discard the next character from the input buffer, '\n' in this case, getline() will have nothing to read and will wait for input from user.

    I personally prefer std::cin.sync(), but that might have side-effects I'm not aware of - I'm but a newbie myself, and if just said something incredibly stupid I expect someone to come along and smack me around.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,318
    Quote Originally Posted by msh
    I personally prefer std::cin.sync(), but that might have side-effects I'm not aware of
    Refer to Prelude's comments concerning std::istream::sync() in Simple C++ help for a beginer?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by laserlight View Post
    Refer to Prelude's comments concerning std::istream::sync() in Simple C++ help for a beginer?
    Thanks!
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    So what that does is: "from input buffer, extract and discard the number of characters that correspond to the maximum possible value of type streamsize, e.g. the maximum number of characters that can be placed in the buffer, OR extract and discard all character up until and including the first '\n' that is encountered, whichever comes first". Did I get that about right?

    Elegant!

    Thanks, laserlight!

  13. #13
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    May I suggest using a do while loop?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int HottestGirl = 0;
    
    	cout << "Who is the hottest girl?\n";
    	cout << "1)Claudia Lynx\n";
    	cout << "2)Megan Fox\n";
    	cout << "3)Audrina Patridge\n";
    	cout << "\n";
    
    //------------------------------------------------------------------------------------------------------------------------// START   //
        do {                                                                                                                  // Do While -
            cin >> HottestGirl;                                                                                               // Input
            if (HottestGirl == 1) {                                                                                           // Set Case If
                cout << "\nClaudia Lynx is the hottest girl!\n\n";
            }
            else if (HottestGirl == 2) {                                                                                      // Set Case If
                cout << "\nMegan Fox is the hottest girl!\n\n";
            }
            else if (HottestGirl == 3) {                                                                                      // Set Case If
                cout << "\nAudrina Patridge is the hottest girl!\n\n";
            }
            else {                                                                                                            // If none above then do
                cout << "Invalid Selection\n\n";
            }
        } while (HottestGirl == 0);                                                                                           // Do the above while HottestGirl equals 0
    
    
    }

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    That would be fine but HottestGirl isn't going to be zero for very long. Either reset the variable to zero when something goes wrong, or use a better loop condition.

  15. #15
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Yeah most probably, I was just using his conditions.

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

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  2. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 09:04 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. Have You Got A program To Match Question.
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 06-01-2002, 03:50 PM

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