user3398442
user3398442

Reputation: 3

How do I declare a variable in a if-else statement?

I'm still a newbie at this and I really dont understand many things THE ONLY THING THAT IS MISSING IS x=d/t and I don't know where to put it... I already tried various methods but still can't figure out where to put it.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{


     int d, t, s, z;
     cout<<"Enter the distance of your destination:"<<endl;
     cin >>d;
     cout<<"Enter the time you want to get there:"<<endl;
     cin >>t;

     for (z=1; z<=5; z++)
     {
      cout<<"Enter your speed:"<<endl;
      cin>>s;

      if (int x=d/t && x>=s)
      { 

              cout<<"Maintain your speed it is the appopriate speed: "<<s<<"mph"<<endl;
              break;
              }
              else 
              {

              cout<<"Go faster at this rate you won't get to your destination on time"<<endl;
              }
              }
    system("PAUSE");
    return EXIT_SUCCESS;
}

The Output always shows the if statement even though it should already show the else statement.. 
I really need help on this..

Upvotes: 0

Views: 172

Answers (7)

vonbrand
vonbrand

Reputation: 11831

I guess your problem is that the input data (distance, time) should be double, not int. In C++, division of integers truncates, i.e., 2 / 3 == 0. Has thrown me off a couple of times... Integers are for counting, not for measurements.

And don't worry about writing some expression several times, current compilers are smart enough to notice and compute it once. In any case, write the simplest, clearest code possible. Only if that shows to be too slow (unlikely) does it really pay to go over the code to tighten it up. Your time writing the code, understanding what is really written when it misbehaves, and fixing it is more valuable than a few seconds of computer time.

Never forget Brian Kernighan's "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Also Donald Knuth's "Premature optimization is the root of all evil".

Upvotes: 1

user3165240
user3165240

Reputation: 36

This is the code:

   int main(int argc, char *argv[])

{

 int d, t, s, z;
 cout<<"Enter the distance of your destination:"<<endl;
 cin >>d;
 cout<<"Enter the time you want to get there:"<<endl;
 cin >>t;

 for (z=1; z<=5; z++)
 {
  cout<<"Enter your speed:"<<endl;
  cin>>s;
 int x=d/t;
  if (x>=s)
  { 

          cout<<"Maintain your speed it is the appopriate speed: "<<s<<"mph"<<endl;
          break;
          }
          else 
          {

          cout<<"Go faster at this rate you won't get to your destination on     time"<<endl;
          }
          }
system("PAUSE");
return EXIT_SUCCESS;
       }

Upvotes: 1

agentp
agentp

Reputation: 6999

why declare x at all?

 if(d/t>=s)

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60037

Try

int x = d/t;
if (x >= s) {
 ....

As per a book on C++

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129524

You don't do that.

Use this if you want to do exactly what you've written:

int x = d/t;
if (x && x >= s)
{
   ...
}

but you probably really just wanted:

if(x >= s)

Upvotes: 1

masoud
masoud

Reputation: 56539

Just put it outside the if

int x = d / t;
if (x && x >= s)

or maybe you want this

int x = d / t;
if (x >= s)

Upvotes: 2

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Write instead:

  int x=d/t;
  if(x && x>=s)
  { 
      // ...

to get the result exactly for what you have written.

Upvotes: 1

Related Questions