![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 20
| Simple program I made - Question about code 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();
}
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 | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Can you tell the difference between this: Code: HottestGirl=1 Code: HottestGirl=1 Code: int HottestGirl=1 Code: while (HottestGirl=1) |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 20
| So when I put: Code: while(HottestGirl=1) How would I make then so it is like if the variable HottestGirl equals 1, then go through the loop? |
| PersianStyle is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Comparison is "==". |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: Jul 2009
Posts: 20
| |
| PersianStyle is offline | |
| | #6 | ||
| Banned Join Date: Mar 2009
Posts: 533
| Quote:
also, Quote:
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 | |
| | #7 | |
| Registered User Join Date: Jul 2009
Posts: 20
| Quote:
I have: Code: while(HottestGirl==1||2||3||4) | |
| PersianStyle is offline | |
| | #8 |
| Banned Join Date: Mar 2009
Posts: 533
| 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 | |
| | #9 | |
| Novice Join Date: Jul 2009
Posts: 32
| Quote:
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 | |
| | #10 | |
| Novice Join Date: Jul 2009
Posts: 32
| Quote:
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;
}
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 | |
| | #11 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,211
| Quote:
__________________ 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 | |
| | #12 | |
| Novice Join Date: Jul 2009
Posts: 32
| Quote:
Code: cin.ignore(numeric_limits<streamsize>::max(), '\n'); ![]() Elegant! Thanks, laserlight! | |
| msh is offline | |
| | #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 | |
| | #14 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,184
| 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 | |
| | #15 |
| Registered User Join Date: Jun 2009 Location: Adeliade, AU
Posts: 128
| Yeah most probably, I was just using his conditions. |
| Aliaks is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |