Click here to Skip to main content
15,891,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a menu using switch(). However, the compiler keeps throwing me errors like invalid conversion of 'const char*' to 'char' [-fpermissive]. Here is the code:
C++
void info::get_factorio_version(string* factorio_version){
    bool loop = true;
    while(loop){
    cout << "What version of Factorio is this mod for?" << endl;
    cout << "   1 - 0.14.xx" << endl;
    cout << "   2 - 0.13.xx" << endl;
    cout << "   3 - 0.12.xx or earlier" << endl;
    cout << "Your selection: ";
    int select; cin >> select;
    switch(select){
        case '1': {
            *factorio_version = "0.14";
            cout << "\n\n";
            loop = false;
            } break;
        case '2': {
            *factorio_version = "0.13";
            cout << "\n\n";
            loop = false;
            } break;
        case '3': {
            *factorio_version = "NULL";
            cout << "\n\n";
            loop = false;
            } break;
        default: {
            cout << "\n\nInvalid response.\n\n";
            } break;}}}

Is there some issue with my code?

What I have tried:

I have tried using:
C++
string selectSTR; cin >> stringSTR;
int selectINT = atoi(selectSTR);

As well as:
C++
string selectSTR; cin >> stringSTR;
char selectCHAR = selectSTR.c_str();
Posted
Updated 22-Sep-16 5:52am

1 solution

You have declared select as int, but your case statements are using character constants. they should be like:
C++
switch(select){
    case 1:
        *factorio_version = "0.14";

Also you do not need block separators ({ and }) surrounding your case code.
 
Share this answer
 
Comments
Anti-Antidote 22-Sep-16 11:58am    
How would I declare select as char const*? Also, I put brackets around the case code to prevent anything from escaping (better safe than sorry).
Richard MacCutchan 22-Sep-16 12:06pm    
You would have to declare the source as a character array. But why would you do that, it serves no purpose?
Anti-Antidote 22-Sep-16 14:06pm    
Oh. Sorry, I misunderstood the answer, but now I know. Thank you!
jeron1 22-Sep-16 12:19pm    
"How would I declare select as char const*?"
You wouldn't, take a closer look at the case statement in Richards post. If you're still having compilation issues, maybe update your post to added the error to the line it occurs on.

"anything from escaping" What do you mean by that?
Anti-Antidote 22-Sep-16 14:08pm    
I was told that in some cases memory could leak from case code if you didn't bracket them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900