0

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..
Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • Regarding `if (int x=d/t && x>=s)`, did you mean `if (x == d/t && x >= s)`? Also, and don't take this the wrong way, this is basic C++. Your best bet is to google 'C++ Tutorial Beginners' or something similar and follow that. It will teach things like this amongst others. – OMGtechy Mar 09 '14 at 13:11
  • possible duplicate of [C++, variable declaration in 'if' expression](http://stackoverflow.com/questions/7836867/c-variable-declaration-in-if-expression) – jrok Mar 09 '14 at 13:12
  • 1
    declare x in the same line, like this: `int d, t, s, z, x;` then, put this `x=d/t;` immediately before the if and the if should look like this: `if(x>=s)` – artur99 Mar 09 '14 at 13:12

7 Answers7

2

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)
masoud
  • 55,379
  • 16
  • 141
  • 208
1

Write instead:

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

to get the result exactly for what you have written.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

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)
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

Try

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

As per a book on C++

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

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;
       }
artur99
  • 818
  • 6
  • 18
1

why declare x at all?

 if(d/t>=s)
agentp
  • 6,849
  • 2
  • 19
  • 37
  • what doesn't work? There is no reason to declare the variable if you are never using it again. My guess your real issue is using integers in what likely should be a floating point calculation. – agentp Mar 09 '14 at 13:30
  • THis may fall foul of the integer division returning the result truncated to integer precision. This can be avoided by rearranging: `if (d >= s * t)` – DanS Mar 13 '14 at 09:33
1

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".

vonbrand
  • 11,412
  • 8
  • 32
  • 52