Function being called without declaration

New to c++ and tried writing a login program. When running, you need to answer login or register to choose to make or to login to an account. Though for some reason when either typing login or register it runs the login function( logFunction() ). Plus, when typing in a command that isn’t login or register for the second time , it will say wrong command and then it will run logFunction. Sry if this is a bad explanation. I would love to understand what went wrong and how to fix it.

Thanks.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <iostream>
#include <vector>

int userPassword;

std::string text;

std::string text1;

int password = 1234;

int attepmts = 1;

bool in = false;

bool logFunction() {
  
  std::cout << "Please type in your password:""\n";
  std::cin >> userPassword;
  
  while(userPassword != password && attepmts <= 2) {
   std::cout << "Please try again\n";
   std::cin >> userPassword;
   attepmts++;
   
  } 
  
  if(userPassword == password && attepmts <= 3) {
      std::cout << "Password Correct\n";
      return true;
      }
  else {
      std::cout << "Only three attemps allowed , try again later\n";
      return false;
  }
  
} 

bool regFunction() {
  std::cout << "registered"; //isn’t finished
  return true;
}

bool userCheckFunction() {
  return false;              //isn’t finished
} 


int main() {
  
  std::cin >> text1;

  if (text1 == "login"){
    //why is this blank but triggers the logFunction()
  
  }
  else if ( text1 == "register") {
    //this also triggers logFunction()
  }
  else {
    std::cout << "That command isnt avalible, please try another""\n";
    std::cin >> text1;
    //and after it goes through one wrong command it triggers logFunction()
  }

  …

  
 if(logFunction() && regFunction()) {
    …
  }
}
Your line 69, which calls at least logFunction(), is always executed.

Perhaps you want
1
2
3
4
5
6
7
8
9
10
11
12
if (text1 == "login")
{
    logFunction();
}
else if (text1 == "register")
{
    regFunction();
}
else
{
    // wrong command ...
}


PS: Functions don't need to have the word "function" in their name. We as programmers already know that they are functions.
Last edited on
Thank you for your reply, though when I declare logFunction inside the if statement it runs logFunction twice.

when I declare logFunction inside the if statement it runs logFunction twice.

Function declaration does not call the function.
Please show the code so that we see what you mean.
Sry, don’t mean declared just mean called. I understand declaring a function is defining the name and type plus perimeters in the header of the code file. I followed the advice “Ganado” gave me. And saw that the function would run twice in the if statement if called.

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
46
47
48
49
50
 bool logFunction() {
  
  std::cout << "Please type in your password:""\n";
  std::cin >> userPassword;
  
  while(userPassword != password && attepmts <= 2) {
   std::cout << "Please try again\n";
   std::cin >> userPassword;
   attepmts++;
   
  } 
  
  if(userPassword == password && attepmts <= 3) {
      std::cout << "Password Correct\n";
      return true;
      }
  else {
      std::cout << "Only three attemps allowed , try again later\n";
      return false;
  }
  
} 

bool regFunction() {
  std::cout << "registered";
  return true;
}

bool userCheckFunction() {
  return false;
} 


int main() {
  
  std::cin >> text1;

  if (text1 == "login"){
    //why is this blank but triggers the logFunction()
    logFunction();
  }
  else if ( text1 == "register") {
    //this also triggers logFunction()
    regFunction();
  }
  else {
    std::cout << "That command isnt avalible, please try another""\n";
    std::cin >> text1;
    //and after it goes through one wrong command it triggers logFunction()
  }
Last edited on
1
2
    //why is this blank but triggers the logFunction()
    logFunction();
¿why does calling the function does call the function? mysterious.

show your actual code, not extracts that you think are relevant.
That comment was there from before when the if statement was blank and still calling logFunction(), here is all the code.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 #include <iostream>
#include <vector>

int userPassword;

std::string text;

std::string text1;

int password = 1234;

int attepmts = 1;

bool in = false;

bool logFunction() {
  
  std::cout << "Please type in your password:""\n";
  std::cin >> userPassword;
  
  while(userPassword != password && attepmts <= 2) {
   std::cout << "Please try again\n";
   std::cin >> userPassword;
   attepmts++;
   
  } 
  
  if(userPassword == password && attepmts <= 3) {
      std::cout << "Password Correct\n";
      return true;
      }
  else {
      std::cout << "Only three attemps allowed , try again later\n";
      return false;
  }
  
} 

bool regFunction() {
  std::cout << "registered";
  return true;
}

bool userCheckFunction() {
  return false;
} 


int main() {
  
  std::cin >> text1;

  if (text1 == "login"){
    //this triggers the logFunction() twice
    logFunction();
  }
  else if ( text1 == "register") {
    //this also triggers logFunction()
    regFunction();
  }
  else {
    std::cout << "That command isnt avalible, please try another""\n";
    std::cin >> text1;
    //and after it goes through one wrong command it triggers logFunction()
  }

  std::vector<std::string> opp(4);

  opp[0] = "help";
  opp[1] = "bankb";
  opp[2] = "profile";
  opp[3] = "logout";  

  
 if(logFunction() && regFunction()) {

  std::cout << "Welcome to The bank, type help for navigation help\n";
  std::cin >> text;

  
    if (text == opp[0]) {
    std::cout << " Type:" "\n" "'bankb' for bank balance" "\n" "'profile' to see your username and password" "\n" "'logout' to logout""\n";
    std::cin >> text;
    }
    else if (text == opp[1]) {
    ...
    }
    else if (text == opp[2]) {
    ...
    }
    else if (text == opp[3]) {
    std::cout << "logged out";
    }
    else {
    std::cout << "that command isnt avalible";
    }
    
   }   
 
}
Comments on lines 54, 58, and 64 are false.
Line 55 calls logFunction(), but only if condition on line 53 is true. Only once.

Line 75 always calls logFunction().
Why would line 75 call logFunction, because isn’t it a if statement that would run if logFunction returns true? Sorry if I don’t understand correctly.
Based upon the provided code, perhaps:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>

int userPassword;
std::string text;
std::string text1;
int password = 1234;
int attepmts = 1;
bool in = false;

bool logFunction() {
	std::cout << "Please type in your password: ";
	std::cin >> userPassword;

	while (userPassword != password && attepmts <= 2) {
		std::cout << "Please try again\n";
		std::cin >> userPassword;
		attepmts++;
	}

	if (userPassword == password) {
		std::cout << "Password Correct\n";
		return true;
	}

	std::cout << "Only three attempts allowed , try again later\n";
	return false;
}

bool regFunction() {
	std::cout << "registered";
	return true;
}

bool userCheckFunction() {
	return false;
}

int main() {
	bool loggedin {};

	std::cin >> text1;

	if (text1 == "login")
		loggedin = logFunction();
	else if (text1 == "register")
		loggedin = regFunction();
	else {
		std::cout << "That command isn't available, please try another""\n";
		std::cin >> text1;
	}

	const std::string opp[4] { "help", "bankb", "profile", "logout" };

	if (loggedin) {
		std::cout << "\nWelcome to The bank, type help for navigation help\n";
		std::cin >> text;

		if (text == opp[0]) {
			std::cout << " Type:" "\n" "'bankb' for bank balance" "\n" "'profile' to see your username and password" "\n" "'logout' to logout""\n";
			std::cin >> text;
		} else if (text == opp[1]) {
			//...
		} else if (text == opp[2]) {
			//	...
		} else if (text == opp[3]) {
			std::cout << "logged out";
		} else {
			std::cout << "that command isn't available\n";
		}
	}
}


Note that usage of global variables is not considered good practice. They should either be locally defined near to where they are first used, or as function parameter. There' also the issue of re-entering a command both after one has completed or if an invalid one is entered.

Last edited on
isn’t it a if statement that would run if logFunction returns true?

If statement has:
1
2
3
if ( condition ) {
  body
}

The condition is an expression that has to be evaluated first.
IF the result of condition is true THEN the body is executed.

In other words, in
1
2
3
if ( logFunction() ) {
  body
}

The body is run only if the logFunction() returns true.
The only way to know what logFunction() returns is to call it.
The if calls logFunction(). There are no ifs about that.
Last edited on
This worked, thank you. Could you or someone explain why I have to make another bool. I just want to understand for future projects. Thanks again. (Edit: sry didn’t see the post above, makes sense now, thanks)
Last edited on
Your functions called logFunction() and regFunction() are of type bool. They return a bool value, which is stored in the bool variable loggedin. This variable is used on line 54.
Topic archived. No new replies allowed.