C++ if statement help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 4
Reputation: wdowell84 is an unknown quantity at this point 
Solved Threads: 0
wdowell84 wdowell84 is offline Offline
Newbie Poster

C++ if statement help

 
0
  #1
Sep 15th, 2005
Hello, noob here. I just started taking a C++ class this semester and I've been understanding everything so far, but I'm sorta confused on the nested if statements. My homework is to have the user enter a number and it'll show the roman numeral. However whenever someone enters an invalid number it still executes both statements.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // main()
  6. int main()
  7. {
  8.  
  9. int number;
  10. string numeral;
  11.  
  12. cout << "Please enter a number between 1 and 10 ";
  13. cin >> number;
  14. {
  15. if(number == 1)
  16. numeral = "I";
  17. else if(number == 2)
  18. numeral = "II";
  19. else if(number == 3)
  20. numeral = "III";
  21. else if(number == 4)
  22. numeral = "IV";
  23. else if(number == 5)
  24. numeral = "V";
  25. else if(number == 6)
  26. numeral = "VI";
  27. else if(number == 7)
  28. numeral = "VII";
  29. else if(number == 8)
  30. numeral = "VIII";
  31. else if(number == 9)
  32. numeral = "IX";
  33. else if(number == 10)
  34. numeral = "X";
  35. else cout << "The number entered was not between 1 and 10";
  36. }
  37. cout << "The Roman numeral for the number " << number << " is " << numeral;
  38. cin.ignore();
  39. cin.get();
<< moderator edit: added [code][/code] tags >>


For some reason it's always printing out the last cout. Also everything is lined up in the program i just had it moved in the post so it'd fit.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: C++ if statement help

 
0
  #2
Sep 15th, 2005
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // main()
  6. int main()
  7. {
  8. int number;
  9. string numeral;
  10.  
  11. cout << "Please enter a number between 1 and 10 ";
  12. cin >> number;
  13.  
  14. {
  15. if(number == 1)
  16. numeral = "I";
  17. else if(number == 2)
  18. numeral = "II";
  19. else if(number == 3)
  20. numeral = "III";
  21. else if(number == 4)
  22. numeral = "IV";
  23. else if(number == 5)
  24. numeral = "V";
  25. else if(number == 6)
  26. numeral = "VI";
  27. else if(number == 7)
  28. numeral = "VII";
  29. else if(number == 8)
  30. numeral = "VIII";
  31. else if(number == 9)
  32. numeral = "IX";
  33. else if(number == 10)
  34. numeral = "X";
  35. else cout << "The number entered was not between 1 and 10";
  36. }
  37.  
  38. cout << "The Roman numeral for the number " << number << " is " << numeral;
  39.  
  40. cin.ignore();
  41. cin.get();
I cleaned up your code a bit. You will notice that your set of curly braces surrounding the if statements isn't doing anything. You want one of two messages printed, but you have a series of eleven conditions that can be met. Only one of the conditions prints a message. Irregardless of which condition is met, the final message is printed. Thus, if the else statement is used, two messages will be printed.

You have two options:
  • Print a message for each condition
    1. if(number == 1)
    2. {
    3. cout << "The Roman Numeral for the number 1 is I\n";
    4. }
    5. else if(number == 2)
    6. {
    7. cout << "The Roman Numeral for the number 2 is II\n";
    8. }
    9. // etc
  • Divide the logic into two different sets: valid number and invalid number
    1. if((number < 1) && (number > 10))
    2. {
    3. cout << "The number entered was not between 1 and 10\n";
    4. }
    5. else
    6. {
    7. if(number == 1)
    8. numeral = "I";
    9. else if(number == 2)
    10. numeral = "II";
    11. //etc
    12.  
    13. cout << "The Roman numeral for the number " << number << " is " << numeral;
    14. }
The second solution is the better solution.

Does that help you out?
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: C++ if statement help

 
0
  #3
Sep 15th, 2005
Eventually, you'll be able to do the same thing with a switch statement, but understanding how and when to use nested ifs is important.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 4
Reputation: wdowell84 is an unknown quantity at this point 
Solved Threads: 0
wdowell84 wdowell84 is offline Offline
Newbie Poster

Re: C++ if statement help

 
0
  #4
Sep 15th, 2005
Thanks a lot that helped out a ton. Also I was wondering if anyone knows of a good book or something that'll help out with C++ I'm trying to teach my girlfriend (we're taking the same class) but she's still confused about everything.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: C++ if statement help

 
0
  #5
Sep 15th, 2005
Isn't there a topic at the top of the forum about good C/C++ books?

Whatever you do, avoid anything written by Herb Schlidt. (Thanks again, Dave!)
He's easy to read, but chock full o' errors.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 4
Reputation: wdowell84 is an unknown quantity at this point 
Solved Threads: 0
wdowell84 wdowell84 is offline Offline
Newbie Poster

Re: C++ if statement help

 
0
  #6
Sep 15th, 2005
yeah i saw that, but I was just wondering if anyone knows of any other ones that might be helpful too, or a video or something. But thanks alot everyone.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 11
Reputation: Judas is an unknown quantity at this point 
Solved Threads: 0
Judas Judas is offline Offline
Newbie Poster

Re: C++ if statement help

 
0
  #7
Sep 16th, 2005
I found these 2 books very helpful
Thinking in c++ volumes 1 and 2
You find links to download them at www.BruceEckel.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC