Tom Eaton
Tom Eaton

Reputation: 23

Having to input value twice to work C++

#include <iostream>
#include <limits>
#include <math.h>

using namespace std;

int main()
{
    float startTemperature;
    float endTemperature;
    float loopTemperature;
    float stepSize;
    float i;
    float numberOne;
    cout << "Please enter a start temperature: " << endl;;
    cin >> startTemperature;
    while(!(cin >> startTemperature)){
        cin.clear();

        cout << "Invalid input.  Try again: ";
    }
    cout << "Please enter an end temperature: ";
    cin >> endTemperature;
    while(!(cin >> endTemperature)) {
        cin.clear();
        cin.ignore(256, '\n');
        cout << "Invalid temperature. Please try again: ";
    }
    cout << "Please enter a step size: ";
    cin >> stepSize;
    while(!(cin >> stepSize)) {
        cin.clear();
        cin.ignore(256, '\n');
    }
    for(i = startTemperature; i < endTemperature; i += stepSize) {
        if(i == startTemperature) {
            cout << "Celsius" << endl;
            cout << "-------" << endl;
            cout << startTemperature << endl;
            loopTemperature = startTemperature + stepSize;
        }
        loopTemperature += stepSize;
        if(loopTemperature > 20) {
            break;
        }
        cout << loopTemperature << endl;
    }
}

Hi, The problem with this code is that I have to input the value of the temperature twice. I have looked at other answers and I think it is something to do with the cin buffer but I don't know exactly what is wrong.

Upvotes: 1

Views: 1795

Answers (3)

Rakib
Rakib

Reputation: 7625

In the line

cin >> startTemperature;  // <---problem here
while(!(cin >> startTemperature)){
    cin.clear();

    cout << "Invalid input.  Try again: ";
}

You are taking input once, then again in the loop. That's is why you had to give input twice.

Just remove first input line, same for endTemparature and stepSize.

Upvotes: 5

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42899

It's not only for the temperature but rather for every input. Change your code to the one below:

  cout << "Please enter a start temperature: " << endl;;
  while (!(cin >> startTemperature)){
    cin.clear();
    cin.ignore(std::numeric_limits<int>::max(), '\n');
    cout << "Invalid input.  Try again: ";
  }
  cout << "Please enter an end temperature: ";
  while (!(cin >> endTemperature)) {
    cin.clear();
    cin.ignore(std::numeric_limits<int>::max(), '\n');
    cout << "Invalid temperature. Please try again: ";
  }
  cout << "Please enter a step size: ";
  while (!(cin >> stepSize)) {
    cin.clear();
    cin.ignore(std::numeric_limits<int>::max(), '\n');
    cout << "Invalid step size. Please try again: ";
  }

Reason:

You had redundant cin calls. Also use std::cin.ignore(std::numeric_limits<int>::max(), '\n'); instead of arbitrary number 256.

Upvotes: 1

Dortimer
Dortimer

Reputation: 617

You're asking for input before the while loop, then again in the loop condition statement. Change the condition in your while statement to

    while(!cin){ 
    //error handling you already have
    cin>>startTemperature; //endTemperature respectively
    }

Upvotes: 1

Related Questions