True/False

In this program I am asking the user to input his/her name, but, in the final few lines, I ask the user whether their full name is so and so. If it is true, type true, if not, type false. I tried to do this with a Boolean value called validation. I ask the user to input true or false. If they typed "true" it will print out their full name. If they input "false", the goto statement will take them back to the start of the program and ask them to start again. But, when I run this program, I tested true and false, but, on both occasions the the console printed out their full name even through I inputted false. Also, I don't think my goto statement is working. Please look at my code and tell me what I did wrong. Thanks for reading.


#include <iostream>
#include <string>
using namespace std;

int main()
{

string first;
string last;
bool validation;

restart:

cout <<"What is your first name?" << endl;

getline (cin,first);

cout << endl;
cout << endl;
cout << endl;

cout << first << endl;

cout << endl;
cout << endl;
cout << endl;

cout <<"What is your last name?" << endl;

getline (cin,last);

cout << endl;
cout << endl;
cout << endl;

cout <<"Is your full name" << " " << first << " " << last << "?" << endl;
cout <<"True/False" << endl;

cin >> validation;

if(validation = true)
cout <<"Your full name is" << " " << first << " " << last <<"." << endl;
else if(validation = false)
goto restart;

return 0;
}
This is the classical mistake of assignment vs comparison.

1
2
3
4
5
6
 
if(validation = true)

and 

else if (validation = false) 


In these instances, you are assigning a value to validation. Use == instead.
you also don't even need to make the comparison at all as:

if (validation)
will execute its block if validation is true otherwise it will not

also there is no need to have an else if for the false, you can just put else, because there can only be true or false

plus you can ditch the goto for a loop
Last edited on
TheGrayWolf, I tried using == too but it still does not work. And, metulburr, I want the user to input something with the cin. So, how can they imput true or false?
cin >> validation;
this is not converting your input to bool just because validation is of bool type.

And, metulburr, I want the user to input something with the cin. So, how can they imput true or false?

compare string to string
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main(){
    std::string valid;
    std::cin >> valid;
    if (valid == "true")
        std::cout << "valid is true";
    else
        std::cout << "valid is false";
}
Last edited on
I think when you read boolean inputs, you need to stream in the boolalpha manipulator, so the user can type "true" or "false" along with "1" or "0".
 
cin >> std::boolalpha >> validation;
Metulburr, I think that might work, I'll give it a try. Thanks. Also, Daleth, I should try that. Seems like the missing piece in my puzzle. Thanks guys.
interesting, did not know that

in that case forget my comparison of strings:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main(){
    bool valid;
    std::cin >> std::boolalpha >> valid;
    if (valid)
        std::cout << "valid is true";
    else
        std::cout << "valid is false";
}

Last edited on
Guys, if I might inquire. It seems as if none of you guys like to use the using namespace std statement. Is there an advantage to that?
That has been discussed many, many times. I bet if you searched that up in just this website's searchbar, you'd find dozens of topics.
the advantage is you do not end up with namespace conflicts. I havent yet had a problem in c++, as i havent done much yet in this language, but the same goes for any programming language. You try not to flood the namespace. I have however had problems in Python with it, and it is a nasty bug to find. So from my experience with it in Python, i also moved it over to c++.

So its a general overall good programming practice. If you don't end up doing it, you will after spend hours upon hours looking for a bug that if you did use it, such a bug would not exist.
Last edited on
Thanks guys, I finally did it. Thanks.
Topic archived. No new replies allowed.