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