Having trouble with my function call

closed account (yR9wb7Xj)
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++.
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
 
#include<iostream>
using namespace std;

void input (){

	 string password = "stay strong";
	 string input;

	 cout << "Enter the password to gain access to this system." << endl;
	 getline(cin,input);

}

int main(){

	input();

	 while (input!= password){
		 cout << "Access denied, please try again" << endl;

	 }
	 cout << "Access accepted, you have enter the system." << endl;










	return 0;
}
Last edited on
closed account (yR9wb7Xj)
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.

1
2
3
4
5
6
7
8
if (getInput() == true)
{
    //Password Correct.
}
else
{
    //Wrong password.
}
Last edited on
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.
closed account (yR9wb7Xj)
Okay I will try that, so you are saying for this specific program while loop should not be used or you would not recommend it?
Message deleted.
Last edited on
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.
Ahh, I didn't read what the code did thoroughly, the while-loop would be better in that context. My mistake for not reading the code.
closed account (yR9wb7Xj)
What does this mean "invalid operands to binary expression (void & int) "
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
	

#include<iostream>
using namespace 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;
}



		

Last edited on
closed account (yR9wb7Xj)
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.
Last edited on
closed account (yR9wb7Xj)
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.
closed account (yR9wb7Xj)
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.
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
#include<iostream>
using namespace 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;
}
closed account (yR9wb7Xj)
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.
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
#include<iostream>
using namespace std;

bool getInput (){

	 string password = "stay strong";
	 string input;
	 bool cInput; // cInput = Correct input


	 cout << "Enter the password to gain access to this system." << endl;
	 getline(cin,input);
return cInput;
}

int main(){

	getInput();
	bool cInput;
	 while (getInput() !=cInput){
		 cout << "Access denied, please try again" << endl;

	 }
	 cout << "Access accepted, you have enter the system." << endl;










	return 0;
}


EDIT : This is not working as well, I've tried so many different ways, I just need to know what I am doing wrong.
1
2
3
4
5
6
7
8
9
int main(){

	bool cInput = getInput();
	 while (cInput != getInput ()){
		 cout << "Access denied, please try again" << endl;

	 }
	 cout << "Access accepted, you have enter the system." << endl;
Last edited on
You're not initialising or setting the value of cInput in getInput().
closed account (yR9wb7Xj)
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.
closed account (yR9wb7Xj)
At this point I am lost and hopeless right now. Any suggestion will be truly grateful.

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

#include <iostream>
#include <string>
using namespace 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)
		return true;	// return true, we were a good password.

	// if we get here password was wrong.
	return false;
}

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;
}
closed account (yR9wb7Xj)
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.
Topic archived. No new replies allowed.