11 Replies - 2028 Views - Last Post: 25 June 2010 - 06:14 AM Rate Topic: -----

#1 Guest_akrddark*


Reputation:

multiple if statements

Posted 23 June 2010 - 08:23 AM

hello , i am writing a code to find the time required to travel a distance, while traveling with speed of light . now here is the issue i want to write code such that there is a option to specify the speed of light by the user if they wish to do so or proceed with the already fed value.i tired using the if statements but somehow it always asks for the user to enter the speed of light . here is the code ..

# include <iostream >
# include <cmath>
using namespace std;
int main ()
{
      double speed_of_light,distance,time ;
      bool x ;
       cout << "please note all data in units of SI" << " \n"<< " would you like to enter the value of speed of light ?" << " \n" << "reply in 1 for yes and 0 for no "   ;
       cout << " \n " ; cin >> x ;
       if  ( x = 1 , cout << " please enter the value of speed of light : ",
       cin >> speed_of_light ,cout << " \n " << " please enter the distance: ", cin >> distance );
       else if ( x = 0 , speed_of_light = 300000000);
       time = distance / speed_of_light;
      cout << " the time needed is :" << time ;
      cin.ignore ( 256 , '/n' );
      cout << " press enter to continue " << endl ;
      cin.get () ;
      return 0;
      }


p.s i use dev c ++

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0

Replies To: multiple if statements

#2 Guest_akrddark*


Reputation:

Re: multiple if statements

Posted 23 June 2010 - 08:24 AM

hello , i am writing a code to find the time required to travel a distance, while traveling with speed of light . now here is the issue i want to write code such that there is a option to specify the speed of light by the user if they wish to do so or proceed with the already fed value.i tired using the if statements but somehow it always asks for the user to enter the speed of light . here is the code ..

# include <iostream >
# include <cmath>
using namespace std;
int main ()
{
      double speed_of_light,distance,time ;
      bool x ;
       cout << "please note all data in units of SI" << " \n"<< " would you like to enter the value of speed of light ?" << " \n" << "reply in 1 for yes and 0 for no "   ;
       cout << " \n " ; cin >> x ;
       if  ( x = 1 , cout << " please enter the value of speed of light : ",
       cin >> speed_of_light ,cout << " \n " << " please enter the distance: ", cin >> distance );
       else if ( x = 0 , speed_of_light = 300000000);
       time = distance / speed_of_light;
      cout << " the time needed is :" << time ;
      cin.ignore ( 256 , '/n' );
      cout << " press enter to continue " << endl ;
      cin.get () ;
      return 0;
      }


p.s i use dev c ++

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: multiple if statements

Posted 23 June 2010 - 08:27 AM

Topics merged, please no more duplicates :)
Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: multiple if statements

Posted 23 June 2010 - 08:44 AM

you should read up on c++ control flow basics. You cannot seperate conditions with a comma ',', for example this is wrong
if(a == 1, b == 2) //wrong!




There are two different operators to accomplish this,

|| or
&& and


if(a == 1 || b == 2) //if a == 1 or b == 2 then execute 



if(a == 1 && b == 2) //if a ==1 and b == 2 then execute


When checking conditions you use the comparison operator == not the assignment operator.

the comparison operator == returns a boolean value, true or false. The assignment operator does not have a return value it will always execute

int x = 5;
int y = 10;

if(x = y) //this will always execute, your not comparing anything.. your assigning
{
    //now x = y
}




you need to use the comparison operator

int x = 5;
int y = 10;

if(x == y) //notice the comparison operator ==
   cout<<"X EQUALS Y"<<endl;
else //do not equal
   cout<<"NOT EQUAL"<<endl;


This post has been edited by ImaSexy: 23 June 2010 - 08:51 AM

Was This Post Helpful? 2
  • +
  • -

#5 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: multiple if statements

Posted 23 June 2010 - 08:53 AM

I suggest including more spaces between your lines. That, and proper indentation will make it much easier for you to read and debug.

One you make those changes and do what ImaSexy has commented on, you should easily be able to get this program working like you want.

If after you make those changes it's still not working, post the updated code on here and we'll take a look.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_akrddark*


Reputation:

Re: multiple if statements

Posted 23 June 2010 - 08:57 AM

View PostImaSexy, on 23 June 2010 - 07:44 AM, said:

you should read up on c++ control flow basics. You cannot seperate conditions with a comma ',', for example this is wrong
if(a == 1, b == 2) //wrong!




There are two different operators to accomplish this,

|| or
&& and


if(a == 1 || b == 2) //if a == 1 or b == 2 then execute 



if(a == 1 && b == 2) //if a ==1 and b == 2 then execute


When checking conditions you use the comparison operator == not the assignment operator.

the comparison operator == returns a boolean value, true or false. The assignment operator does not have a return value it will always execute

int x = 5;
int y = 10;

if(x = y) //this will always execute, your not comparing anything.. your assigning
{
    //now x = y
}




you need to use the comparison operator

int x = 5;
int y = 10;

if(x == y) //notice the comparison operator ==
   cout<<"X EQUALS Y"<<endl;
else //do not equal
   cout<<"NOT EQUAL"<<endl;



ok , well first thing i began learning c++ today about 3 hours ago so my basic will be weak , second here is my rewritten code , i would appreciate correction in the if statement clearly or read a new one ( only if statement ) ok here is the code
# include <iostream >
# include <cmath>
using namespace std;
int main ()
{
      double speed_of_light,distance,time ;
      bool x ;
       cout << "please note all data in units of SI" << " \n"<< " would you like to enter the value of speed of light ?" << " \n" << "reply in 1 for yes and 0 for no "   ;
       cout << " \n " ; cin >> x ;
       if  (( x == 1 && cout << " please enter the value of speed of light : " &&
       cin >> speed_of_light && cout << " \n " << " please enter the distance: "
       && cin >> distance ) || ( x == 0 && speed_of_light = 300000000));
       time = distance / speed_of_light;
      cout << " the time needed is :" << time ;
      cin.ignore ( 256 , '/n' );
      cout << " press enter to continue " << endl ;
      cin.get () ;
      return 0;
      }
      
         


Was This Post Helpful? 0

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: multiple if statements

Posted 23 June 2010 - 09:10 AM

heres the logic for an if statment,

if the condition is true, execute code beween brackets
if  ( x == 1 )//if this is true
{
    //execute code within these brackets
}




Dont try to put all of your code within the conditions of your if statment
Was This Post Helpful? 0
  • +
  • -

#8 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: multiple if statements

Posted 23 June 2010 - 11:20 AM

Let's take a look at this part of your code now that I've deciphered what you are trying to say:
 if  (( x == 1 && cout << " please enter the value of speed of light : " &&
       cin >> speed_of_light && cout << " \n " << " please enter the distance: "
       && cin >> distance ) || ( x == 0 && speed_of_light = 300000000));



What I think you're trying to say here is:

If x(the value the user enters at the beginning about whether or not he wants to choose a speed of light) is equal to 1....
 if  (( x == 1//this is your code, you should change it to if(x ==1)




...then you want to allow the user to enter the values.
cout << " please enter the value of speed of light"
cin >> speed_of_light
cout << " \n " << " please enter the distance:"
cin >> distance



ELSE IF x is equal to 0 (you put || where there should be an else-if)....
speed_of_light = 300000000





Just remember, after if-statements there should be brackets({ }) that have everything inside of them that goes in that part. And then there should also be brackets for the else-if and have everything that goes in there.
Was This Post Helpful? 1
  • +
  • -

#9 Guest_akrddark*


Reputation:

Re: multiple if statements

Posted 23 June 2010 - 10:30 PM

ok i included all changes yet i seemed to have messed up in the if statement so here it is
if  ( x == 1) 
       ({ cout << " please enter the value of speed of light : " 
       && cin >> speed_of_light && cout << " \n " << " please enter the distance:" 
       && cin >> distance});
       else if ( x == 0 ) 
      ({ speed_of_light = 300000000});


Was This Post Helpful? 0

#10 v0rtex   User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: multiple if statements

Posted 24 June 2010 - 01:44 AM

Here is what I think you were looking for:
#include <iostream>

using namespace std;

int main()
{
      double speed_of_light,distance,time ;
      bool x ; 
//1 = TRUE, 0 = FALSE
       cout << "please note all data in units of SI" << " \n"<< " would you like to enter the value of speed of light ?" << " \n" << "reply in 1 for yes and 0 for no "   ;
       cout << " \n " ; cin >> x ;
    if  ( x == 1)  //if int x is equal to 1, execute body
       { 
            //body:
       cout << " please enter the value of speed of light : "; 
       cin >> speed_of_light;
       cout << " \n " << " please enter the distance:";
       cin >> distance;
       }
       else if ( x == 0 ) //if x is equal to 0, then execute statement below:
       speed_of_light = 300000000;
       /*if no braces are included then only the next statement will 
       be executed as part of the if statement, in this case, that is sufficient
       */  
       cout << " the time needed is :" << time ;
      cin.ignore ( 256 , '/n' );
      cout << " press enter to continue " << endl ;
      cin.get () ;
    return 0;
}


This post has been edited by v0rtex: 24 June 2010 - 01:46 AM

Was This Post Helpful? -1
  • +
  • -

#11 Guest_akrddark*


Reputation:

Re: multiple if statements

Posted 24 June 2010 - 09:37 PM

ok i tired everything and finally perfected it . i wanna thank everyone who bothered to read and suggest included the admin,imasexy, garbage collector and suggesting me .. now here is the code.
# include <iostream >
# include <cmath>
using namespace std;
int main ()
{
      double speed_of_light,distance,time ;
      bool x ;
       cout << "please note all data in units of SI" << " \n"
       << " would you like to enter the value of speed of light ?" << " \n" 
       << "reply in 1 for yes and 0 for no " ;
       cout << " \n " ; cin >> x ;
       if  ( x == 1) 
       { cout << " please enter the value of speed of light : " ;
        cin >> speed_of_light ; 
        }
       else if ( x == 0 ) 
      { speed_of_light = 300000000;
       }
       cout << " \n " << " please enter the distance:"; 
        cin >> distance;
      time = (distance) / speed_of_light;
      cout << " the time needed is :" << time ;
      cin.ignore ( 256 , '/n' );
      cout << " press enter to continue " << endl ;
      cin.get () ;
      return 0;
      }


Was This Post Helpful? 0

#12 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: multiple if statements

Posted 25 June 2010 - 06:14 AM

Glad I could be of assistance!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1