mardi 17 mars 2020

How to make functions for reverse number guessing game?

For my assignment, I need to make a program that guesses the users number, between 1-19 inclusively, within 5 tries. For each try, the user inputs if the number is a. correct b. too high or c. too low. We are supposed to define two functions. The first is a function that takes a number (int) as a parameter and outputs the prompt to the user that guesses that number (tells the user "Is this your number: ") and shows them a menu that explains how to enter correct, high, or low. The second function is supposed to calculate the next guess after being told if it is too high or too low. I was able to accomplish this with nested switch statements, but I am having trouble trying to come up with the second function. Any help is appreciated. I will try to attach my first program with the switch statements. I assume I need to generate a random number with the min and max, but I'm not sure how to do it.

#include<iostream>
using namespace std;

int main()
{
    int guess = 10;
    int input = 0;
    cout<<"Is this your number: "<< guess<<endl;
    cout<< "Correct? (1), High?(2), Low(3)"<<endl;
    cin>> input;
    switch(input) {
        case(1):
            cout<< "Thanks for playing";
            break;
        case(2):
            guess = 5;
            cout<<"Is this your number: "<< guess<<endl;
            cout<< "Correct? (1), High?(2), Low(3)"<<endl;
            cin>> input;
                switch(input){
                    case(1):
                        cout<< "Thanks for playing";
                        break;
                    case(2):
                        guess = 3;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case(1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 2;
                                cout<<"Is this your number: "<< guess<<endl;
                                                       cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                                       cin>> input;
                                switch(input){
                                    case(1):
                                        cout<< "Thanks for playing";
                                        break;
                                    case (2):
                                        guess = 1;
                                        cout<< "Your guess was: "<<guess<<endl;
                                        break;
                                    case (3):
                                        cout<< "Cheater..."<<endl;
                                        break;
                                }
                                break;
                            case(3):
                                guess = 4;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;

                              }
                        break;
                    case(3):
                        guess = 7;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case (1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 6;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;
                            case(3):
                                guess = 8;
                                cout<<"Is this your number: "<< guess<<endl;
                                cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                cin>> input;
                                switch (input){
                                        case (1):
                                        cout<< "Thanks for playing";
                                        break;
                                        case(2):
                                        cout<<"Cheater..."<<endl;
                                        case(3):
                                        guess = 9;
                                        cout<< "Your guess was: "<< guess<<endl;
                                        break;
                                }


                        }

                        break;
                }

            break;
        case(3):
            guess = 15;
            cout<<"Is this your number: "<< guess<<endl;
            cout<< "Correct? (1), High?(2), Low(3)"<<endl;
            cin>> input;
                switch(input){
                    case(1):
                        cout<< "Thanks for playing";
                        break;
                    case(2):
                        guess = 13;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case(1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 12;
                                cout<<"Is this your number: "<< guess<<endl;
                                                       cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                                       cin>> input;
                                switch(input){
                                    case(1):
                                        cout<< "Thanks for playing";
                                        break;
                                    case (2):
                                        guess = 11;
                                        cout<< "Your guess was: "<<guess<<endl;
                                        break;
                                    case (3):
                                        cout<< "Cheater..."<<endl;
                                        break;
                                }
                                break;
                            case(3):
                                guess = 14;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;
                              }
                        break;
                    case(3):
                        guess = 17;
                        cout<<"Is this your number: "<< guess<<endl;
                        cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                        cin>> input;
                        switch (input){
                            case (1):
                                cout<< "Thanks for playing";
                                break;
                            case(2):
                                guess = 16;
                                cout<< "Your guess was: "<<guess<<endl;
                                break;
                            case(3):
                                guess = 18;
                                cout<<"Is this your number: "<< guess<<endl;
                                cout<< "Correct? (1), High?(2), Low(3)"<<endl;
                                cin>> input;
                                switch (input){
                                        case (1):
                                        cout<< "Thanks for playing";
                                        break;
                                        case(2):
                                        cout<<"Cheater..."<<endl;
                                        case(3):
                                        guess = 19;
                                        cout<< "Your guess was: "<< guess<<endl;
                                        break;
                                }


                        }

                        break;
                }
            break;
    }

















    return 0;

}




Aucun commentaire:

Enregistrer un commentaire