Double IF statements - only the first one works [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Here's my problem: The program requires login info. The username verification works fine - when a bad username is given it is rejected and the user is asked to retry. However, when the password prompt appears, any given character is accepted - even just hitting the ENTER key will get you logged in. Can anyone spot my error??
int main()
{
int i;
int e;
string x;
string userName;
string userPass;
i = 1;
e = 1;
cout << "Please enter your information to login.\n";
//First 'do' is so that the line "Please enter your info..." does not get repeated
do
{
//This 'do' is to loop through the login process until successful
do
{
cout << "Username: ";
getline (cin, userName);
//Checks for a successfully input username
if (userName == "Fin")
{
cout << "Password: ";
getline (cin, userPass);
//Checks for a successfully input password
// HERE'S WHERE THE PROBLEM IS
if (userPass == "fin123");
{
cout << "Login successful.\n" << "Press ENTER to exit.";
//Waits for user to hit the ENTER key before exiting
getline (cin, x);
if (x.empty())
{
return 0;
}
}
//If the password is incorrect, start the login loop over
if (userPass != "financiero123")
{
cout << "Incorrect password. Please try again.";
//Waits for user to press a key before starting over
cin.get();
e = 0;
}
}
//If the username is incorrect, start the login loop over
if (userName != "Financiero")
{
cout << "Incorrect username. Please try again.";
//Waits for user to press a key before starting over
cin.get();
e = 0;
}
} while (e != 0);
e = 1;
} while (e != 0);
return 0;
}

if (userPass == "fin123");
{
// ...
The semicolon after the conditional closes the conditional, so what is left becomes an anonymous block. It's as though you had written this:
if (userPass == "fin123") { }
{
// ...
Therefore the block following this line is executed unconditionally.
Remove the semicolon to correct the logic error.
(Compiling with the -Wempty-body flag would have warned about this.)

Related

Why won't my "main()" function call a void function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm working on a program to aid me in world-building that randomly generates a settlement (hamlet, village, town, city) based on a nation (German, Latin, Eastern) that the user chooses. Unfortunately, my code halts right at the "main()" function as it won't call the "settlementCreation()" void function I created.
I've tried moving the function I want to call above the "main()" function, or my usual method of creating the function above, defining it's contents below, but neither of these are working. I can't figure out any other solutions with my limited experience coding C++.
Main() Function:
int main() {
char tempChoice{};
bool isMakingSettlement = true;
while (isMakingSettlement = true) {
cout << "Create a settlement? (y/n): ";
cin >> tempChoice;
cout << "\n\n";
if (tempChoice == 'y') {
settlementCreation();
} else {
isMakingSettlement = false;
}
}
return 0;
}
settlementCreation() Function:
void settlementCreation() {
int tempType{};
int tempNation{};
bool isTypeValid = false;
bool isNationValid = false;
while (isTypeValid = false) {
cout << "What type of settlement would you like to create?:";
cout << "\n 1. Hamlet";
cout << "\n 2. Village";
cout << "\n 3. Town";
cout << "\n 4. City\n";
cin >> tempType;
if (tempType >= 1 && tempType <= 4) {
isTypeValid = true;
} else {
cout << " is an invalid choice, please select a valid choice.";
}
cout << "\n\n";
}
while (isNationValid = false) {
cout << "What nation would you like your settlement to be in?: ";
cout << "\n 1. Latin";
cout << "\n 2. German";
cout << "\n 3. Eastern\n";
cin >> tempNation;
if (tempNation >= 1 && tempNation <= 3) {
isNationValid = true;
} else {
cout << " is an invalid choice, please select a valid choice.";
}
cout << "\n\n";
}
Settlement objSettlement(tempType,tempNation);
}
So the program is supposed to allow the user to choose a nation and a settlement type before redirecting to the Settlement object constructor to create the objSettlement instance of the object.
The usual outcome however, is just an infinite loop of:
"Create a settlement? (y/n): "
With no responses I've tried closing the program or going to the "settlementCreation()" function.
while (isMakingSettlement = true) {
This does not check if isMakingSettlement is true. It sets isMakingSettlement to true! This means the check in the while loop always sees true, so never stops going round.
Use while (isMakingSettlement == true).
(Or while (isMakingSettlement), or while (true == isMakingSettlement); all are fine, it's a stylistic choice, though the last would have helped you catch this bug!).
Similarly for all your other while loops.
Assuming you fix the above, your next problem will be here:
bool isTypeValid = false;
bool isNationValid = false;
while (isTypeValid == false) { // once corrected
// ... never get here!
while (isNationValid == false) { // once corrected
// ... never get here!
You always set those bools to false, so these loops are never executed.

C++ if i have a user records within a line how can find the data to login [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
The following code does not take the data from file.
It always displays You have entered wrong data!!!, and I need the code to select username and password from one line.
My record look like this: username password name id Contactnumber
if(choose ==2)//teacher
{
string line=" ";
ifstream read("teacher.txt");
cout <<" Enter Username: "; cin >> username;
cout <<" Enter Password: "; cin >> password;
bool found = false;
while (getline(read,line))
{
stringstream ss(line);
ss>>un>>pw;
if (un == username && pw == password)
{
cout<<"You have logged in as Teacher!"<<endl;
found = true;
break;
//system("pause");
}
}
if(!found)
{
cout<<"\nYou have entered wrong data!!!"<<endl;
}
read.close();
}
cin >> username;
cin >> password;
Object cin represents standard input (keyboard by default). The file "teacher.txt" is represented by object read in your code. To read from this file you should write:
read >> username;
read >> password;
By the way, read is good name for function but not for object.

C++ How check characters in real time with _getch() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i need in my loop get character in realtime and check it by conditions. If user press whatever except enter, program works fine. Can anyone help me ? thanks !
while (read != '\n')
{
cout << "Enter character:\n";
read = _getwch();
if (read == '\n') {
cout << "You pressed : ENTER\n";
}
else {
cout << "Your character is: \"" << read << "\"\n\n";
read = '\0';
}
}
include
using namespace std;
int main()
{
cout << "Press the ENTER key";
if (cin.get() == '\n')
{
cout << "Good job.\n";
}
else
{
cout << "I meant ONLY the ENTER key... Oh well.\n";
}
return 0;
}
This code will help in detecting the ENTER key when pressed.
Hope this helps you.

Why do I keep ending up on infinite loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I keep ending up on a infinite loop even on do while loop!
What am I doing wrong? I tried everything but I still cant figure it it out. any help?
here is my code :
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
//function prototypes
//prototype for IsAccessible
int IsAccessible(string username, string password);
//prototype for menu
void menu();
int main()
{
string username;
string password;
//make user to login
cout << "Enter username : ";
getline(cin, username);
cout << "\nEnter Password : ";
cin >> password;
IsAccessible(username, password);
cout << "Thank you for logging in!!";
cin.ignore();
cin.get();
return 0;
}
//function definitions
//definition for IsAccesible
int IsAccessible(string username, string password)
{
//check if user entered correct details
do
{
int x = 0;
if(password == "123" && username == "asdqw")
{
cout << "\n\nThank you for loggin in John!";
break;
}
//if user entered wrong details
else if(password != "123" && username != "asdqw")
{
cout << "\nYou have either entered a wrong password or username.\n";
cout << "Please retry.";
}
//if user exceeds limitation
if(x == 5)
{
cout << "\n\nYou have exceeded the 5 retry limitations......\n";
Sleep(4000);
cout << "Exiting program....";
Sleep(5000);
return 0;
}
}while(password != "123" && username != "asdqw");
return 0;
}
The while will keep looping until the username is not "asqdf" and the password is not "123" and the code never asks for a fresh username & password, so it will just keep looping to infinity. Also, you don't incrementx every time the loop iterates, so the 5-maximum-attempts code will never run.
Just a final tip - if your methods don't need to return, you can make the return type void. Instead of returning to exit the do-while, you could use a break statement.

C++ Check user's first name is not too short [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a function (below) that checks the user's first name for invalid characters and it works fine.
while(run)
{
size_t positionFirstName = userFirstName.find_first_of(invalidCharacter, 0, sizeof(invalidCharacter));
if (positionFirstName != string::npos)
{
cout << "Please only use letters. Please re-enter your first name." << endl;
cin >> userFirstName;
}
else
{
run = false;
}
}
I also want to check that the user's first name is not shorter than 3 characters.
I have tried a few times, and can get the program to run the first function, but if I put in another function to check name length, it seems to skip it. Any ideas?
Here's a slightly adjusted way to do it:
cout << "Please enter your first name." << endl;
while( cin >> userFirstName )
{
size_t positionFirstName = userFirstName.find_first_of(invalidCharacter, 0, sizeof(invalidCharacter));
if (positionFirstName != string::npos)
{
cout << "Please only use letters.";
}
else if( userFirstName.size() < 3 )
{
cout << "Name must be at least 3 characters long."
}
else {
break;
}
cout << " Please re-enter your first name." << endl;
}
Note that I've avoided repetition, but printing only the errors and handling the input in one place.