LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 09-27-2005, 02:01 PM   #1
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
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
 
Old 09-27-2005, 02:05 PM   #2
Javasnob
LQ Newbie
 
Registered: Sep 2005
Distribution: Arch
Posts: 19

Rep: Reputation: 0
I don't understand...what's the problem that you want us to find?
 
Old 09-27-2005, 02:17 PM   #3
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
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.
 
Old 09-27-2005, 02:40 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Rep: Reputation: 66
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.
 
Old 09-27-2005, 02:46 PM   #5
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
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").
 
Old 09-27-2005, 02:54 PM   #6
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
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.
 
Old 09-27-2005, 02:59 PM   #7
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
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
 
Old 09-27-2005, 02:59 PM   #8
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
Got it. Never mind. Thanks for the help you provided.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
Why my else statements always run? lectraplayer Programming 9 04-02-2004 11:38 AM
if statements thesnaggle Linux - Software 1 02-16-2004 09:52 AM
How do I use X =< Y w/ IF statements? dolvmin Linux - Software 11 09-24-2003 10:39 PM
if then statements vapor Programming 4 07-16-2003 06:44 AM


All times are GMT -5. The time now is 07:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration