#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
usingnamespace std;
int main()
{
string a, b, c, d, e, g, h;
int f, answer;
g = "Username";
h = "Password";
do {
std::cout << "Username:" << endl;
std::getline(std::cin, h);
if (h == "Username")
{
std::cout << "Password:" << endl;
std::cin >> g;
}
if (g != "Password")
{
std::cout << "Invalid Password. Try again." << endl;
}
else
{
std::cout << "Invalid Username. Try again." << endl;
exit(0);
}
while (g != "Password");
std::cout << "Hello there!" << 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;
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;
}
elseif (answer > 32)
{
std::cout << "Wrong! Do I look old to you?!" << endl;
}
else
{
std::cout << "Whoa! That's unbelievable! You got it!" << endl;
}
cin.get();
system("Pause");
return 0;
}
I actually solved that. A problem is that it won't go back to username if username is wrong. It keeps moving on to password instead of going back to username.
#include <iostream>
#include <string>
#include <stdio.h>
usingnamespace std;
int main()
{
string a, b, c, d, e, g, h;
int f, answer;
g = "Password";
h = "Username";
do {
std::cout << "Username:" << endl;
std::getline(std::cin, h);
if (h != "Username")
{
{
std::cout << "Invalid username. Try again." << endl;
}
}
std::cout << "Password:" << endl;
std::getline(std::cin, g);
if (g != "Password")
{
std::cout << "Invalid password. Try again." << endl;
}
else
{
std::cout << "Access Granted..." << endl;
std::cout << "Hello there!" << 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;
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;
}
elseif (answer > 32)
{
std::cout << "Wrong! Do I look old to you?!" << endl;
}
else
{
std::cout << "Whoa! That's unbelievable! You got it!" << endl;
}
cin.get();
}
}
while (g != "Password");
system("Pause");
return 0;
}
#include <iostream>
#include <string>
#include <stdio.h>
usingnamespace std;
int main()
{
string a, b, c, d, e, g, h;
int f, answer;
g = "Password";
h = "Username";
do
{
std::cout << "Username:" << endl;
std::getline(std::cin, h);
if (h != "Username")
{
{
std::cout << "Invalid username. Try again." << endl;
}
}
std::cout << "Password:" << endl;
std::getline(std::cin, g);
if (g != "Password")
{
std::cout << "Invalid password. Try again." << endl;
}
else
{
std::cout << "Access Granted..." << endl;
std::cout << "Hello there!" << 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;
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;
}
elseif (answer > 32)
{
std::cout << "Wrong! Do I look old to you?!" << endl;
}
else
{
std::cout << "Whoa! That's unbelievable! You got it!" << endl;
}
cin.get();
}
} while (g != "Password");
system("Pause");
return 0;
}
A problem is that it won't go back to username if username is wrong. It keeps moving on to password instead of going back to username.
There are a couple of solutions of this. One would be to use another loop, the other would be to use the continue statement when the username is "wrong".
do
{
Username: //I added this one.
std::cout << "Username:" << endl;
std::getline(std::cin, h);
if (h != "Username")
{
{
std::cout << "Invalid username. Try again." << endl;
goto Username; //I added this one too.
}
}
But I never thought about the best message you wrote. I tested the line you put before you said so. It caused a problem with putting certain username. For example:
Username: userna
It will say Invalid username. Try again.
Please re-enter your username:
Username: username
It would say Please re-enter your username again unless I add Username: and goto Username;
It solves the problem.
But I was thinking about counting 3 attempts or whatever it is before it uses break; But I am not sure about counting down.
Outside the do loop otherwise each time, the command will reinitilize the counter. So each time count =1 and therefore it will ask you for username over and over.
And maybe a dowhile( count < = 3) is better than " if "