| | |
If Else If Help plz
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi, im a new to C++ and i am having an issue with if else if commands. I have the code below, and i am attempting to make it so when prompted "Do you want the correct answers?" you can type yes and it will display the answers, or type no to display "have a nice day" and if neither are typed then it says "you did not enter a valid character" and thats the end of the program.
if you have any help please do leave a message, keep in mind im new to C++
Thanks
-Scott
if you have any help please do leave a message, keep in mind im new to C++
Thanks
-Scott
c Syntax (Toggle Plain Text)
//NOT ENTIRE PROGRAM //Additional work cout << "Do you want the correct answers? (Type yes or no)\n"; char x; cin >> x; //cin.ignore(50, '\n'); float PIa=3.14159; float PI2a=3.14285714; float Ea=2.718281828459; if (x == yes) { system("CLS"); cout << "Have a nice day!\n"; cin.ignore(50, '\n'); } else if (x == no) { cout << "\"PI\" is equal to: " << PIa << endl << "\"E\" is equal to: " << Ea << endl << "\"22/7\" is equal to: " << PI2a << endl; } else { cout << "You did not enter a valid character\n"; } system("PAUSE"); return 0; }
You have to remember that this code is only inputting one charecter:
Because
Better yet, to make it more C++, use a
Of course, you may have only been intending to hold one charecter, but then when I saw this, it made be think that you wanted to input more:
Now you've got several things wrong with this. The most obvious mistake is that you're not using quotes around the string; thus it will confuse the compiler. Secondly, unless the variable is only 1 charecter long (see previous section), you cannot compare C-style strings with ==. If you want to compare a string, you must use strcmp(). Yet another reason to use string instead of char (strings can be compared with ==).
Lastly, this isn't entirely incorrect but...
A more portable and standard method to do this is simply to use
Hope this helps
C++ Syntax (Toggle Plain Text)
cout << "Do you want the correct answers? (Type yes or no)\n"; char x; cin >> x;
char only can hold one charecter, you'll need an array of letters to be able to hold a response that's more than one letter long. For example: C++ Syntax (Toggle Plain Text)
char name[200]; // holds a name up to 200 charecters
string, which does not have to be an array, and instead dynamically allocates the memory needed.Of course, you may have only been intending to hold one charecter, but then when I saw this, it made be think that you wanted to input more:
c Syntax (Toggle Plain Text)
if (x == yes)
Lastly, this isn't entirely incorrect but...
c Syntax (Toggle Plain Text)
system("PAUSE");
c Syntax (Toggle Plain Text)
cin.get();
Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- can someone plz help me with this? (Visual Basic 4 / 5 / 6)
- I NEED SUPPORT **"IMPORTANT"** PLZ HELP (Windows NT / 2000 / XP)
- Windows media player (Windows NT / 2000 / XP)
- Bridge.dll...Make it go away, Plz (Viruses, Spyware and other Nasties)
- can sum1 look @ dis plz (Viruses, Spyware and other Nasties)
- Hijackthis log file - plz help (Viruses, Spyware and other Nasties)
- IE not working...PLZ help :cry: (Web Browsers)
- PLZ help it's urgent! (Web Browsers)
- plz help ppl...... (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: problem with calendar..
- Next Thread: Linked Lists and Copy Constructor
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






