Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-27-2005, 02:01 PM
|
#1
|
|
Member
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 199
Rep:
|
if else statements
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
|
|
|
|
09-27-2005, 02:05 PM
|
#2
|
|
LQ Newbie
Registered: Sep 2005
Distribution: Arch
Posts: 19
Rep:
|
I don't understand...what's the problem that you want us to find?
|
|
|
|
09-27-2005, 02:17 PM
|
#3
|
|
Member
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 199
Original Poster
Rep:
|
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. 
|
|
|
|
09-27-2005, 02:40 PM
|
#4
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,466
Rep:
|
void main() <---- no no
int main()
should always return something back to the os
this is not whats causing the error tho but just something to keep in mind.
|
|
|
|
09-27-2005, 02:46 PM
|
#5
|
|
Member
Registered: Jul 2004
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").
|
|
|
|
09-27-2005, 02:54 PM
|
#6
|
|
Member
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 199
Original Poster
Rep:
|
Quote:
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.
|
|
|
|
09-27-2005, 02:59 PM
|
#7
|
|
Member
Registered: Jul 2004
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
|
|
|
|
09-27-2005, 02:59 PM
|
#8
|
|
Member
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 199
Original Poster
Rep:
|
Got it. Never mind. Thanks for the help you provided. 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|