954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ if statement help

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.

#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();


<< 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.

wdowell84
Newbie Poster
4 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 
#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();

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

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 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; }

The second solution is the better solution.

Does that help you out?

chrisbliss18
Posting Shark
917 posts since Aug 2005
Reputation Points: 38
Solved Threads: 25
 

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.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

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.

wdowell84
Newbie Poster
4 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

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.

wdowell84
Newbie Poster
4 posts since Sep 2005
Reputation Points: 10
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

Judas
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You