C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 03:29 PM   #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!
PersianStyle is offline   Reply With Quote
Old 07-08-2009, 03:33 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-08-2009, 03:38 PM   #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?
PersianStyle is offline   Reply With Quote
Old 07-08-2009, 03:42 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Comparison is "==".
tabstop is offline   Reply With Quote
Old 07-08-2009, 03:43 PM   #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!
PersianStyle is offline   Reply With Quote
Old 07-08-2009, 03:44 PM   #6
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
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,

Quote:
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.
ಠ_ಠ is offline   Reply With Quote
Old 07-08-2009, 07:13 PM   #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.
PersianStyle is offline   Reply With Quote
Old 07-08-2009, 07:15 PM   #8
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
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"
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-09-2009, 12:05 AM   #9
msh
Novice
 
Join Date: Jul 2009
Posts: 32
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
}
msh is offline   Reply With Quote
Old 07-09-2009, 12:57 AM   #10
msh
Novice
 
Join Date: Jul 2009
Posts: 32
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.
msh is offline   Reply With Quote
Old 07-09-2009, 01:14 AM   #11
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-09-2009, 01:40 AM   #12
msh
Novice
 
Join Date: Jul 2009
Posts: 32
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!
msh is offline   Reply With Quote
Old 07-09-2009, 01:43 AM   #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


}
Aliaks is offline   Reply With Quote
Old 07-09-2009, 02:55 AM   #14
Registered User
 
Join Date: Apr 2006
Location: United States
Posts: 3,201
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.
__________________
Os iusti meditabitur sapientiam
Et lingua eius loquetur indicium

"There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii)

http://www.myspace.com/whiteflags99
whiteflags is offline   Reply With Quote
Old 07-09-2009, 03:18 AM   #15
Registered User
 
Join Date: Jun 2009
Location: Adeliade, AU
Posts: 128
Yeah most probably, I was just using his conditions.
Aliaks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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