-1

So I'm producing this program that can only accept a numerical value from 1 to 5, and by only using switch statements, I have to turn that numerical value into the respective roman numeral. I'm having trouble with the int case, as I already tried it with double quotes around the numbers, as I'm sure single quotes are for characters. I made sure to include iostream and have int = num;

#include <iostream>  //preprocessor directives are included

using namespace std;

int main() {
    int num = 0;

    cout << "Enter a number from 1 to 5: " << endl;
    cin >> num;
    switch (num) {
    case "1" :
        cout << "I" << endl;
        break;
    case "2" :
        cout << "II" << endl;
        break;
    case "3" :
        cout << "III" << endl;
        break;
    case "4" :
        cout << "IV" << endl;
        break;
    case "5" :
        cout << "V" << endl;
        break;
    }

}
galois
  • 825
  • 2
  • 11
  • 31
Can
  • 7
  • 2

2 Answers2

4

You are comparing against the string values, not an int. Drop the quotes from each case statement.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • I keep getting the error that case label does not reduce to an integer constant. I also dropped the quotes. – Can Oct 16 '16 at 00:15
  • #include using namespace std; int main() { int num = 0; cout << "Enter a number from 1 to 5: " << endl; cin >> num; switch (num) { case 1 : cout << "I" << endl; break; case 2 : cout << "II" << endl; break; case 3 : cout << "III" << endl; break; case 4 : cout << "IV" << endl; break; case 5 : cout << "V" << endl; break; } } – Can Oct 16 '16 at 00:16
  • The program won't run because of the "case label does not reduce to an integer constant" message I get before trying to run it. – Can Oct 16 '16 at 00:18
  • @Can http://stackoverflow.com/questions/14069737/switch-case-error-case-label-does-not-reduce-to-an-integer-constant – Daniel A. White Oct 16 '16 at 00:20
  • Got it to work. Thanks alot for the help. Much appreciated! – Can Oct 16 '16 at 00:26
0

you can enter character or a string or anything to cin whatever the type of variable is being inputted! so entering a string as integer will cause an unknown behavior or infinite loop.

sorry there is no way to tell cin that you want only numbers or number from 1 to 5.

the solution is:

use exception handling:

#include <iostream>

int main() 
{
    int num = 0;
    std::cin.exceptions();

    try{
            std::cout << "Enter a number from 1 to 5: " << std::endl;
            std::cin >> num;
            if(std::cin.fail())
                throw "Bad input!";
            if(num > 5 || num < 1)
                throw "only numbers 1 throug 5 are allowed!";
    }
    catch(char* cp)
    {
        std::cout << cp << std::endl;
    }
    catch(...)
    {
        std::cout << "An error occuered sorry for the inconvenience!" << std::endl;
    }

    switch (num)
    {
        case 1 :
            std::cout << "I" << std::endl;
        break;
        case 2 :
            std::cout << "II" << std::endl;
        break;
        case 3 :
            std::cout << "III" << std::endl;
        break;
        case 4 :
            std::cout << "IV" << std::endl;
        break;
        case 5 :
            std::cout << "V" << std::endl;
        break;
        //default:
        // std::cout "Bad input! only numbers 1 through 5" << std::endl;
    }

    return 0;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27