Program does not turn off

closed account (EwCjE3v7)
Okay I have updated this code to not use the main() call. But I still would like to know what caused the problem in my old code(below). Do the following

1. Compile and run the code
2. guess the country and wait for it to tell you would you like to play again, the first time type 'y' but once you guess the second country, type 'n'. It should turn of but does not

(This is the code that I`m not using but still want to know the problem.)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
using std::cout; using std::endl; using std::cin;
using std::vector; using std::string;

int main()
{
    vector<string> countries{"Afghanistan", "Brazil", "Cuba"};
    unsigned counter = 0;
    
    srand(time(NULL));
    int randomIndex = rand() % countries.size();
    
    cout << "We will give you the first and last letter of a country, you have to guess the country. Points will be counted." << endl;
    
    for (auto c : countries[randomIndex]) {
        if (counter == 0) {
            cout << "First letter is: " << c << endl;
        }
        else if (counter == countries[randomIndex].size() - 1) {
            cout << "Last letter is: " << c << endl;
        }

        ++counter;
    }

    string input;

    while (cin >> input) {
        if (!input.empty()) {
            input[0] = toupper(input[0]);

            if (input == countries[randomIndex]) {
                cout << "Correct!!! Would you like to try again? (y,n)" << endl;
                char tryAgain = 'n';
                cin >> tryAgain;

                if (tryAgain == 'y') {
                    main();
                }
                else if (tryAgain == 'n'){
                    cout << "Bye!" << endl;
                    return 0;
                }

            } else {
                cout << "Incorrect!!! Try again." << endl;
            }
        }
    }

    return 0;
}
Last edited on
YellowPyrmid wrote:
Okay I have updated this code to not use the main() call.
What's that on line 42 then?
closed account (EwCjE3v7)
No I mean, that this is my old code. And I have changed it not to use that. :) Didn`t explain
You want help with your new code but you only show us your old code?
No, no. He wants to know why his old code didn't work; his new code is fine.
Ah, I see - I misread.

@OP: What do you think happens if, somehow, neither y nor n is entered?
closed account (EwCjE3v7)
It's stops if it's the first time an continues after
Last edited on
What do you think happens after the code reaches line 47?
closed account (EwCjE3v7)
Well it should either turn of or do the while loop again. Depending on choice.
The second time for some reason, it prints bye but dosn`t turn of.
Last edited on
No, I'm talking about the end of line 47. You do realize it is possible to get to there?
Last edited on
closed account (EwCjE3v7)
Yea it should go back to the while right?
Topic archived. No new replies allowed.