Correction on my C++ program

Hello everyone, I am trying to write a simple practice C++ program in which the user types a number greater than 50. I'm trying to use bool type often in preparation for my college computer science course. I ran this program, and there are no errors. But when I run and type a number greater than 50, the function returns false, and the program terminates from there. Anything I did wrong?

#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
using namespace std;

bool trueFalse (int l);

int main()
{
int k;

cout << "Enter an integer greater than 50\n";
cin >> k;
bool i;
i == trueFalse(k);

if (i == false) {
for (int j = 0; j >= 0; j++) {
cout << "You are wrong. Press Q to continue.\n";
exit(EXIT_FAILURE);
}
}

if (i == true) {
string name;
cout << "Enter a name that starts with J and ends with n: ";
cin >> name;
if (name == "John") {
cout << "Welcome aboard, " << name << ". ";
system("pause");
}
if (name != "John") {
i == false;
}
}
return 0;
}

bool trueFalse (int l) {
if (l >= 50) {
return true;
}
else if (l < 50) {
return false;
}
}
Hey and welcome, please edit your post and use code tags - http://www.cplusplus.com/articles/jEywvCM9/

i == trueFalse(k);
should be: i = trueFalse(k); // note = and not ==
Last edited on
Be careful of
assignment --> =
and
compare --> ==
Yes, it looks like what you're doing is just comparing:

bool i;

the function:

bool trueFalse(int l)

When what you really want is i equal to the result of the function trueFalse with the parameter l:

bool i = trueFalse(int l)

What this is saying is, "Is bool i equal to true or false? Doesn't really matter past this line..." and compares what existed in i previously, and it seems like whatever i had in the address before was 0 (or it's doing some strange 0 default initialization to 0), since it isn't initialized by you, and any nonzero number in a bool initialization evaluates to true.

Also, for future reference, you should probably avoid whatever you used as the parameter for trueFalse, since it doesn't matter now, but variables with i, I, l, L get a bit confusing.
Topic archived. No new replies allowed.