SWITCH PASSWORD PROGRAM

I'm having a problem converting the password program to a switch this is the first choice in the program Menu: In your main function, present the user with a numbered menu (choices 1, 2, or 3) that allows them to run the following tasks. You MUST implement the menu using a switch statement.
1. For the first choice, ask the user for a password. Then ask them to type their password again. Compare what they typed each time and provide a message indicating whether or not their password matched. Here's my code so far Id appreciate any help
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
 
/*Menu: In your main function, present the user with a numbered menu
(choices 1, 2, or 3) that allows them to run the following tasks.
You MUST implement the menu using  a switch statement.*/



#include<iostream>
#include<string>
using namespace std;


int main()
/*
1. For the first choice, ask the user for a password. Then ask them to type their password again.
	Compare what they typed each time and provide a message indicating whether
	or not their password matched.*/

{
	int choice;
	const int password = 1,
		const int division_woohoo = 2,
		const int division_2;
	char pass1, pass2;

	cout << "Enter choice 1, 2 or 3";
	cin >> choice;

	switch (choice)
	{
	case 1:
		cout << "Enter your 6 character password \n";
		cin >> pass1;
		cout << "re-enter your password /n";
		cin >> pass2;
		
		case pass1 == pass2;
	}















	system("pause");
	return 0;

}

thanks for any help 









the code you need help with here.
I'm not quite sure what some of your variables are intended for, but here's a basic sort of set-up which you might find useful, although I'm sure others can improve upon it:

Your requirements. Menu: In your main function, present the user with a numbered menu (choices 1, 2, or 3) that allows them to run the following tasks. You MUST implement the menu using a switch statement. 1. For the first choice, ask the user for a password. Then ask them to type their password again. Compare what they typed each time and provide a message indicating whether or not their password matched.


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
#include<iostream>
#include<string>

int main()
{
    int choice;
    
    do
    {
    std::cout << "Enter choice 1, 2 or 3:\t";
    std::cin >> choice;
    std::cin.clear();
    std::cin.ignore(1000,'\n');

    std::string pass1, pass2;

	    switch (choice)
	    {
	    case 1:
	    std::cout << "Enter your 6 character password:\n";
	    std::getline(std::cin, pass1);
	    std::cout << "Re-enter your password:\n";
	    std::getline(std::cin, pass2);
	    (pass1==pass2)? std::cout << "Pwds match\n":std::cout << "Pwds don't match\n";
	    break;
	    case 2:
            // stuff ...
            break;
	    case 3:
	    // stuff ...
	    break;
            default:
            std::cout << "You didn't enter 1, 2 or 3\n";
	    }
    }	
    while (choice>0);
     
return 0;
}

Good luck.
Last edited on
Hello agrennan,

In the future in your subject line do not shout, i.e., the use of all caps. Some people tend to consider this rude.

This is the typical setup for a switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch (choice)
{
	case 1:
		cout << "Enter your 6 character password \n";
		cin >> pass1;
		cout << "re-enter your password /n";
		cin >> pass2;

		if (pass1 == pass2)
			// <--- Do something.
		break;
	case 2:
		break;

	case 3:
		break;

	default:
		break;
}

You have a good start, but it fell apart at line 37.

The if statement is just a guess here.

Without the "break" statement to end the case it would fall through to the next case statement. Unless you have code to make sure that "choice" will always have a matching case statement you should end the switch with the "default" case. Here you can put a message to explain the problem before leaving the switch and trying again.

If you have learned about functions you could use the switch to call a function to do the work. This would be a better use of the switch than trying to put all your code in the case statements.

Hope that helps,

Andy
Topic archived. No new replies allowed.