C++ Tip please

Basically I'm trying to make an "account recovery" program that ask max of 3 questions in order to recover a user password, if user gets "question 1" wrong it'll display "You have been banned!", but if user gets "question 2" wrong it'll ask one more question before banning the user.

I want to know is using "switch" the best method, or is there a simpler way

I'm sorry if this question offends you, I'm obviously new to C++

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>

using namespace std;

void lastchance()
{
	string oka;
	string okab;
	string answer;
	string sq3 = "ReizeiMako";
	string password = "longshot123";
	cout << "Sercurity Question 3: Who is your Waifu?\n";
	cin >> answer;
		if (answer == sq3)
		{
			cout << "Your account has been recovered!, Your password is " << password << " Thank you for waiting.\n";
			cout << "ok\n";
			cin >> oka;
		}
		else
		{
			cout << "Please contact customer support\n";
			cout << "Email: sales@hellahard.com\n";
			cout << "ok\n";
			cin >> okab;
		}
		cout << "ok\n";
			cin >> okab;
}

void correct()
{
	int last;
	string ok;
	string password = "longshot123";
	string input2;
	cout << "Sercurity Question 2: What is the name of the anime?\n";
	cout << "1. Watamote\n";
	cout << "2. Girl Panzer\n";
	cin >> last;
		if (last == 2)
		{
			cout << "Your account has been recovered!, Your password is " << password << " Thank you for waiting.";
			cout << "ok";
			cin >> ok;
		}
		else
		{
			switch (last)
			{
			case 1:
				lastchance();
				break;


		}

		}
}





int main()
{
	
	int input;
	string username;

	cout << "What account do you want to recover?\n";
	getline(cin, username, '\n');
    
	if (username == "ChromeHF")
	{
		cout << "Sercurity Question 1: Which is your favorite Anime Charactor?\n";
		cout << "1. Kuroki Tomoko \n";
		cout << "2. Reizei Mako \n";
		cin >> input;

			switch (input)
			{
			case 1:
				cout << "You have been IP Banned!\n";
				cin.get();
				break;
			case 2:
				correct();
				break;
			}
	}
	else
	{
		cout << "Sorry, that account is not in our database.\n";
		cin.get();
	}			
			
}
Last edited on
First things first. Put prototype definitions for function above main and implementations for functions beneath main.

The logic of your program has you asking the first question in the main function (lines 77-80) but more questions are asked inside functions. I wold either put all input/output in the main or in other functions, but not mix the two (given the programs simplicity, I would put all input/output in the main).

Given your switch function has only two possibilities (right or wrong), it would make more since to use an if/else. If they fail the first test, then they will fall out of the loops and the program will end. Otherwise, if their answer is correct, another if/else can be used to determine the second question.

If you are hard set on using functions to make your main() prettier, then you might as well make everything you do in the main one large function, which is counter-intuitive for a simple program.

Good luck!
It really would be best to use the if and else statement
This isn't very good design but it is kind of what I was describing:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>

using namespace std;

int main()
{
    int input;
	string username;
	string password = "longshot123";
	bool givePassword = false;

	cout << "What account do you want to recover?\n";
	getline(cin, username, '\n');

	if(username == "ChromeHF")
	{
	    //Prompt user with security question
		cout << "Sercurity Question 1: Which is your favorite Anime Charactor?\n";
		cout << "1. Kuroki Tomoko \n";
		cout << "2. Reizei Mako \n";

		//User presses a key to respond
        cin >> input;
        cout << endl;

        //Continues to ask until a valid input is recieved
        while(input != 1 && input != 2)
        {
            cout << "Invalid input. Try again.\n";
            cin >> input;
            cout << endl;
        }

        //Correct
        if(input == 2)
        {
            cout << "\nSercurity Question 2: What is the name of the anime?\n";
            cout << "1. Watamote\n";
            cout << "2. Girl Panzer\n";
            cin >> input;
            cout << endl;

            while(input != 1 && input != 2)
            {
                cout << "Invalid input. Try again.\n";
                cin >> input;
                cout << endl;
            }

            if(input == 2)
            {
                cout << "You have answered a sufficient amount of security qestions correctly!\n";
                givePassword = true;
            }
            else
            {
                cout << "Incorrect. Last chance question.";
                string sq3 = "ReizeiMako";
                string answer;
                cout << "Sercurity Question 3: Who is your Waifu?\n";

                cin >> answer;
                
                //Correct
                if(answer == sq3)
                {
                    cout << "You have answered a sufficient amount of security qestions correctly!\n";
                    givePassword = true;
                }
  
                //Wrong
                else
                {
                    cout << "You have been IP Banned!\n";
                }
            }

        }
        //Wrong
        else
        {
            cout << "You have been IP Banned!\n";
        }
	}

	else
	{
		cout << "Sorry, that account is not in our database.\n";
		cin.get();
	}

	//Give user the password
	if(givePassword)
	{
	    cout << "Your password is: " << password;
	}

    return 0;
}
Last edited on
@PrivateRyan

Damn thanks bro, that makes it way more clear if I was to go back to it & creates an easier flow when coding with just if and else

Thanks for the example aswell this really helped me alot
Topic archived. No new replies allowed.