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;
#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:
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 ?
#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" ;
longlong 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' ;
}
}
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..