| | |
C++ if statement help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2005
Posts: 4
Reputation:
Solved Threads: 0
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.
<< 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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; // main() int main() { int number; string numeral; cout << "Please enter a number between 1 and 10 "; cin >> number; { if(number == 1) numeral = "I"; else if(number == 2) numeral = "II"; else if(number == 3) numeral = "III"; else if(number == 4) numeral = "IV"; else if(number == 5) numeral = "V"; else if(number == 6) numeral = "VI"; else if(number == 7) numeral = "VII"; else if(number == 8) numeral = "VIII"; else if(number == 9) numeral = "IX"; else if(number == 10) numeral = "X"; else cout << "The number entered was not between 1 and 10"; } cout << "The Roman numeral for the number " << number << " is " << numeral; cin.ignore(); cin.get();
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; // main() int main() { int number; string numeral; cout << "Please enter a number between 1 and 10 "; cin >> number; { if(number == 1) numeral = "I"; else if(number == 2) numeral = "II"; else if(number == 3) numeral = "III"; else if(number == 4) numeral = "IV"; else if(number == 5) numeral = "V"; else if(number == 6) numeral = "VI"; else if(number == 7) numeral = "VII"; else if(number == 8) numeral = "VIII"; else if(number == 9) numeral = "IX"; else if(number == 10) numeral = "X"; else cout << "The number entered was not between 1 and 10"; } cout << "The Roman numeral for the number " << number << " is " << numeral; cin.ignore(); cin.get();
You have two options:
- Print a message for each condition C++ Syntax (Toggle Plain Text)
- if(number == 1)
- {
- cout << "The Roman Numeral for the number 1 is I\n";
- }
- else if(number == 2)
- {
- cout << "The Roman Numeral for the number 2 is II\n";
- }
- // etc
- Divide the logic into two different sets: valid number and invalid number C++ Syntax (Toggle Plain Text)
- if((number < 1) && (number > 10))
- {
- cout << "The number entered was not between 1 and 10\n";
- }
- else
- {
- if(number == 1)
- numeral = "I";
- else if(number == 2)
- numeral = "II";
- //etc
- cout << "The Roman numeral for the number " << number << " is " << numeral;
- }
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
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
•
•
Join Date: Jun 2005
Posts: 11
Reputation:
Solved Threads: 0
I found these 2 books very helpful
Thinking in c++ volumes 1 and 2
You find links to download them at www.BruceEckel.com
Thinking in c++ volumes 1 and 2
You find links to download them at www.BruceEckel.com
![]() |
Similar Threads
- missing return statement (Java)
- MySQL LIKE statement (MySQL)
- loop in main function to an "if" statement (C++)
- Switch Case Statement (Java)
- run sql statement in asp (ASP)
- switch/case statement (C++)
- change statement (JSP)
- Reading MSWord Document through an ASP Statement (ASP)
Other Threads in the C++ Forum
- Previous Thread: Building DLLs in VC++
- Next Thread: Need Your Guide
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





