Why is my function not being called? This little program I created just for my own benefits to understand functions. What I've just learn today. Do not give me the solution right away, just give me a hint or a clue what I'm doing wrong. That way I can think it out and see how to solve it. Bare with me though I'm still new to C++.
I've tried to do this, thinking if that if the while is not the input function, it would give the following option and run inside the in loop multiple times until the person types in the correct password, but that did not work.
1 2 3 4 5 6 7
while (!input()){
cout << "Access denied, please try again" << endl;
}
cout << "Access accepted, you have enter the system." << endl;
The input variable you check in the while-loop is not a global variable, meaning it can only be used within the function it was defined in. You need to check string inputs value inside your function.
Furthermore, I recommend naming your function and string differently. Name the function something like getInput();
EDIT:
Even more, I recommend making your function return a boolean, after checking the password return true/false accordingly, the in main process the return value with an if-statement.
line 11: input is a local variable. It goes out of scope when input() exits. BTW, it never a good idea to give a function and a variable the same name.
Line 19: main knows nothing about the local variable (input) at line 8. You're testing the address of the function input(), which is the only symbol named input the compiler knows about at this point.
This program should not even compile since password is not defined at line 19.
I disagree with what R23MJ said about using an if-statement instead of a loop. A loop makes perfectly sense because you want to repeatedly ask the user to try again until the correct password is entered.
#include<iostream>
usingnamespace std;
void getInput (){
string password = "stay strong";
string input;
cout << "Enter the password to gain access to this system." << endl;
getline(cin,input);
}
int main(){
getInput();
bool input = true;
if (getInput() == true ) /* The mistake occurs here. Still trying to figure it out. I'm only confuse on the part after the main function calls the getInput () after setting the bool input to true, shouldn't the if statement work? */
cout << "Access accepted, you have enter the system." << endl;
else
cout << "Access denied!" << endl;
return 0;
}
I did not include the blocks to the if statement or else statement because I'm only outputting one thing. Right? I would only use the blocks ({} ) to the if statement if I was outputting multiple things to the user.
I've just read your post above R23MJ and now I'm confused lol. So I should use the while loop then. I would still like to know both ways for near future. If I decided not to let the user re attempt to type in the password if it was incorrect in this case it would be if and else statement. I will re attempt to do very first code I have display on this thread.
You need to set getInput() as a bool function, not a void and you need to get it to return a value if you are going to compare it to something. Also, I don't think people should ever omit the curly braces as you never know if you are going to add stuff to the if block and it makes it more readable anyway by separating the blocks.
Okay I only use void because that's what the instructor was using at the time being, I'm going to read the tutorials for function on this forum and figure how just to do that to set bool as a function, because I'm having some problems with that right now. I will post the code incase you guys would want to figure it out to see what I'm doing wrong, but again do not solve the solution for me as I am trying to learn from my own mistakes.
#include<iostream>
usingnamespace std;
bool getInput (){
string password = "stay strong";
string input;
cout << "Enter the password to gain access to this system." << endl;
getline(cin,input);
}
int main(){
getInput();
while (false){ // I just set this bc if I set true then it will be infinite, but false then anything the user type will be true. Which is not the case lol.
cout << "Access denied, please try again" << endl;
}
cout << "Access accepted, you have enter the system." << endl;
return 0;
}
Still having trouble, here is my new code. For some reason it's repeating the same cout statement inside the bool function when the user types in the incorrect input. What am I doing wrong? Please help.
I thought I did I initialized cInput to a bool and I try setting a value of cInput whether it's true or false but that complicated everything even more. So I left that part out.
#include <iostream>
#include <string>
usingnamespace std;
bool getInput()
{
// ok so you set password to this,
// we then need to check against
// this information with what the
// user has entered.. and since
// you have declared the function
// to return a boolean then we need
// to return true if the password
// was correct or false of it wasnt.
string password = "stay strong";
string input;
// bool cInput; // <-- dont need this
cout << "Enter the password to gain access to this system." << endl;
getline(cin, input);
// simple if statement
if (input == password)
returntrue; // return true, we were a good password.
// if we get here password was wrong.
returnfalse;
}
int main()
{
// remember your function returns a boolean, so we
// can test this in a IF statement like:
if (getInput())
cout << "Well done, password is correct..";
else
cout << "Wrong password!";
return 0;
}
Okay now I see why we didn't need bool cInput because we already declare getInput as a bool function so all we needed to do was write the if statement like you did and give the two options of a boolean true or false, that make sense thank you for that.