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.