Проблемы ввода и вывода в C ++

0

Моя проблема заключается в том, что когда я компилирую и запускаю программу, часы не имеют "часов" с листингом 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;
}
  • 0
    C! = C ++. В общем, вам не следует пересекать теги, так как языки могут быть очень разными.
  • 0
    while(speed < 0) // while statement отличный комментарий :)
Показать ещё 2 комментария
Теги:
loops
while-loop
distance

2 ответа

0
Лучший ответ

Когда вы печатаете в своем цикле for, скорость = 20 и время = 5 всегда, поэтому он всегда печатает 100 (в примере, который вы даете).

Вы хотите, чтобы скорость печати * подсчитывалась (в данном случае).

Снова вы печатаете "" в течение часов, что является пустой строкой, вы хотите напечатать счетчик.

cout << count << "\t\t" << speed*count << endl;
  • 0
    это работает! благодарю вас.
  • 0
    Однако как мне заставить часы сказать 1,2,3 ... 5?
0

Вот правильная программа. Ваша программа была очень хорошей. Но у вас очень маленькая ошибка.

#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;
    }

Ещё вопросы

Сообщество Overcoder
Наверх
Меню