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

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.

#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?

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.

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.

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.

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.

I found these 2 books very helpful
Thinking in c++ volumes 1 and 2
You find links to download them at www.BruceEckel.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.