I am working on a program that uses conditionals and user input in the form of strings. The problem is that my program doesn't appear to be comparing the strings properly.
Here is my code
#include <iostream>
#include <cstdlib>
#include <string>
using std::string;
usingnamespace std;
int main(){
//Define Local Variables
string driver_name= "NULL";
string run_program= "NULL";
int light_color= 0;
string green_option= "NULL";
string red_option= "NULL";
string amber_option= "NULL";
//begin data collection
cout<< "Hi, could you please tell me your first name? :";
cin>> driver_name;
cout<< "Okay " <<driver_name <<", this program will tell you how good of a driver you are." <<endl;
if (run_program!="Y"&&"N"&&"y"&&"n"){ //check if the user is entering a valid option
cout<<"Would you like to run this program and learn" <<endl;
cout<< "the horrible truth about your driving habbits (Y or N): ";
cin >> run_program;
}
elseif (run_program=="Y"&&"y"){
//main program here....
cout<< "you said yes!";
}
else{
cout<< "you said no!";
}
}
if (run_program!="Y"&&"N"&&"y"&&"n"){ //check if the user is entering a valid option
Instead of that, consider this: if(run_program!="Y" && run_program!="N" && run_program!="y" && run_program!="n")
np np
same thing down there: elseif (run_program=="Y"&&"y"){
instead of that,
do this: elseif (run_program=="Y"&&run_program=="y"){
EDIT: thats the right way to do it, but program still doesnt make any sense.
ok. I understand that I had it wrong at first. I guess there is still something fundamentally wrong with the code though. I am simply trying to check the user's input and jump to the apropriate part of the code. For example is the user enters "Y" or "y" as an option, It will jump to the code where cout<< "you said yes!"; and if the user types "N" or "n", then jump to the cout<< "you said no!"; then if any other input is used, end the program.
Is is possible to analyze strings as such? or do I need to somehow convert the string to some kind of integer for the purpose of conditionals?
I am a complete beginner as you SHOULD be able to tell.
You need a logical OR, not an AND in the second and third conditional - they only occur if the string is for instance, "Y" AND "y" - something that obviously never happens.
thank you very much hanst99, that is exactly the help I was looking for. No offence Krofna, but just saying "your program makes no sense" with out any comments or theories on why, is not very constructive. Props to hanst99, something sooo simple. /facepalm
edit:
unfortunately, my program is still skipping to the end regardless of the input.
here is how the code looks now:
#include <iostream>
#include <cstdlib>
#include <string>
using std::string;
usingnamespace std;
int main(){
//Define Local Variables
string driver_name= "NULL";
string run_program= "NULL";
int light_color= 0;
string green_option= "NULL";
string red_option= "NULL";
string amber_option= "NULL";
//begin data collection
cout<< "Hi, could you please tell me your first name? :";
cin>> driver_name;
cout<< "Okay " <<driver_name <<", this program will tell you how good of a driver you are." <<endl;
if (run_program != "Y" || run_program != "N" || run_program != "y" || run_program != "n"){ //check if the user is entering a valid option
cout<<"Would you like to run this program and learn" <<endl;
cout<< "the horrible truth about your driving habbits (Y or N): ";
cin >> run_program;
}
elseif (run_program=="Y" || run_program=="y"){
//main program here....
cout<< "you said yes!";
}
else{
cout<< "you said no!";
}
cout<< "end of program!";
return 0;
}
#include <iostream>
#include <cstdlib>
#include <string>
using std::string;
usingnamespace std;
int main(){
//Define Local Variables
string driver_name= "NULL";
string run_program= "NULL";
int light_color= 0;
string green_option= "NULL";
string red_option= "NULL";
string amber_option= "NULL";
//begin data collection
cout<< "Hi, could you please tell me your first name? :";
cin>> driver_name;
cout<< "Okay " <<driver_name <<", this program will tell you how good of a driver you are." <<endl;
cout<<"Would you like to run this program and learn" <<endl;
cout<< "the horrible truth about your driving habbits (Y or N): ";
cin >> run_program;
if (run_program !="Y" || run_program !="N" || run_program !="y" || run_program !="n"){ //check if the user is entering a valid option
cout<< "Please enter a valid input!" <<endl;
cout<< "Would you like to run this program and learn" <<endl;
cout<< "the horrible truth about your driving habbits (Y or N): ";
cin >> run_program;
}
elseif (run_program=="Y" || run_program=="y"){
//main program here....
cout<< "you said yes!";
}
else{
cout<< "you said no!";
}
cout<< "end of program!" <<endl;
cout<< "RESULT OF RUN_PROGRAM= " <<run_program;
return 0;
}
But unfortunately the program is still not working as intended. Here is sample output for the valid option "Y":
Hi, could you please tell me your first name? :...
Okay ..., this program will tell you how good of a driver you are.
Would you like to run this program and learn
the horrible truth about your driving habbits (Y or N): Y
Please enter a valid input!
Would you like to run this program and learn
the horrible truth about your driving habbits (Y or N): Y
end of program!
RESULT OF RUN_PROGRAM= Y
Process returned 0 (0x0) execution time : 8.445 s
Press any key to continue.
as you can see, it is still asking me the question twice (as if the IF statement didn't even get evaluated). Also as you can see, right at the end, The variable is verified as being "Y" still.
I am kinda lost right now. It appears to me that everything SHOULD work. Of course I am a novice though, so meh.... help please?
ok, well.... Did a little bit of research. I found that my previous logic was wrong because I was attempting to evaluate strings which in c++ evaluate as bool. eg. a string that has a value is 1(true). So I was effectively saying "if (something !=true ||something!=true). So what I did, was clean up my code and logic in a way that works. I took a step down, not running before I can walk. For reference and to help people in the future. He is my solution (and code that does as intended):
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main(){
//Define Local Variables
string driver_name= "NULL";
string run_program= "NULL";
int light_color= 0;
string green_option= "NULL";
string red_option= "NULL";
string amber_option= "NULL";
//begin data collection
cout<< "Hi, could you please tell me your first name? :";
cin>> driver_name;
cout<< "Okay " <<driver_name <<", this program will tell you how good of a driver you are." <<endl;
cout<<"Would you like to run this program and learn" <<endl;
cout<< "the horrible truth about your driving habbits? (Y or N): ";
cin >> run_program;
if (run_program == "Y"){ //
//main program here...
cout<<"You said yes, great. Lets begin!" <<endl;
//...........
}
elseif (run_program == "N"){
//I guess they don't want to run the program?
cout<< "You said no!" <<endl;
cout<< "Fair enough, maybe another day." <<endl;
}
else{
//Invalid input.
//User will need to re-run the program and use a valid input
cout<< "Invalid input!," <<endl;
cout<< "Please re-run the program and use upper case Y or N" <<endl;
}
return 0;
}
//end of program.
Thank you for your help anyways guys. Problem solved via some research and initiative on my part :D