bug

It's almost perfect but I noticed that there is one bug. Whenever I choose to not sign up, type random username. It should say invalid username. Please try again. It thinks random username is correct. And I will use #include <ctime> later.

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
#include <iostream>
#include <string>
#include <array>
#include <stdio.h>
#include <ctime>
using namespace std;


int beta[] = { 300, 200, 50, 20, 15 };
int z, y = 0, yes, no;
int main()
{
	string a, b, c, d, e, g, h, i, result, a1, bz, cz;
	int f, answer;
	a1 = "Steel";
 	g = "Bruce Lee";
	i = "Programmer";
	h = "Steel2";
 	yes = 1;
	no = 2;
	do
	{
		std::cout << "Do you need to sign up? Please choose numbers. 1 = yes 2 = no" << endl;
		std::cin >> no;
		if (no == 2)
		{
			{
				std::cout << " " << endl;
				std::cout << "Ok, you may skip this since you have the account!" << endl;
				std::cout << " " << endl;
				goto Username2;
			}
		}
		else
		{
			std::cin.ignore();
			std::cout << " " << endl;
			std::cout << "Welcome, new user!" << endl;
			std::cout << " " << endl;
			std::cout << "What will your new username be?" << endl;
			std::getline(std::cin, bz);
			std::cout << " " << endl;
			std::cout << "What will your new password be?" << endl;
			std::getline(std::cin, cz);
		}
	} 
while (yes != 1);
	do {
		Username2:
		Username:
		std::cin.get();
		std::cout << "Username:" << endl;
		std::getline(std::cin, h);
		if (h != "Steel2", a1 != "Steel")
		{
			{
				std::cout << "Invalid username. Try again." << endl;
				std::cout << " " << endl;
				goto Username;
			
		}
		}
		std::cout << " " << endl;
		std::cout << "Password:" << endl;
		std::getline(std::cin, g);
		if (g != "Superman", i != "Programmer")
		{
			std::cout << "Invalid password. Try again." << endl;
			std::cout << " " << endl;
		}
		else
		{
			std::cout << " " << endl;
			std::cout << "Access Granted..." << endl;
			std::cout << " " << endl;
			std::cout << "Hello \b " << h << "!" << endl;
			std::cout << "What is your first name?" << endl;
			std::getline(std::cin, a);
			std::cout << "What is your middle name?" << endl;
			std::getline(std::cin, b);
			std::cout << "What is your last name?" << endl;
			std::getline(std::cin, c);
			std::cout << "Where are you from?" << endl;
			std::getline(std::cin, d);
			std::cout << "Where are you working?" << endl;
			std::getline(std::cin, e);
			std::cout << "How old are you?" << endl;
			std::cin >> f;
			std::cout << "My full name is \b " << a << " \b " << b << " \b " << c << "." << endl;
			std::cout << "I am from \b " << d << "." << endl;
			std::cout << "My work is in \b " << e << "." << endl;
			std::cout << "I am \b " << f << " years old." << endl;
			for (z = 0; z < 5; ++z)
			{
				y += beta[z];
			}
			std::cout << "Final array result is: \b " << y << endl;
			std::cin.ignore();
			std::cout << "How old am I?" << endl;
			std::cin >> answer;
			if (answer < 32)
			{
				std::cout << "Wrong! I wish I can go back to that age. :)" << endl;
			}
			else if (answer > 32)
			{
				std::cout << "Wrong! Do I look old to you?!" << endl;
			}
			else
			{
				std::cout << "Whoa! That's unbelievable! You got it!" << endl;
			}
			std::cin.get();
			std::cout << "Press any key..." << endl;
			
		}
	} while (g != "Superman", i != "Programmer");
	std::cin.ignore();
	return 0;
}
Last edited on
if (h != "Steel2", a1 != "Steel")
Is absolute equivalent to if (a1 != "Steel") As a1 never changes, that condition is always true. Read on logical operators, it looks like you would want to use them
http://www.learncpp.com/cpp-tutorial/36-logical-operators/
It shouldn't approve if you put different words or letters for username. I am going to read logical operators.
It's almost solved. I still have problems with one thing. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

Int main()
{

string a;

std::cout << "What will your username be?" << endl;
std::getline(std::cin, a);

do
{
Username:
std::cout << "What is your username?" << endl;
std::getline(std::cin, a)
if (a != a) // I have no ideas about how to add a from std::cin, a. I only know it's possible to do that like this. std::cout << "Your new username is \b " << a << endl; That can come from std::cin, a.
{
std::cout << " " << endl;
std::cout << "Invalid username. Please try again." << endl;
goto Username;
}
Last edited on
if (a != a) // I have no ideas about how to add a from std::cin, a Elaborate what you want. Do you want to check if enterad name is not same as previous?
Any words from a should be approved. Like after you sign up. And then you log in. It should recognize the word from sign up.
@DCisthebest

I think you have put in a good effort for your code, but there are some improvements to be made.

First up, avoid using goto, use loops instead. I am not saying it should be banned, there are situations where expert might use it, but beginners should avoid it .

Also avoid global variables - they can get messy quickly and cause subtle problems. Try to keep the scope of variables as local as possible.

Next, try to use functions. Compound statements (code between braces) as found in loops, if and switch or case are good candidates for functions. Use of functions makes the code easier to understand because it provides a little bit of abstraction. If there was a function named GetUserData, and it works - then this becomes easy to understand:

1
2
3
if (LoginSuccess) {
    GetUserData();
}


The details of GetUserData appear after main(), so the reader of the code in main can think about things at a higher level, rather than be bogged down in detail.

There is a bit of a rule that functions should be no longer 40LOC - and that includes main(). Some go for even less. The point is that functions should do 1 well defined thing.

If you name your functions and variables well, then the code becomes self documenting and should almost read like sentences and paragraphs. Or if you like, it tells the story of what the code does. Names like a, b , c ,d aren't good examples of variable names in this context. Consider FirstName , MiddleName etc. Someone who knows very little about the subject of your code should be able to read the code and not be confused.

Avoid using namespace std; - you already have std:: for most of the code - just need to add it for string as well. There is lots written on the net as to why this is a good idea.

From a style POV it is good to declare 1 variable per LOC. If it is not an STL thing (like std::string say) then you should initialise the variable at the same time as declaration. Comments can be put in to describe things like expected range of valid values.

With variables that are going to have yes / no values consider using a bool type. Consider using SignUpOption rather than yes / no.

Try to avoid having magic numbers like 32 in your code. Make them const variables instead, then use the variable name from then on:

1
2
3
4
5
6
const unsigned short AgeLimit = 32;
constexpr unsigned short AgeLimit = 32; // in c++11 we can use constexpr

if (answer < AgeLimit) {
   // Do stuff
}


With your do loop on line 48, there doesn't seem to be a way for the user to exit early.

Duoas is spending a lot of effort & time writing an excellent FAQ for beginners, there is a Topic about it at the moment. Here is an excellent paper on variable naming that is linked in Duoas' article- well worth reading:

http://www.objectmentor.com/resources/articles/Naming.pdf


Any way good luck with your coding - I hope that I have helped a bit :+)
I tried GetUserData(); It doesn't work successfully. If I remove using namespace std;, I receive 95 errors.

Note:

I chose this like this before I start typing C++

New Project -> Visual C++ -> Win32 Console Application
Add new item -> C++ File (.cpp)
Last edited on
Hi,

I tried GetUserData();


So what does the code for that look like? Maybe you should post all your code so far, so I (& others) can see how you are doing. If you are going to use functions, declare them before main(), and define them after main()

If I remove using namespace std;, I receive 95 errors.


TheIdeasMan wrote:
Avoid using namespace std; - you already have std:: for most of the code - just need to add it for string as well. There is lots written on the net as to why this is a good idea.


Did you read up about this on Google?

Hope all goes well :+)
I changed a little bit expect removing using namespace std; and GetUserData();

I read some of them from google.

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
#include <iostream>
#include <string>
#include <array>
#include <stdio.h>
#include <ctime>
using namespace std;


int beta[] = { 300, 200, 50, 20, 15 };
int z, y = 0, yes, no;
int main()
{
	string a, b, c, d, e, g, h, i, result, bz, cz;
	int f, answer;
	g = "Bruce Lee";
	h = "Steel";
	yes = 1;
	no = 2;
	const unsigned short Agelimit = 32;
		std::cout << "Do you need to sign up? Please choose numbers. 1 = yes 2 = no" << endl;
		std::cin >> no;
		if (no == 2)
		{
			{
				std::cout << " " << endl;
				std::cout << "Ok, you may skip this since you have the account!" << endl;
				std::cout << " " << endl;
				goto Username2;
			}
		}
		else
		{
			std::cin.ignore();
			std::cout << " " << endl;
			std::cout << "Welcome, new user!" << endl;
			std::cout << " " << endl;
			std::cout << "What will your new username be?" << endl;
			std::getline(std::cin, bz);
			std::cout << " " << endl;
			std::cout << "What will your new password be?" << endl;
			std::getline(std::cin, cz);
		}
	do {
		Username2:
		Username:
		std::cin.get();
		std::cout << "Username:" << endl;
		std::getline(std::cin, h);
		if (h != "Steel")
		{
			{
				std::cout << "Invalid username. Try again." << endl;
				std::cout << " " << endl;
				goto Username;
			}
		}
		std::cout << " " << endl;
		std::cout << "Password:" << endl;
		std::getline(std::cin, g);
		if (g != "Superman")
		{
			std::cout << "Invalid password. Try again." << endl;
			std::cout << " " << endl;
		}
		else
		{
			std::cout << " " << endl;
			std::cout << "Access Granted..." << endl;
			std::cout << " " << endl;
			std::cout << "Hello \b " << h << "!" << endl;
			std::cout << "What is your first name?" << endl;
			std::getline(std::cin, a);
			std::cout << "What is your middle name?" << endl;
			std::getline(std::cin, b);
			std::cout << "What is your last name?" << endl;
			std::getline(std::cin, c);
			std::cout << "Where are you from?" << endl;
			std::getline(std::cin, d);
			std::cout << "Where are you working?" << endl;
			std::getline(std::cin, e);
			std::cout << "How old are you?" << endl;
			std::cin >> f;
			std::cout << "My full name is \b " << a << " \b " << b << " \b " << c << "." << endl;
			std::cout << "I am from \b " << d << "." << endl;
			std::cout << "My work is in \b " << e << "." << endl;
			std::cout << "I am \b " << f << " years old." << endl;
			for (z = 0; z < 5; ++z)
			{
				y += beta[z];
			}
			std::cout << "Final array result is: \b " << y << endl;
			std::cin.ignore();
			std::cout << "How old am I?" << endl;
			std::cin >> answer;
			if (answer < Agelimit)
			{
				std::cout << "Wrong! I wish I can go back to that age. :)" << endl;
			}
			else if (answer > Agelimit)
			{
				std::cout << "Wrong! Do I look old to you?!" << endl;
			}
			else
			{
				std::cout << "Whoa! That's unbelievable! You got it!" << endl;
			}
			std::cout << " " << endl;
			std::cin.get();
			std::cout << "Press any key..." << endl;
			
		}
	} while (g != "Superman");
	std::cin.ignore();
	return 0;
}
Last edited on
Hi,
So what did you change? It looks like your original code.

I am at work so only a brief reply 😃
Cheers
I removed do { } while (yes != 1); and added const unsigned short Agelimit = "32" and if (answer > Agelimit), and else if (answer < Agelimit) as you suggested.
Registered users can post here. Sign in or register to post.