2017-02-10 2 views
-6

私はC + +クラスで移動した典型的な距離の宿題を得ました。しかし、この問題で先生は "for"ループを使うことはできないと言って、 "whileループ"しか使えないので、私はこのコードに固執しています。このC++プログラムを修正するには?

問題は、時間には個別に移動した距離が表示されますが、移動された距離の合計は1時間ごとに表示されることです。

#include <iostream> 
using namespace std; 

int main() 
{ 
    double distance, 
      speed, 
      time, 
      counter=1; 


    cout << "This program will display the total distance travel each hour.\n\n"; 

    cout << " What is the speed of the vehicle in mph? "; 
    cin >> speed; 

    while(speed < 0) 
    { 
     cout << " Please enter a positive number for the speed: "; 
     cin >> speed; 
    } 

    cout << " How many hours has it traveled? "; 
    cin >> time; 

    while(time < 1) 
    { 
     cout << " Please enter a number greater than 1 for the hours: "; 
     cin >> time; 
    } 


    cout << endl; 
    cout << " Hour" << "\t\t" << " Distance Traveled" << endl; 
    cout << " ------------------------------------" << endl; 

    while(counter <= time) 
    { 
     distance = speed * time; 
     cout << counter << "\t\t" << distance << endl; 
     counter++; 

    } 

    return 0; 
} 
+2

whileループとforループは基本的に同じです。あなたはforループでそれをする方法を知っていますか? – NathanOliver

+0

はい、私はforループを使っていましたが、先生は私にwhileループを求めていると言いました。 –

+0

多分、私だけかもしれませんが、これが何を意味するのか理解できません。*問題は、時間には個別に移動した距離が表示されますが、1時間に移動した距離の合計が表示されることです。個別に時間を列挙し、各時間に移動した金額を列挙することの違いは? –

答えて

-1

距離=スピード*時間と同じくらい簡単です。変数countertimeが混在しているようです。すべての増分でcounterを使用してください。これはローカル値のtimeです。

#include <iostream> 
using namespace std; 

int main() 
{ 
    double distance, 
      speed, 
      time, 
      counter=1; 


    cout << "This program will display the total distance travel each hour.\n\n"; 

    cout << " What is the speed of the vehicle in mph? "; 
    cin >> speed; 

    while(speed < 0) 
    { 
     cout << " Please enter a positive number for the speed: "; 
     cin >> speed; 
    } 

    cout << " How many hours has it traveled? "; 
    cin >> time; 

    while(time < 1) 
    { 
     cout << " Please enter a number greater than 1 for the hours: "; 
     cin >> time; 
    } 


    cout << endl; 
    cout << " Hour" << "\t\t" << " Distance Traveled" << endl; 
    cout << " ------------------------------------" << endl; 

    while(counter <= time) 
    { 
     distance = speed * counter; 
     cout << counter << "\t\t" << distance << endl; 
     counter++; 

    } 

    return 0; 
} 

テスト

This program will display the total distance travel each hour. 

What is the speed of the vehicle in mph? 50 
How many hours has it traveled? 5 

Hour  Distance Traveled 
------------------------------------ 
1  50 
2  100 
3  150 
4  200 
5  250 
+0

私を助けてくれてありがとう、あなたを迷惑してすみません。 –

+0

@HugoFederico幸い...コードがあなたのために働くことを願っています。 –

1

(私はコードから理解することができるものから)あなたはそれぞれ各時間で移動した距離を計算したいと仮定すると、これは、whileループでcounter <= timeの繰り返しを繰り返すたびに、その時間分の距離を計算するためです。時間= 1時間と言うと、あなたのコードは1時間の移動距離を計算して表示します。時間が2時間の場合、1時間と2時間(合計距離は2時間)で移動した距離をそれぞれ計算する。

例:

time = 2, speed = 60 kmph 

120は2時間としない第2の時間に1時間から距離の総距離である

1 60 
2 120 

を印刷します。

1時間ごとの移動距離を計算する必要がある場合、時間は一定で1時間です(速度は時間の経過とともに一定であると仮定します)。ためにループ、使用しながら、それを中に使用するには:n番目の時間で走行

distance = (speed * counter) - (speed *(counter - 1)) 

距離は(N-1)の時間で走行n時間マイナス距離の総距離です。

関連する問題