ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I know this seems like an easy problem to figure out, however, i am unable to see the problem. If one of you could so kindly point out where i am going wrong in my ways.
Code:
#include <iostream>
#include <conio>
using namespace std;
void main()
{
cout<<"Control structures and the beach"<<endl;
int temp, temp2;
string day;
bool sunny;
cout<<"Please enter the same twice temperature: ";
cin>>temp;
if(temp >= 75){
cout<<"Good temperature"<<endl;
}
else{
cout<<"Kind of chilly out there!"<<endl;
}
if(temp >= 90){
cout<<"Its a little hot out there"<<endl;
}
else if(temp >= 80){
cout<<"Great weather, warm but not too warm"<<endl;
}
else if(temp < 70){
cout<<"STAY HOME!"<<endl;
}
cout<<"If its sunny type true else type false: ";
cin>>sunny;
if(sunny==true && temp >= 90){
cout<<"Go to the park"<<endl;
}
else if(sunny==true && temp >= 80){
cout<<"Go to the beach"<<endl;
}
else{ if(sunny==false || temp < 70)
cout<<"Go to the mall"<<endl;
}
cout<<"Press any key to conitinue"<<endl;
getch();
}//end of main
Sorry, i guess i should have stated that. When it executes, the first part that is above "cout<<"If its sunny type true else type false: ";" functions as you would think. However, below that doesn't work correctly. When it runs, no matter what data i input, it always tells me to go to the mall, and i don't want to go to the mall all the time.
Distribution: LFS, I felt the itch and scratched it
Posts: 227
Rep:
Quote:
Originally posted by 0.o Sorry, i guess i should have stated that. When it executes, the first part that is above "cout<<"If its sunny type true else type false: ";" functions as you would think. However, below that doesn't work correctly. When it runs, no matter what data i input, it always tells me to go to the mall, and i don't want to go to the mall all the time.
Are you actually typing in "true" and "false" for that? If so, cin to a boolean doesn't work that way. Try typing 0 and 1 and that will input correctly to a boolean. Otherwise change it to a string and use if (str == "true").
Originally posted by kjordan Are you actually typing in "true" and "false" for that? If so, cin to a boolean doesn't work that way. Try typing 0 and 1 and that will input correctly to a boolean. Otherwise change it to a string and use if (str == "true").
Yes. I was typing "true" and "false" as input; although, I get the same output if i use 1 or 0.
Distribution: LFS, I felt the itch and scratched it
Posts: 227
Rep:
Quote:
Originally posted by 0.o Yes. I was typing "true" and "false" as input; although, I get the same output if i use 1 or 0.
Interesting, because I get no output unless I use 1 or 0. What are you typing in? Here's the output of what I get if I type the values that would match those if else statements:
Control structures and the beach
Please enter the same twice temperature: 80
Good temperature
Great weather, warm but not too warm
If its sunny type true else type false: 1
Go to the beach
Control structures and the beach
Please enter the same twice temperature: 90
Good temperature
Its a little hot out there
If its sunny type true else type false: 1
Go to the park
Control structures and the beach
Please enter the same twice temperature: 90
Good temperature
Its a little hot out there
If its sunny type true else type false: 0
Go to the mall
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.