C++ | For Loop Efficiency? - c++

I've been learning C++ and obviously before hitting loops, the tuition manual I've been reading provided me with this scenario:
Expand the password checking program from earlier in this chapter and make it take multiple usernames, each with their own password, and ensure that the right username is used for the right password. Provide the ability to prompt users again if the first login attempt failed. Think about how easy (or hard) it is to do this for a lot of usernames and passwords.
Without any knowledge yet of loops and whatnot that'll repeat any incorrect values prompting the user to input the correct information until they do so, I've attempted to complete the task and ended up with this code here:
#include < iostream >
using namespace std;
int main()
{
int inputCodeOne, inputCodeTwo = 0;
bool correctPassOne=false,correctPassTwo;
cout << "Please enter your first Code: ";
cin >> inputCodeOne;
if(inputCodeOne==1111||inputCodeOne==2222||inputCodeOne==3333)
{
correctPassOne = true;
}
if (correctPassOne)
{
cout << "Please enter your second Code: ";
cin >> inputCodeTwo;
if (inputCodeOne == 1111 && inputCodeTwo == 100)
{
cout << "Password Correct! Welcome back David";
return 0;
}
else if (inputCodeOne == 2222 && inputCodeTwo == 200)
{
cout << "Password Correct! Welcome back Darren";
return 0;
}
else if (inputCodeOne == 3333 && inputCodeTwo == 300)
{
cout << "Password Correct! Welcome back Jake";
return 0;
}
correctPassTwo = false;
if(!correctPassTwo)
{
cout << "Please re-enter your second Code: ";
cin >> inputCodeTwo;
if (inputCodeOne == 1111 && inputCodeTwo == 100)
{
cout << "Password Correct! Welcome back David";
return 0;
}
else if (inputCodeOne == 2222 && inputCodeTwo == 200)
{
cout << "Password Correct! Welcome back Darren";
return 0;
}
else if (inputCodeOne == 3333 && inputCodeTwo == 300)
{
cout << "Password Correct! Welcome back Jake";
return 0;
}
cout << "ACCESS DENIED";
return 0;
}
}
else
{
cout << "Please re-enter your first Code: ";
cin >> inputCodeOne;
if (inputCodeOne == 1111 && inputCodeTwo == 100)
{
cout << "Password Correct! Welcome back David";
return 0;
}
else if (inputCodeOne == 2222 && inputCodeTwo == 200)
{
cout << "Password Correct! Welcome back Darren";
return 0;
}
else if (inputCodeOne == 3333 && inputCodeTwo == 300)
{
cout << "Password Correct! Welcome back Jake";
return 0;
}
else
{
cout << "Please enter your second Code: ";
cin >> inputCodeTwo;
if (inputCodeOne == 1111 && inputCodeTwo == 100)
{
cout << "Password Correct! Welcome back David";
return 0;
}
else if (inputCodeOne == 2222 && inputCodeTwo == 200)
{
cout << "Password Correct! Welcome back Darren";
return 0;
}
else if (inputCodeOne == 3333 && inputCodeTwo == 300)
{
cout << "Password Correct! Welcome back Jake";
return 0;
}
correctPassTwo = false;
if (!correctPassTwo)
{
cout << "Please re-enter your second Code: ";
cin >> inputCodeTwo;
if (inputCodeOne == 1111 && inputCodeTwo == 100)
{
cout << "Password Correct! Welcome back David";
return 0;
}
else if (inputCodeOne == 2222 && inputCodeTwo == 200)
{
cout << "Password Correct! Welcome back Darren";
return 0;
}
else if (inputCodeOne == 3333 && inputCodeTwo == 300)
{
cout << "Password Correct! Welcome back Jake";
return 0;
}
else
{
cout << "ACCESS DENIED";
return 0;
}
}
}
}
}
Apologies for how messy the code probably is, but I wanted to know if there were any ways to make this more space efficient whilst providing the same result.

Optimization -- First Pass
Create indentifiers or named constants
This allows you to avoid duplication and making typos. Also allows for compiler to make more optimizations.
const int PASSWORD1a = 1111;
const int PASSWORD2a = 2222;
const int PASSWORD3a = 3333;
const int PASSWORD1b = 100;
const int PASSWORD2b = 200;
const int PASSWORD3b = 300;
Group passwords together.
Keeping the passwords paired together will make the process more generic.
You could use the existing std::pair or create your own:
struct Password_Entry
{
int first;
int second;
};
Next, create a table of valid password pairs:
const Password_Entry valid_passwords[] =
{
{PASSWORD1a, PASSWORD1b},
{PASSWORD2a, PASSWORD2b},
{PASSWORD3a, PASSWORD3b},
};
const size_t quantity_valid_passwords =
sizeof(valid_passwords) / sizeof(valid_passwords[0]);
Search the table for valid passwords
int inputCode1;
int inputCode2;
bool passwords_are_valid = false;
std::cout << "Enter first password: ";
std::cin >> inputCode1;
for (unsigned int index = 0; index < quantity_valid_passwords; ++index)
{
if (inputCode1 == valid_passwords[i].first)
{
std::cout << "Enter second password: ";
std::cin >> inputCode2;
if (inputCode2 == valid_passwords[i].second)
{
passwords_are_valid = true;
}
}
}
Summary
The above code is table driven. The code to search the table is generic and depends on the data in the table. The quantity of entries can be changed without having to modify the remaining code.
Pairing the first password with the second, in a structure, allows for more optimal data storage and code space.
Using named constants allows for the value to only be specified once. If you need to change the value, you only make one change. You don't make the risk of skipping past one or more when making the changes.
Optimization -- Second Pass
Person name
Printing of the person's name can be optimized by adding another field or member to the structure:
struct Password_Entry
{
int first;
int second;
char * name;
};
The table now becomes:
const Password_Entry valid_passwords[] =
{
{PASSWORD1a, PASSWORD1b, "David"},
{PASSWORD2a, PASSWORD2b, "Darren"},
{PASSWORD3a, PASSWORD3b, "Jake"},
};
const size_t quantity_valid_passwords =
sizeof(valid_passwords) / sizeof(valid_passwords[0]);
The search / validation code is changed as:
std::cout << "Enter second password: ";
std::cin >> inputCode2;
if (inputCode2 == valid_passwords[i].second)
{
passwords_are_valid = true;
std::cout << "Password Correct! Welcome Back "
<< valid_passwords[i].name
<< "!\n";
}
Optimization -- Third Pass
Consolidation of duplicate text
There is duplicate text, which means that more space can be squeezed out:
char const * const text_enter[] = "Enter ";
char const * const text_password[] = "password";
The code can be changed as:
std::cout << text_enter << "first " << text_password << ": ";
//...
std::cout << text_enter << "second " << text_password << ": ";
//...
std::cout << "Correct " << text_password << "! Welcome Back "
<< valid_passwords[index].name << "\n";
Block Writing
You may be able to squeeze some code space by block writing instead of using formatted writing. All the output is text, so there is no need to format; the text can be directly output. You'll have to compare assembly language listings of before this optimization and after to measure the space difference.
This technique may also show some speed improvements.
Use the following:
std::cout.write(text_enter, sizeof(text_enter) - 1U); // -1 so the terminating nul is not output.
std::cout.write(text_password, sizeof(text_password) - 1U);
std::cout.write(": ", 2);
Likewise, replace the other std::cout << with std::cout.write like the code above.
Optimization -- Fourth Pass
Don't use std::cout.
The std::cout may carry extra baggage with it. You can save some code space by using an alternative.
Replace std::cout with fwrite(stdout, /*...*/).
The fwrite function contains minimal code to write to the given stream. No extra code to format or convert. Plain and simple, write the data to the stream.
Some compilers may be lazy and insert a "one-size-fits-all" library rather than only the code for fwrite.
You may be able to squeeze out more code space by accessing the low level drivers of your operating system directly. Your program is small enough that it doesn't need buffering and some other overhead from the C++ and OS streams. At this point, the space savings may be negligible to the development time spent achieving this. Depends on your platform. On memory constrained systems, this may be a worthwhile endeavor. On most desktop systems, this is not worth your development time, as they have more memory than the constrained systems.

As Jesper commented, yes there are many ways. I'd encourage you to learn about loops first and then revisit this.
Loops just allow you to do the same thing again, anywhere from no times at all (loop doesn't run) to forever (infinite loop).
Anytime you find yourself typing the same thing again or copy-pasting, there is a very high chance that using a loop (or moving code into a function, a class, etc.) would be a better option.
Also, your code doesn't appear to behave as intended. If you initially type the incorrect input, correctPassOne is false, and you going into the first else and prompted to "re-enter your first code:".
The first set of if else statements will never pass. It's checking if inputCodeTwo is correct but it has never been given by the user at this point.
It seems like you recognized this and put the else statement after that which then prompts for inputCodeTwo.
The problem is now that if inputCodeOne was incorrect the second time, inputCodeTwo doesn't matter. So you'll prompt for inputCodeTwo with no chance of success.
On lines 103, 104 you type
correctPassTwo = false;
if(!correctPassTwo) { }
If 104 is ever reached, 103 will have been reached as well, so the assignment of false and the conditional are unnecessary.
Again if inputCodeOne was incorrect twice, this code block is unnecessary. You're now prompting the user to give the second code twice, with no chance of success occurring. Because inputCodeOne is still wrong.
With loops you could do something like this:
Ask for inputCodeOne as many times as you'd like, until it's correct or until the limit is reached.
If the limit is reached, print "ACCESS DENIED" and end program.
If the password is correct, you can go onto the inputCodeTwo.
Same thing, ask for inputCodeTwo as many times as you'd like.
If the two codes match, print your password correct message.
Otherwise, after a certain number of attempts, you can terminate the program.
You can do that with 25% of the initial code.

Related

C++ program stuck in an infinite loop

Please note that I am a complete beginner at C++. I'm trying to write a simple program for an ATM and I have to account for all errors. User may use only integers for input so I need to check if input value is indeed an integer, and my program (this one is shortened) works for the most part.
The problem arises when I try to input a string value instead of an integer while choosing an operation. It works with invalid value integers, but with strings it creates an infinite loop until it eventually stops (unless I add system("cls"), then it doesn't even stop), when it should output the same result as it does for invalid integers:
Invalid choice of operation.
Please select an operation:
1 - Balance inquiry
7 - Return card
Enter your choice and press return:
Here is my code:
#include <iostream>
#include <string>
using namespace std;
bool isNumber(string s) //function to determine if input value is int
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
int ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
else if (rtrn == "2" and isNumber(rtrn)) { return true; }
else {cout << "Invalid choice." << endl; ReturnCard(); };
return 0;
}
int menu() //function for operation choice and execution
{
int choice;
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == 1 and isNumber(to_string(choice))) { cout << "Your balance is $" << balance; "\n\n"; }
else if (choice == 7 and isNumber(to_string(choice))) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; menu(); }
} while (ReturnCard()==false);
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}
I've tried every possible solution I know, but nothing seems to work.
***There is a different bug, which is that when I get to the "Do you wish to continue?" part and input any invalid value and follow it up with 2 (which is supposed to end the program) after it asks again, it outputs the result for 1 (continue running - menu etc.). I have already emailed my teacher about this and this is not my main question, but I would appreciate any help.
Thank you!
There are a few things mixed up in your code. Always try to compile your code with maximum warnings turned on, e.g., for GCC add at least the -Wall flag.
Then your compiler would warn you of some of the mistakes you made.
First, it seems like you are confusing string choice and int choice. Two different variables in different scopes. The string one is unused and completely redundant. You can delete it and nothing will change.
In menu, you say cin >> choice;, where choice is of type int. The stream operator >> works like this: It will try to read as many characters as it can, such that the characters match the requested type. So this will only read ints.
Then you convert your valid int into a string and call isNumber() - which will alway return true.
So if you wish to read any line of text and handle it, you can use getline():
string inp;
std::getline(std::cin, inp);
if (!isNumber(inp)) {
std::cout << "ERROR\n";
return 1;
}
int choice = std::stoi(inp); // May throw an exception if invalid range
See stoi
Your isNumber() implementation could look like this:
#include <algorithm>
bool is_number(const string &inp) {
return std::all_of(inp.cbegin(), inp.cend(),
[](unsigned char c){ return std::isdigit(c); });
}
If you are into that functional style, like I am ;)
EDIT:
Btw., another bug which the compiler warns about: cout << "Your balance is $" << balance; "\n\n"; - the newlines are separated by ;, so it's a new statement and this does nothing. You probably wanted the << operator instead.
Recursive call bug:
In { cout << "Invalid choice of operation."; menu(); } and same for ReturnCard(), the function calls itself (recursion).
This is not at all what you want! This will start the function over, but once that call has ended, you continue where that call happened.
What you want in menu() is to start the loop over. You can do that with the continue keyword.
You want the same for ReturnCard(). But you need a loop there.
And now, that I read that code, you don't even need to convert the input to an integer. All you do is compare it. So you can simply do:
string inp;
std::getline(std::cin, inp);
if (inp == "1" || inp == "2") {
// good
} else {
// Invalid
}
Unless that is part of your task.
It is always good to save console input in a string variable instead of another
type, e.g. int or double. This avoids trouble with input errors, e.g. if
characters instead of numbers are given by the program user. Afterwards the
string variable could by analyzed for further actions.
Therefore I changed the type of choice from int to string and adopted the
downstream code to it.
Please try the following program and consider my adaptations which are
written as comments starting with tag //CKE:. Thanks.
#include <iostream>
#include <string>
using namespace std;
bool isNumber(const string& s) //function to determine if input value is int
{
for (size_t i = 0; i < s.length(); i++) //CKE: keep same variable type, e.g. unsigned
if (isdigit(s[i]) == false)
return false;
return true;
}
bool ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
if (rtrn == "2" and isNumber(rtrn)) { return true; } //CKE: remove redundant else
cout << "Invalid choice." << endl; ReturnCard(); //CKE: remove redundant else + semicolon
return false;
}
int menu() //function for operation choice and execution
{
string choice; //CKE: change variable type here from int to string
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == "1" and isNumber(choice)) { cout << "Your balance is $" << balance << "\n\n"; } //CKE: semicolon replaced by output stream operator
else if (choice == "7" and isNumber(choice)) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; } //CKE: remove recursion here as it isn't required
} while (!ReturnCard()); //CKE: negate result of ReturnCard function
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}

How to limit user input in C++ for strings & characters?

I'm trying to create a small restaurant program in which I'll be practicing everything I learned in C++ so far. However I jumped into a small issue. At the beginning of the program, I prompt the user whether they want to enter the program, or leave it by choosing Y or N. If the input is anything other than that the program will tell the user is invalid.
The issue is lets say the user input one invalid character a.
The invalid output will be displayed normally and everything seems perfect.
But if the user inputs two characters, or more, the invalid output case will be printed as many as the characters input by the user. Sample below:
Output image
#include <iostream>
int main()
{
char ContinueAnswer;
std::string Employee {"Lara"};
std::cout << "\n\t\t\t---------------------------------------"
<< "\n\t\t\t| |"
<< "\n\t\t\t| Welcome to OP |"
<< "\n\t\t\t|Home to the best fast food in Orlando|"
<< "\n\t\t\t| |"
<< "\n\t\t\t--------------------------------------|" << std::endl;
do
{
std::cout << "\n\t\t\t Would you like to enter? (Y/N)"
<< "\n\t\t\t "; std::cin >> ContinueAnswer;
if(ContinueAnswer == 'y' || ContinueAnswer == 'Y')
{
system("cls");
std::cout << "\n\t\t\t My name is " << Employee << "."
<< "\n\t\t\tI will assist you as we go through the menu." << std::endl;
}
else if(ContinueAnswer == 'n' || ContinueAnswer == 'N')
{
std::cout << "\t\t\t\tGoodbye and come again!" << std::endl;
return 0;
}
else
std::cout << "\n\t\t\t\t Invalid Response" << std::endl;
}
while(ContinueAnswer != 'y' && ContinueAnswer != 'Y')
Thank you for taking time to read and for anyone who answers :)
You could simply make the user input a string:
std::string ContinueAnswer;
and compare like this:
if(ContinueAnswer == "y" || ContinueAnswer == "Y")
which will handle multi-character inputs.
If you want to handle spaces in the input as well, change the:
std::cin >> ContinueAnswer;
to:
std::getline(std::cin, ContinueAnswer);
Before addressing your question I need to point out that you should always verify that the input was successful before doing anything with it. Processing variables which were not set due to the inout failing is a rather common source of errors. For example:
if (std::cin >> ContinueAnswer) {
// do something with successfully read data
}
else {
// deal with the input failing, e.g., bail out
}
I assume you consider everything on the same line to be invalid if nine of the expected characters was read. You could read a line into an std::string. However, that could be abused to provide an extremely long line of input which would eventually crash your program. Also, reading data into a std::string just to throw it away seems ill-advised. I’d recommend ignoring all characters up to and including a newline which could be done using (you need to include <limits> for this approach):
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);
The first argument is a special value indicating that there may be an arbitrary amount of character before the newline. In practice you could probably use a value like 1000 and it would be fine but it can be gamed. Of course, in a real application a dedicated limit may be used to prevent an adversary to keep the program busy for long. I tend to assume my programs are under attack to make sure I deal with unusual cases.
A quick refactor produces this:
#include <iostream>
#include <cstring>
#include <stdio.h>
int main()
{
char ContinueAnswer[256];
std::string Employee {"Lara"};
std::cout << "\n\t\t\t---------------------------------------"
<< "\n\t\t\t| |"
<< "\n\t\t\t| Welcome to OP |"
<< "\n\t\t\t|Home to the best fast food in Orlando|"
<< "\n\t\t\t| |"
<< "\n\t\t\t--------------------------------------|" << std::endl;
do
{
std::cout << "\n\t\t\t Would you like to enter? (Y/N)"
<< "\n\t\t\t "; std::cin.getline(ContinueAnswer,sizeof(ContinueAnswer));
if(strcmp(ContinueAnswer, "Y") == 0 || strcmp(ContinueAnswer, "y") == 0)
{
system("cls");
std::cout << "\n\t\t\t My name is " << Employee << "."
<< "\n\t\t\tI will assist you as we go through the menu." << std::endl;
}
else if(strcmp(ContinueAnswer, "N") == 0 || strcmp(ContinueAnswer, "n") == 0)
{
std::cout << "\t\t\t\tGoodbye and come again!" << std::endl;
return 0;
}
else
std::cout << "\n\t\t\t\t Invalid Response" << std::endl;
}
while(true);
}
The cin.getline will get all characters until a delimiter. Then, you can check for equivalence using strcmp and reject anything other than what you want. Lastly, it seems like you are wanting this to be in an infinite loop, so don't worry about checking the input at the end and just loop back.

Safe [Y/N]; [1/2/3/etc.] function

I tried to make a an introduction to a "game", and in its functions I made some Yes/No, 1/2/3, situations.
Im new to this however it wasn't that difficult, worked perfectly. The problem appeared when handling with invalid inputs. So this is what the code looks like by now:
#include "Introduction.h"
#include "GameConstants.h"
#include "PlayerCharacter.h"
#include <iostream>
#include <windows.h>
using namespace std;
Introduction::Introduction()
{
}
/////////Function N.1///////////
void Introduction::presentation()
{
char confirm;
string enteredName;
cout << constants.line() << "Welcome traveler! What is the name?" << endl;
getline(cin,enteredName);// Gets the WHOLE LINE as the name.
while (confirm != 'Y') //If the player doesn't confirm the name with 'Y' in will run again until it does.
{
cout << constants.xline() << "Your name is " << enteredName << " right? (Y/N)" << endl;
cin >> confirm; //The player's answer
cin.sync(); //Only takes the first character
confirm = toupper(confirm); //Turns player message into CAPS for easier detection in the "if" statements
if (confirm == 'N'){ //If not the correct name, gives another chance
cout << constants.xline() << "Please, tell me your name again..." << endl;
cin >> enteredName;
cin.sync();}
if ((confirm != 'Y')&&(confirm != 'N')){ //If an invalid input is entered, gives another chance. And insults you.
cout << constants.xline() << "Fool Go ahead, just enter your name again." << endl;
cin >> enteredName;
cin.sync();}
}
if (confirm == 'Y'){ //When the answer is yes ('Y') /* Uneeded line */
PC.setName(enteredName); //Saves the name
cout << constants.xline() << "Excellent! I have a few more questions for you " << PC.name() << "..." << endl;
}
}
//////////Function N.2///////////
void Introduction::difSelection(){
int selectedDif = 0; //Variable to store selected difficulty whitin this function.
Sleep(2500);
cout << constants.xline() << "What kind of adventure do you want to take part in?" << endl;
Sleep(2500); //Wait 2,5 s
cout << "\n1= Easy\n2= Normal\n3= Hard" << endl;
while(selectedDif != 1&&2&&3){ //Selected option must be 1/2/3 or will run again
cin >> selectedDif; //Sets the user selected difficulty
cin.sync(); //Gets only first character
if((selectedDif != 1||2||3)&&(!(selectedDif))){ //If the input isn't 1/2/3 AND is an invalid character, this will run. And it'll start again
cout << constants.xline() << "Criminal scum. Go again." << endl;
cin.clear();
cin.ignore();
}
if(selectedDif != 1&&2&&3){ //If selected option isn't 1/2/3, this will run and will loop again. However I know this conflicts with the previous statement since this will run anyways.
cout << constants.xline() << "Wrong input, please try again." << endl;
}
else if(selectedDif == 1){
constants.setDiff(1);
constants.setStatPoints(15);
} else if(selectedDif == 2){
constants.setDiff(2);
constants.setStatPoints(10);
} else if (selectedDif == 3){
constants.setDiff(3);
constants.setStatPoints(5);}
}
}
The first function works perfectly you can type "aaa" or "a a a" and will work. However I'd like to know if there's a simpler way to do it. (Understandable for a beginner, just started 3 days ago lol; if it includes some advanced or less known code prefer to stay like this by now).
Now, the second one, I really have no idea how to fix it. I need something that if the user's input was an invalid character type, throw certain message, and if it's an int type, but out of the range, another message. And of course, run again if it fails. Did a lot of search and couldn't find anything that meet this requirements.
To check if the user input is an int, you could use the good() function.
int val;
cin >> val;
if( cin.good() ) {
// user input was a valid int
} else {
// otherwise
}
As for the range check, the syntax is a bit different.
This returns true if the number is not equal to 1 nor 2 nor 3:
selectedDif != 1 && selectedDif != 2 && selectedDif != 3
Another shorter way would be to use:
selectedDif < 1 || selectedDif > 3
Another thing, in c++, there are two keywords break and continue which will allow to reduce the code in the loops.

Using strcmp() to compare two arrays of C-strings

My project is to make a bank account program where the user enters an account number and a password to do anything within the program. The account numbers and passwords used must be stored as C-strings (the string header file is not allowed). I believe that the problem I am having is with the strcmp function. Here is my function where the problem occurs.
void get_password(int num_accounts, char **acc_num, char **password)
{
char account[ACCOUNT_NUMBER];
char user_password[PASS_LENGTH];
std::cout << "\nEnter the account number: ";
// std::cin.getline(account, ACCOUNT_NUMBER);
std::cin >> account;
int i = 0;
do
{
if (strcmp(account, *(acc_num + i)) != 0)
{
i++;
}
else
break;
} while (i <= num_accounts);
if (i == num_accounts)
{
std::cout << "\nCould not find the account number you entered...\nExiting the program";
exit(1);// account number not found
}
std::cout << "\nEnter the password: ";
// std::cin.getline(user_password, PASS_LENGTH);
std::cin >> user_password;
if (strcmp(user_password, *(password + i)) != 0)
{
std::cout << "\nInvalid password...\nExiting the program";
exit(1);// incorrect password
}
else
{
std::cout << "\nAccount number: " << account
<< "\nPassword: " << user_password << "\n";
return;
}
}
acc_num and password are both arrays of C-strings. When I run/debug the program, it crashes at the first if statement. I guess my question is whether I'm using the strcmp function correctly or not, or if there is a problem with the pointers that I am using.
Your loop will run even when num_accounts is 0. Also, you're doing an out-of-bound array access by writing while (i <= num_accounts); instead of while (i < num_accounts);.
It would be better to write it like this:
while (i < num_accounts)
{
if (strcmp(account, *(acc_num + i)) == 0)
{
// match found!
break;
}
i++;
}
You're assuming there is at least one account, and you're also looping once too often. A safer way to write it would be as follows:
for (int i = 0; i < num_accounts && !strcmp(account, accnum[i]); i++)
;
or the corresponding while loop. A do/while is not appropriate here.

C++ Checking Against Particular Numbers

Hello Dear Programmers,
I've been working on a piece of code for a while, but I don't seem to figure this out, i'm trying so hard to check an input with a specific number, but so far it ends up not working and even when number 2,3, or 4 is pressed the error message pops up, and I go to my if else condition. here are the codes, number_of_bedrooms is an integer.
out << "Number Of Bedrooms: (Limited To 2,3, and 4 Only)" << endl;
if (isNumber(number_of_bedrooms) == false ) {
cout << "Please Do Enter Numbers Only" << endl;
cin.clear();
}
else if (number_of_bedrooms != '2' || number_of_bedrooms != '3' || number_of_bedrooms != '4') {
cout << "Numbers Are Only Limited To 2,3, And 4" << endl;
cin.clear();
}
and the function :
bool isNumber(int a)
{
if (std::cin >> a)
{
return true;
}
else
{
return false;
}
}
the first validation which checks for numbers works fine, but the second validation no, my guess is the system is not capturing inputted data after that boolean function. And if that's the reason, what's the solution ?!!
Change your || to &&, also you need to compare to int not char
else if (number_of_bedrooms != 2 && number_of_bedrooms != 3 && number_of_bedrooms != 4)
Note that a more general way to solve such a problem (for example if your list got much longer) would be to do something like
std::set<int> const allowableBedrooms = {2,3,4};
else if (allowableBedrooms.find(number_of_bedrooms) == allowableBedrooms.end())
{
// Warn user here
}
As your goal conditions are sequential, I'd use something like this:
else if ( number_of_bedrooms < 2 || number_of_bedrooms > 4 ) {
cout << "Numbers Are Only Limited To 2,3, And 4" << endl;
cin.clear();
}
This is very clear and easy to manage if you want to change it. If you want to enumerate everything you'll need to use && instead of || since your want it to both be not 2 and not 3 and not 4 to trigger the issue.
Another problem in your code is that you're comparing against characters by putting the numbers in single quotes. Since these are integers you should not have them in quotations.
You have to change your function to make the change out of the local function like this:
bool isNumber(int * a)
{
if (std::cin >> * a)
{
return true;
}
else
{
return false;
}
}
And then call the function with the address of number_of_bedrooms like this:
out << "Number Of Bedrooms: (Limited To 2,3, and 4 Only)" << endl;
if (isNumber(&number_of_bedrooms) == false ) {
cout << "Please Do Enter Numbers Only" << endl;
cin.clear();
}
else if (number_of_bedrooms < 2 || number_of_bedrooms > 4) {
cout << "Numbers Are Only Limited To 2,3, And 4" << endl;
cin.clear();
}
Check the code above becouse i took off the '' that means you are comparing number_of_bedrooms (int) with '2' (char) so it will be always true.
The condition i wrote would be better then becouse you are considering an interval of numbers, if you are considering the specific numbers you can leave your condition but you should change the logical operator in && and chain with the other != conditions