0

first of all I'm sorry for my bad english.

I'm trying to read some numbers and write them into a vector in C++. This should go as long as the input is a double number and the loop should be stopped if the user writes an 'a'.

My Question is how can I check if the input is 'a'. Breaking the loop is not the problem

while(true){
    if(!(cin>>userInput)){
        //here i want to know if the input is 'a' or some other stuff//
        //also i want to do some other stuff like printing everything//
        //what already is in the vector//
        //when everything is done; break//
       }
    else
       //the input is a valid number and i push it into my vector//

'userInput' is defined as double so the loop will stop. My Problem is, if the user write 'q' the loop stops but it's instantly stoping the whole program. My try look like this:

    while(true){    //read as long as you can
    cout<<"Input a number. With 'q' you can stop: "<<endl;
    if(!(cin>>userInput)){ //here the progam stops when the input is anything but a number
        cout<<"How many numbers do you want to add up?"<<endl; //there are numbers in a vector that should be added up
        cin>>numberOfAdditions;
        break;
    }

So I have a vector with some numbers the users writes down (20,50,90,...) When the input is equal to 'q' (in this example everything but numbers ) the loop stops and I want to ask the user how many numbers should be added. The cout-command is displayed but the input is beeing skipped. So my program is not reading how many valued from the vector I want to add.

I hope you know what I mean and I don't want to use two questions and two variables to save the input but if it's not working without it I'll change my program.

Have a nice Day :)

CodeHard
  • 23
  • 4
  • it is already disscused in this post http://stackoverflow.com/questions/3273993/how-do-i-validate-user-input-as-a-double-in-c – anbu selvan Nov 06 '13 at 08:48

2 Answers2

0

Because your Input variable is of type double you have to flush the Input from cin before reading again. Otherwise there is still a newline in the buffer. Consider the following example:

#include <iostream>

using namespace std;

int main(){
  double userInput;
  int numberOfAdditions;
  while(true){    //read as long as you can
    cout<<"Input a number. With 'q' you can stop: "<<endl;
    if(!(cin>>userInput)){ //here the progam stops when the input is anything but a number
      cout<<"How many numbers do you want to add up?"<<endl; //there are numbers in a vector that should be added up
      cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
      cin.clear();
      cin.ignore(INT_MAX,'\n'); 
      cin >> numberOfAdditions;
      break;
    }
  }
  return 0;
}

The two Statements:

cin.clear();
cin.ignore(INT_MAX,'\n'); 

are flushing the Input stream until the newline is encountered.

Constantin
  • 8,721
  • 13
  • 75
  • 126
0

The first answer already explains how to flush the cin stream after the user types in a char. If you want to determine which character it was, you should define userInput as std::string. If the string is not "q" or "a" or whatever you are looking for, you have to cast the string to a double, just like this:

std::string str;
cin >> str;

if (str == "j")
    // User typed in a special character
    // ...some code...
else
    double d = atof(str.c_str());  // Cast user input to double

Notice that the result of the cast is zero, if the user typed in any other string than the ones you especially look for.

Dave Chandler
  • 651
  • 6
  • 17