Switching problems

I am not quite sure how to explain this so please take it easy :)

I was assigned a project for a class I am taking. The project is design a program that takes a username and a password. If they are wrong display it. If they are right display a GUI(sorta thing).
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// UserName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>




using namespace std;

int veriUser;


int main()
{
	string username;
	string password;
	do 
		{
			 std::cout << "username: ";
			 getline(std::cin, username);
			 if (username == "Strychnine.213") 
			{
				std::cout << "password: ";
				getline(std::cin, password);
				if (password != "abc123") 
					{
					 std::cout << "Invalid Password. Please make sure the password you entered is correct.!\n." << std::endl;
					}
				else 
					{
					std::cout << "Welcome Nathan.\n" << std::endl;
					std::cout << "Loading user files\n";
					std::cout << "Setting up interface\n";
					std::cout << "Have fun Nathan!\n";
					}
		
			 }
		}

	   while (password != "abc123"); 

	 return veriUser;
}
		 




// defines entry point for user interface

int menu();
void DoTaskOne();
void DoTaskMany(int);


int userface()
{
	
			bool exit = false;
			for (;;)
			{
				int choice = menu();
				switch(choice)
				{
				case (1):
					DoTaskOne();
					break;
				case (2):
					DoTaskMany(2);
					break;
				case (3):
					DoTaskMany(3);
					break;
				case (4):
					continue;
					break;
				case (5):
					exit=true;
					break;
				default:
					cout << "Please select again. " << endl;
					break;
				  } // end switch

				if (exit==true)
					break;
			 }  // end forever

			return 0;
		
}

int menu()
{
	int choice;

	cout << " ****Menu**** " << endl << endl;
	cout << "(1) Choice one" << endl;
	cout << "(2) Choice two " << endl;
	cout << "(3) Choice three" << endl;
	cout << "(4) Redisplay Menu" << endl;
	cout << "(5) Quit. "<< endl << endl;
	cout << " . ";
	cin >> choice;
	return choice;
}

void DoTaskOne()
{
	cout << "Task one" << endl;
}

void DoTaskMany(int which)
{
	if (which == 2)
		cout << "Task two" << endl;
	else
		cout << "Task Three " << endl;
}


My problem is that it goes through the normal route of starting at int main(). It prompts me for my username and password. I enter the correct info that I have defined and closes the window. What it is susposed to do is after prompted and accepted, start the code block for int menu() which should display the GUI. Does anyone know why it appears to stop after the int main() ends and dosen't show the rest of the program? I have compiled both the interface and the main seperatly, and they both come back as working.I am also sorry for such the large post.

Thanks,
Strych!
Don't kick yourself too hard, but you never call menu()
First off move the function declarations up above main()

1
2
3
4
5
6
7
8

int menu();
void DoTaskOne();
void DoTaskMany(int);

int main()
{

Then you can do something like below.
1
2
3
4
5
6
7
					std::cout << "Welcome Nathan.\n" << std::endl;
					std::cout << "Loading user files\n";
					std::cout << "Setting up interface\n";
					std::cout << "Have fun Nathan!\n";
					menu();
					return 0;
					}


You might want to look at your }while() statement and add a counter so the user gets say 3 attempts. If the counter<3 then the while statement would loop, if not the program would end.
Similarly, nothing is called after the cin >> choice; at line 106.

There are a few other problems with the code, but see how you do with the counter idea and the choice bit first.
Last edited on
Thank you for the advice. I got the menu to appear, and I never thought about a loop for user log attempts, that is a GREAT! Idea. If I may ask one more question. It appears with the menu but when I click say 1 it inputs 1 and says "press any key to end". I designed this part of the program with endless loops so that the user could interact with it. You wouldn't happen to know how I can program the loops back in, so when a user inputs 1 that it says "Choice 1" and goes back to the menu, intill the user presses the key for exit(which I belive I set as 5)would you?
If you take a look at how your functions get called you'll see what's going wrong as there's no loop to go back to start the menu. Since I'm feeling generous {B¬) see how I did it below. If you don't want to cheat, look away now!
It's not perfect, but it'll give you an idea if you follow the code through and compare it to what you have.

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
100
101
102
// UserName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

// defines entry point for user interface

void drawmenu();
void DoTaskOne();
void DoTaskMany(int);
void userface();

int main()
{
int	attempts=0;
string username;
string password;

	do 
	{
		std::cout << "username: ";
		getline(std::cin, username);
		if (username == "Strychnine.213") 
		{
			std::cout << "password: ";
			getline(std::cin, password);
			if (password != "abc123") 
			{
				 std::cout << "Invalid Password. Please make sure the password you entered is correct.!\n." << std::endl;
				 attempts++;
			}
			else 
			{
				std::cout << "Welcome Nathan.\n" << std::endl;
				std::cout << "Loading user files\n";
				std::cout << "Setting up interface\n";
				std::cout << "Have fun Nathan!\n";
				userface();
				return 0;	// Everything went well.
			}
		}
		else
		{
			cout << "Username does not exist\n";
			attempts++;
		}
		cout << "You have " << 3-attempts << " more login attempts left.\n";
	}while (attempts<3); 
	cout << "Login Failed, check your user details\n";
	return 1;	// Failed login
}

// defines entry point for user interface

void userface()
{
bool	exit = false;
int		choice = 0;


	do
	{
		drawmenu();
		cin >> choice;
		switch(choice)
		{
			case 1 :	DoTaskOne();		break;
			case 2 :	
			case 3 :	DoTaskMany(choice);	break;	// This will be called on either option 2 or 3
			case 4 :	drawmenu();			break;	// don't really nead this option
			case 5 :	exit=true;			break;
			default:	cout << "Please select again. " << endl;	break;
		} // end switch
	} while(!exit);
}

void drawmenu()
{
	cout << " ****Menu**** " << endl << endl;
	cout << "(1) Choice one" << endl;
	cout << "(2) Choice two " << endl;
	cout << "(3) Choice three" << endl;
	cout << "(4) Redisplay Menu" << endl;
	cout << "(5) Quit. "<< endl << endl;
	cout << " . ";
}

void DoTaskOne()
{
	cout << "Task one" << endl;
}

void DoTaskMany(int which)
{
	if (which == 2)
		cout << "Task two" << endl;
	else
		cout << "Task Three " << endl;
}


Feel free to ask question if something dosn't make sence, there's plenty of knowledge available here.
Last edited on
Thank you Moooce. I can't belive I didn't catch that the first 4 times around.
As a tip, if you use the debugging part of your editor/compiler, you can step through your code line by line, you'd probably have seen what was going wrong pretty quick.
If you're using MS Visual studio you can do this by pressing F10 to step through the code.
Hmm, In VS2010 it is F11.

In Dev C++ it is F7.

I forgot what it is in Eclipse...

It may also good to get into the habit of setting up 'roadblocks' in your code, for parts you think may be causing it to fail... These roadblocks, would be, for example, printing out what part of the program you're at, e.g.
1
2
3
4
5
6
7
8
int main() {
  cout << "I'm in main() now\n Press Enter to continue ... ";
  cin.get();
//other stuff in main() func
  cout << "I'm all done and about to return 0! \nPress Enter to continue ...";
  cin.get();
  return 0;
}

Doing something like that can help you to track down where errors are occuring, or where you missed something, etc. The built in debugger of any IDE is usually sufficient though, but it doesn't hurt, especially if your IDE doesn't do stepping (that'd be weird though).

Anyways,
Good luck with your project & Happy Coding!


Edit: after looking at your code more thoroughly, it is now clear to me that you're using Visual Studio ... lol

VS definately has a full debugger. So press either F11 or F10, depending on version I guess.
Last edited on
Thank you for all the great replies and ideas. Ryan yes I am using Studio ;), I also like the idea of using road blocks . I am going to have to use those more often!
Topic archived. No new replies allowed.