how to fix it !

Hour Distance Traveled
-------------------------------
1 40

2 80

3 120

my display output should be as same as above but I got :

Hour Distance Traveled
-------------------------------
3 120
3 120
3 120

My program: note that my input hours is 3-speed is 40

#include<iostream>
#include<iomanip>
using namespace std;
int main(){


int speed,hour,i,distance_traveled=0;


cout<<"Enter hours traveled \n \n ";
cin>>hour;

cout<<"Enter speed of vehicle(mile/hour) \n \n";
cin>>speed;

cout<<setw(10)<<"hour"<<setw(25)<<"Distance Traveled"<<endl;


if(hour>=1&&speed>=0)
{
i=1;
while(i<=hour)
{

{distance_traveled=speed*hour;
distance_traveled+=0; }

i++;


cout<<setw(6)<<hour<<setw(10)<<distance_traveled<<endl;
}

}

else
cout<<"Invalid values"<<endl;




return 0;
}


Last edited on
distance_traveled+=0; this does absolutely nothing.

distance_traveled=speed*hour; this looks like it should use 'i' instead of 'hour'

any loop can do the work of any other loop, the difference is clean looking code.
All that code can be boiled down to more or less this:
//if statement is not needed; if hour is zero, it won't enter the loop.
for(int i = 1; i <= hour; i++)
cout << i << " " << speed*i << endl;
if(hour <= 0 || speed <=0) //note the changed logic for inverse condition.
cout<<"Invalid values"<<endl;
Last edited on
First, code tags and indentation make posted code much more readable. See http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<iomanip>

int main(){
  using std::cin;
  using std::cout;
  int speed, hour, i, distance_traveled=0;
  cin >> hour;
  cin >> speed;

  if ( hour>=1 && speed >=0 )
  {
    i=1;
    while ( i <= hour )
    {
      {
        distance_traveled = speed*hour;
        distance_traveled +=0;
      }
      i++;
      cout << hour << std::setw(10) << distance_traveled << '\n';
    }
  }
  else
    cout << "Invalid values\n";
}

Line 18 does nothing.
Brace scope (lines 16 and 19) adds nothing.
Testing for invalid input could be separate step before the loop.
With those changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<iomanip>

int main(){
  using std::cin;
  using std::cout;
  int speed, hour, i, distance_traveled=0;
  cin >> hour;
  cin >> speed;

  if ( hour < 1 || speed < 0 )
  {
    cout << "Invalid values\n";
    return 1;
  }

  i=1;
  while ( i <= hour )
  {
    distance_traveled = speed*hour;
    i++;
    cout << hour << std::setw(10) << distance_traveled << '\n';
  }
}

That loop could be a for loop and result is same without the verbose (which is good) 'distance_traveled':
1
2
3
4
  for ( int h=0; h < hour; ++h ) // repeat hour times
  {
    cout << hour << std::setw(10) << speed*hour << '\n';
  }

The 'speed' does not change.
The 'hour' does not change.

On every iteration you do print the same 'hour' and 'speed*hour'.
guys your notes are helpful but regarding hours they really should be incrementing too i mean never they are the same as this :
3 3 3

if I entered 3 then I should get 1 2 3 and stop …

so i ran the program and this thing remains the same.

@jonnin: your note about changing hour to i was the key and useful.

one more thing guys you both said about using the logic Or instead of And but what if the question said : Input Validation: Do not accept a negative number for speed and do not accept any value less than one for time traveled.

main problem :
now the speed is incrementing right but hours are all 3 ?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>

int main() {

    int num_hours = 0 ;
    // do not accept any value less than one for time traveled.
    // note: we assume that the user does enter a valid integer
    while( std::cout << "hours travelled: " && std::cin >> num_hours && num_hours < 1 )
        std::cout << "please enter a positive value hor hours travelled\n" ;

    int speed = 0 ;
    // Do not accept a negative number for speed
    // note: again, we assume that the user enters a valid integer
    while( std::cout << "speed (miles pr hour): " && std::cin >> speed && speed < 0 )
        std::cout << "please enter a non-negative speed\n" ;

    std::cout << '\n' << "hour" << "   " << "distance (miles)\n"
                      << "----" << "   " << "----------------\n" ;

    long long distance_travelled = 0 ;
    for( int hour = 1 ; hour <= num_hours ; ++hour ) {

        distance_travelled += speed ; // every hour, we travel another 'speed' miles
        std::cout << std::setw(3) << hour << "   " << std::setw(10) << distance_travelled << '\n' ;
    }
}
The logic of and/or:
IF time is good and speed is good
THEN both are good
ELSE both are not good


IF time is bad or speed is bad
THEN both are not good
ELSE both are good


now the speed is incrementing right but hours are all 3 ?

Compare:
1
2
3
4
for ( int h=0; h < hours; ++h )
{
  cout << hours << '\n';
}

and
1
2
3
4
for ( int h=0; h < hours; ++h )
{
  cout << h << '\n';
}

one more thing guys you both said about using the logic Or instead of And but what if the question said : Input Validation: Do not accept a negative number for speed and do not accept any value less than one for time traveled.

--------------
english words vs computer logic may not always match exactly.
you can flip the computer's logic any which way --- there are infinite ways to code an expression that is identical logically but different to read.

If you gave me the above, it could be..
if(speed < 0 || time < 1)
// error

note the OR. the english says AND. Why? Because that c++ expression is what you NEED to express speed is less than zero AND time is less than 1 as an error. Its the inverse expression, though, we are looking for the error condition, not the accepted condition. If you study your boolean math, you will see that reversing a condition almost always** inverts the ands and ors, but that takes some getting used to. ** the not operation or complicated logic may double invert the words making them appear to have not changed, is all.

you could also say this, though:
if( speed >= 0 && time >= 1)
//not an error.
This is the c++ expression you NEED if you want to check it from the other side.

both approaches work. you can throw an error if it matches the error condition (the first version) or you can proceed with processing if no error is found (second version).

Hopefully I inverted that right. Im having a bit of a day... even if I messed it up, hopefully you see what I meant..
Last edited on
I have read all of your messages thanks to you all.

Topic archived. No new replies allowed.