944,120 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3209
  • C++ RSS
Jan 12th, 2007
0

If Else If Help plz

Expand Post »
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

  1.  
  2. //NOT ENTIRE PROGRAM
  3. //Additional work
  4. cout << "Do you want the correct answers? (Type yes or no)\n";
  5.  
  6. char x;
  7. cin >> x;
  8. //cin.ignore(50, '\n');
  9.  
  10.  
  11. float PIa=3.14159;
  12. float PI2a=3.14285714;
  13. float Ea=2.718281828459;
  14. if (x == yes)
  15. {
  16. system("CLS");
  17. cout << "Have a nice day!\n";
  18. cin.ignore(50, '\n');
  19. }
  20. else if (x == no)
  21. {
  22. cout << "\"PI\" is equal to: " << PIa << endl
  23. << "\"E\" is equal to: " << Ea << endl
  24. << "\"22/7\" is equal to: " << PI2a << endl;
  25.  
  26. }
  27. else
  28. {
  29. cout << "You did not enter a valid character\n";
  30. }
  31. system("PAUSE");
  32. return 0;
  33. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
killerdrummer2 is offline Offline
4 posts
since Jul 2006
Jan 12th, 2007
0

Re: If Else If Help plz

You have to remember that this code is only inputting one charecter:
C++ Syntax (Toggle Plain Text)
  1. cout << "Do you want the correct answers? (Type yes or no)\n";
  2.  
  3. char x;
  4. cin >> x;
Because 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)
  1. char name[200]; // holds a name up to 200 charecters
Better yet, to make it more C++, use a 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:
  1. if (x == yes)
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...
  1. system("PAUSE");
A more portable and standard method to do this is simply to use
  1. cin.get();

Hope this helps
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem with calendar..
Next Thread in C++ Forum Timeline: Linked Lists and Copy Constructor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC