Home > other >  I can't select a different option in my program
I can't select a different option in my program

Time:11-07

I was writing a program inspired by one of my favorite books Diary of a wimpy kid. You play as Greg's dad and you have 3 options with what to do with him. When i ran the program I first selected the first option which printed the first one. I tried it again with the third option as well with the same result.

#include <iostream>

using namespace std;

void greg(int choice){
    cout<<"Option 1: Scold."<<endl;
    cout<<"Option 2: Take away games."<<endl;
    cout<<"Option 3: kill "<<endl;
    cin>>choice;
    //Options on what to do with greg and getting user input.
    if(choice = '1'){
    cout<<"You told Greg he sucks. Responds with:"<<endl;
    cout<<"Ok.."<<endl; 
    } else if(choice = '2'){
    cout<<"You storm into Greg's room while Greg keeps asking you why."<<endl;
    cout<<"Once you are insde and grab his game."<<endl;    
    }else if(choice = '3'){
    cout<<"you killed greg."<<endl;
    cout<<"A white bang then proceeds to happen."<<endl;
    cout<<"You killed the main character. You no longer exist."<<endl;  
        }else{
        cout<<"no"<<endl;   
            }
}
//All above is what will happen if you pick a choice.
int main()
{
    cout<<"There once was a guy named Frank."<<endl;
    cout<<"You talk to greg."<<endl;
    cout<<"Yeah dad?"<<endl;
    greg(3);
    return 0;
}

CodePudding user response:

  1. you have to use a double = because you want to compare the values in the if statement. You dont want to set the left variable equal to the right value.

2.because youre passing a int value into the function, the program will not return true because a int is not equal to a string. So you dont have to use these '' around the number.

In conclusion you have to change this:

...
if(choice == 1){
//your code
}
else if(choice == 2){
//your code
}
else if(choice == 3){
//your code
}
...
  •  Tags:  
  • c
  • Related