Function not properly executing

Hi, so I'm new to the forum. My name is Alex, I am currently learning C/C++ off of the website: www.learncpp.com. I am fairly new at programming, only about a week or two into coding. With that said, I am an eager learner and need some help with a practice program I am developing.

I cannot seem to get the Registration() function to properly execute the Login() function when the user chooses that they are a returning user. It is explained a little more in detail with the comments of the code.

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
/* Employee Registry Program.cpp : 
Expected results of this program:

Asks the user if they are new or returning. 

Choosing "new" will direct the user to registration, where the user will 
input a chosen username and password, which will be saved into a directory. 

Choosing "returning" will direct the user to the login screen where they 
will input a valid username and password. 

After the user is logged in, the program will display a menu of actions the 
user would like to perform.
*/

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

/*
Login() asks the user to enter a valid username and password. If the user
fails to enter a valid username and password the function informs the user
of the failure to login and reexecutes.
*/

void Login()
{
	string LogPassword;
	string LogUsername;

TryAgain:

	cout << "Please enter the your username: " << endl;
	cin >> LogUsername;
	cout << "\n";

	cout << "Please enter your password: " << endl;
	cin >> LogPassword;
	cout << "\n";

		if (LogPassword=="user" && LogUsername=="password")							
			{
				cout << "Login credentials accepted." << endl;
				cout << "\n";			
			}

		else 
			{
				cout << "Login credentials denied. Please try again." << endl;
				cout << "\n";
				goto TryAgain;
			}

}

/*
Registration() asks the user if they are new or returning. If "new,
the function directs them to registration. If "returning" the 
function directs the user to the login screen. [Login()]

Problem: Cannot get function to execute Login() function if user is 
"returning".

*/

void Registration()
{
	string RegResponse;

	cout << "Are you a new or returning user?" << endl;
	cin >> RegResponse;

	if (RegResponse=="new"||"New"||"NEW")
		{
			string RegUsername;
			string RegPassword;

			cout << "Please choose and enter a username:" << endl;
			cin >> RegUsername;
			cout << "\n";

			cout << "Please choose and enter a password:" << endl;
			cin >> RegPassword;
			cout << "\n";

			cout << "Thank you for registering, you may proceed to the login screen." << endl;
			cout << "\n";
		}

	else (RegResponse=="returning"||"Returning"||"RETURNING");
		{
			Login();
		}
}

int main()
{
	cout << "Welcome to <insert company name>." << endl;
	Registration();
	cout << "Welcome, <user>, to <insert company name>'s employee registry." << endl;

	system ("PAUSE");
	return 0;
	
}
Last edited on
closed account (zb0S216C)
There's multiple problems here:

1) if/else if/else statements shouldn't be followed by a semi-colon.

2)
darkliight wrote:
if (RegResponse=="new"||"New"||"NEW") (sic)

This should be: if( RegResponse=="new"|| RegResponse=="String" ... )

3) else statements cannot have a condition. Instead, use else if.

Wazzak
Last edited on
closed account (zb0S216C)
darkliight wrote:
Can you explain what adding RegResponse=="String" did to fix the issue? (sic)

When you use a string as single operand within an expression, the operand is tested for either true or false. For example:

 
if( 3 || 5 )

This if statements is the same as:

 
if( true || true )

Since non-zero values are always true, the operand is always true. So, when you used a string as an operand, the if statement looked like this:

 
if( RegResponse == "new" || true || true )

if statements always evaluate to either true or false. When comparing an operand against a literal or pre-defined value, always do this:

1
2
3
int Var( 10 );

if( Var == 10 || Var == 12 || ... )


Wazzak
Last edited on
Say I wanted to add a error message to this program that informs the user of the invalid input and re-executes Registration() when the user does not enter either 'new' or 'returning'...

...how would I go about adding that into the function. I've tried adding another else if statement but that is illegal. I've tried adding another if statement but it doesn't seem that the compiler ever recognizes this statement.

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
void Registration()
{
	string RegResponse;

RegTryAgain:

	cout << "Are you a new or returning user?" << endl;
	cin >> RegResponse;

	if (RegResponse=="new")
	{
		string RegUsername;
		string RegPassword;

		cout << "Please choose and enter a username:" << endl;
		cin >> RegUsername;
		cout << "\n";

		cout << "Please choose and enter a password:" << endl;
		cin >> RegPassword;
		cout << "\n";

		cout << "Thank you for registering, you may proceed to the login screen." << endl;
		cout << "\n";
	}

	else if (RegResponse=="returning");
	{
		cout << "\n";
		Login();
	}
	
	if (RegResponse!="new" || RegResponse!="returning")
	{
		cout << "Error. Please enter either 'new' or 'returning'." << endl;
		cout << "\n";
		goto RegTryAgain;
	}	
}
Last edited on
closed account (zb0S216C)
It's best not to use goto since it's been deprecated along time ago. It's still around to allow backwards compatibility for legacy programs. Instead, use something like this:

1
2
3
4
while( ( LogPassword != "user" ) && ( LogUsername != "password" ) )
{
    // ...
}

This loop will continue while LogPassword and LogUsername contain strings other than the specified string literals.

Wazzak
Ah ok, thank you so much I'll be sure to remember that advice regarding goto. A while statement, of course. Thanks, I'm trying to self-study C++ and finding it difficult seeing as how there is no one around (irl) to discuss the topic with, but with the help of this forum and a lot of practice I should have it down in no time.
Last edited on
closed account (zb0S216C)
darkliight wrote:
I'm trying to self-study C++ and finding it difficult (sic)

You're not alone there; I'm in the same boat.

Wazzak
Topic archived. No new replies allowed.