Hi, i'm new in C++

i was asking to get an input from user, eg. 12 -8 7/8 6 D. Valid input is type of whole number and fraction, and D marking end of input.
i'm doing the input validation. if there is an error input, eg. 4 92 1/2 +6 55 14 D, where +6 is an error input, break from the reading and go back to ask the user to re-input again.

i'm using cin to get the input. if the program encounter an error input, it will re-asking the user again, but before the user typing some input, it still read the previous input, the one after the error input, because it was missed by the "break;"

so i'm trying to do like this,

if(!valid)
{
   while(input[0] != '\n')
   {
      cin >> input;
   }
}

hoping that if there is an error input, the program will still read the misses input until no more input, ie. end of line, but do nothing to it.

but this code seems didn't work.

is there any other way tracing the end of line instead of '\n'?

regards,
mel

the problem is i want to read each number/fraction seperately, so i can validate one by one and if the validation successful, i can directly create the object of each type.

or is there a way to tokenize the input other than strtok, since i not allowed to use C library function?

oops..

now i get what you mean.. read only the rest of the input after getting error using getline..

thank you very much on helping me solving this problem ^^

regards,
Mel

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  bool validated = true ;
  do
  {
    std::string line ;
    std::getline( std::cin, line ) ; // read next line
    std::istringstream stm(line) ;
    // read each number/fraction seperately from stm
    // eg. int a ; stm >> a ; etc.
    // validate one by one
    // if the validation is successful,
    //     create the object of each type.
    //     set validated = true ;
    // else
    //     set validated = false ;
  } while( !validated ) ;
}
This article has been dead for over six months. Start a new discussion instead.