944,004 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 25111
  • C++ RSS
Sep 15th, 2005
0

C++ if statement help

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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wdowell84 is offline Offline
4 posts
since Sep 2005
Sep 15th, 2005
0

Re: C++ if statement help

C++ Syntax (Toggle Plain Text)
  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
    C++ Syntax (Toggle Plain Text)
    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
    C++ Syntax (Toggle Plain Text)
    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?
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 15th, 2005
0

Re: C++ if statement help

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.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Sep 15th, 2005
0

Re: C++ if statement help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wdowell84 is offline Offline
4 posts
since Sep 2005
Sep 15th, 2005
0

Re: C++ if statement help

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.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Sep 15th, 2005
0

Re: C++ if statement help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wdowell84 is offline Offline
4 posts
since Sep 2005
Sep 16th, 2005
0

Re: C++ if statement help

I found these 2 books very helpful
Thinking in c++ volumes 1 and 2
You find links to download them at www.BruceEckel.com
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Judas is offline Offline
11 posts
since Jun 2005

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: Building DLLs in VC++
Next Thread in C++ Forum Timeline: Need Your Guide





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


Follow us on Twitter


© 2011 DaniWeb® LLC