Моя проблема заключается в том, что когда я компилирую и запускаю программу, часы не имеют "часов" с листингом 1,2,3, когда цикл продолжается, а также вычисление цикла одинаково для каждой строки.
это то, что программа выглядит как http://postimg.org/image/htk194eah/
расчет неправильный, и часы, предположительно, говорят 1,2... 5
Я хотел бы, чтобы это выглядело примерно так http://postimg.org/image/pnkvab1j1/
это то, что у меня есть до сих пор:
int main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled" <<endl;
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 " <<endl;
cin >> time;
}
// Calculation
distance = speed * time;
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << " " << "\t\t" << speed*time << endl;
}
system ("PAUSE");
return 0;
}
Когда вы печатаете в своем цикле for, скорость = 20 и время = 5 всегда, поэтому он всегда печатает 100 (в примере, который вы даете).
Вы хотите, чтобы скорость печати * подсчитывалась (в данном случае).
Снова вы печатаете "" в течение часов, что является пустой строкой, вы хотите напечатать счетчик.
cout << count << "\t\t" << speed*count << endl;
Вот правильная программа. Ваша программа была очень хорошей. Но у вас очень маленькая ошибка.
#include<iostream.h>
main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled ";
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 ";
cin >> time;
}
// Calculation
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << count << "\t\t" << speed * count << endl;
}
system ("PAUSE");
return 0;
}
while(speed < 0) // while statement
отличный комментарий :)