Program not functioning right.

I have almost completed my project the last thing I can not for the life of me figure out is why when I input 'B' into 'currentresponse99' it will not return but instead continue to out put the 'What Happened' prompt. It is probably a stupid fix but I have been staring at this for hours and can't figure out what's wrong.

**In addition 'N' works fine, the function runs and it kicks it back to main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int punt (){
    char currentresponse;
    char currentresponse99;
    int currentyards;
    int playernumber;
    cout << "What happened?" << endl << "B - Fair Catch" << endl << "N - Returned" << endl;
    cin >> currentresponse99;
    if (currentresponse99 == 'N'){
        cout << "How many yards was the return?" << endl;
        cin >> currentyards;
        cout << "Who returned it? (Player #)" << endl;
        cin >> playernumber;
        while (currentresponse != 'Y' && currentresponse != 'N'){
            cout << "Was it returned for a touchdown? (Y or N)" << endl;
            cin >>  currentresponse;
        }
        if (teamswap == 1){
            home.kickoff(currentyards);
            homep.specyards(currentyards,playernumber);
            if (currentresponse == 'Y'){
                home.returnfortd();
                homep.spectds(playernumber);
                extrapoint();
            }
        changeteam();
        }
        else if (teamswap == 0){
            away.kickoff(currentyards);
            awayp.specyards(currentyards,playernumber);
            if (currentresponse == 'Y'){
                away.returnfortd();
                awayp.spectds(playernumber);
                extrapoint();
            }
        changeteam();
        }
    }
    else if (currentresponse99 == 'B'){
        changeteam();
        return 0;
    }
    return 0;
}


Here's more code to give more context:
1
2
3
4
5
6
7
8
char mainprompt(){
    char playresponse;
    while (playresponse != 'P' && playresponse != 'Q' && playresponse != 'R' && playresponse != 'X' && playresponse != 'T' && playresponse != 'C' && playresponse != 'K' && playresponse != 'H' && playresponse != 'U' && playresponse != 'E'){
        cout << "What was the play?" << endl << "P - Pass          Q - QB Kneel" <<  endl << "R - Run           X - Penalty" << endl << "T - Turnover      C - Sack" << endl << "K - Fieldgoal     H - Halftime" << endl << "U - Punt          E - Game Over" << endl;
        cin >> playresponse;
    }
    return playresponse;
}


Function Inside Main where Punt is called. All the other functions work as intended.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    while (playcin != 'E'){
        mainprompt();
        playcin = mainprompt();
        if (playcin == 'P'){
            pass();
        }
        else if (playcin == 'Q'){
            qbkee();
        }
        else if (playcin == 'R'){
            run();
        }
        else if (playcin == 'X'){
            string penresponse;
            cout << "Which team is it against? (" << team1 << " or " << team2 << ")" << endl;
            cin >> penresponse;
            if (penresponse == team1){
                home.penalty();
            }
            else if (penresponse == team2){
                away.penalty();
            }
        }
        else if (playcin == 'T'){
            turnover();
        }
        else if (playcin == 'C'){
            sack();
        }
        else if (playcin == 'K'){
            fieldgoal();
        }
        else if (playcin == 'U'){
            punt();
        }
        else if (playcin == 'H'){
            halftime();
        }
    }
Last edited on
(With your current code logic) your "What happened" prompt is not within a loop, but IS the first executable line in your function ... so the only way to get to it is to call this function again ... so what is wrong is probably OUTSIDE this particular code fragment. Look at any statement in the rest of your code which calls punt() (or revisit your logic if you intended "What happened" to be in a loop).

currentresponse (as opposed to currentresponse99) isn't defined on first pass in line 13, but this won't cause your problem.

Other variables, like teamswap and objects home and away must, presumably, be global.
Last edited on
home and away are userdefined classes and teamswap is a global variable, however I assumed it can't be affected by one of these due to the fact that 'N' runs its functions, stops the loop and goes back to main. It is only 'B' that has this problem.
Please don't back-edit your original post like this - it completely breaks the thread of subsequent comments.

As far as I can see, punt() will only be called if playcin is 'U'. Then the while loop must run again and goes to mainprompt (twice - no idea why).

In mainprompt(), you try to test a condition on playresponse - a local variable which, at first pass whenever this function is called (i.e. calling twice makes no difference whatsoever) will be undefined.
Topic archived. No new replies allowed.