10 Replies - 440 Views - Last Post: 03 October 2012 - 04:13 PM Rate Topic: -----

#1 Tuakro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-November 11

ss.fail() problem.

Posted 03 October 2012 - 01:54 PM

Hi, i have code for a program that takes in input in the form of getline (cin, INPUT_LINE)
The format of this input should be
(int) (int) (any non-whitespace character/number combination) (double)

and if the inputs are not of this format then an error should be printed.

The particular case i am having problems for is when i reach the double ie if i put:
1 3 rt 12a
this input should produce "Error: argument is not a number" but instead its producing "Error: too many arguments"
for context, the point of this program is to detect errors in input (nothing else) and then print an error message, for this particular case it should be the first error.
Here is the code that checks if the last input is a double
int checkDoubleNumber (stringstream &ss){
    double resistance;
    ss>>resistance;
    if (ss.fail()){
        if (ss.eof()){
            cout << "Error: missing argument" << endl;
            return 1;
        }
        else{
            cout << "Error: argument is not a number" << endl;
            return 1;
        }
    }
    else return 0;
}


in the main function it is called like this:
int main(){
while(1){
.
.
.
if (checkDoubleNumber(ss)) {continue;}
.
.
.
}
return 0;
}



Thanks for any help.

Also the way im checking for too many arguments is creating an extra string (called error) and then after the double i do ss>>error and if error != ""(this means nothing i hope?) it prints "Error: too many arguments"

Is This A Good Question/Topic? 0
  • +

Replies To: ss.fail() problem.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,393
  • Joined: 25-December 09

Re: ss.fail() problem.

Posted 03 October 2012 - 02:07 PM

Quote

The particular case i am having problems for is when i reach the double ie if i put:
1 3 rt 12a
this input should produce "Error: argument is not a number" but instead its producing "Error: too many arguments"

Where does your error: "Error: too many arguments" actually come from I don't see it in the code you provided. Also, based on your description:

Quote

The format of this input should be
(int) (int) (any non-whitespace character/number combination) (double)

The above input has 5 inputs. The int 1, the int 2, the string "rt", the double 12 and the string "a". Remember the extraction operator>> stops processing several ways. The most common is that is stops when it encounters a white space character. Another way is that it stops processing a numeric value when it encounters a non-number, in the case of a double the non-number is the "a". It will leave this non-number, white space character in the buffer for the next input operation.

Jim

This post has been edited by jimblumberg: 03 October 2012 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 Tuakro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-November 11

Re: ss.fail() problem.

Posted 03 October 2012 - 02:55 PM

i see... is there any other way of checking type mismatch other than with ss.fail()?
Was This Post Helpful? 0
  • +
  • -

#4 Tuakro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-November 11

Re: ss.fail() problem.

Posted 03 October 2012 - 03:15 PM

View PostTuakro, on 03 October 2012 - 02:55 PM, said:

i see... is there any other way of checking type mismatch other than with ss.fail()?
Might there be a way to force stringstream to go to the next variable only on whitespace?
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: ss.fail() problem.

Posted 03 October 2012 - 03:21 PM

You could output from the stringstream to string, then test for invalid characters.

cctype
Was This Post Helpful? 0
  • +
  • -

#6 Tuakro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-November 11

Re: ss.fail() problem.

Posted 03 October 2012 - 03:25 PM

View Post#define, on 03 October 2012 - 03:21 PM, said:

You could output from the stringstream to string, then test for invalid characters.

cctype

Yes i am currently attempting to do that with ss.peek() but i cant seem to find a decent guide for its syntax. when using ss.peak() should i be comparing the value it returns with ascii code numbers?? or just the characters?
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: ss.fail() problem.

Posted 03 October 2012 - 03:35 PM

If you output to a string you can use the [] operators.

    string resistance;
    ss>>resistance;

    cout << resistance[0] << endl;


Was This Post Helpful? 0
  • +
  • -

#8 Tuakro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-November 11

Re: ss.fail() problem.

Posted 03 October 2012 - 03:40 PM

currently i am trying to implement the checker using ss.peek to check if each character is from 0 to 9 (for the ints) i have
int checkIfNumber (stringstream &ss){
    string NODE;
    ss>>NODE;
    char c;
    c=ss.peek();
    while (c!=' '){
        c=ss.peek();
        if ((c<'0')||(c>'9')) {cout << "Error: argument is not a number" << endl; return 1;}
    }

    return 0;

}



but this doesnt seem to ever hit the error msg, it just puts w.e the input is into the string NODE
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: ss.fail() problem.

Posted 03 October 2012 - 03:54 PM

If you are going to check the characters on the the stream then you dont need to output to a string. Peek doesn't remove characters so you need to extract a character.
Was This Post Helpful? 0
  • +
  • -

#10 Tuakro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-November 11

Re: ss.fail() problem.

Posted 03 October 2012 - 04:03 PM

View Post#define, on 03 October 2012 - 03:54 PM, said:

If you are going to check the characters on the the stream then you dont need to output to a string. Peek doesn't remove characters so you need to extract a character.
ok i also attempted to use the method you suggested
int checkIfNumber (stringstream &ss){
    string NODE;
    int i=0;
    ss>>NODE;
    char c=NODE[i];

    while (c!=32){
        cout << c << endl;
        if ((c<48)||(c>57)) {cout << "Error: argument is not a number" << endl; return 1;}
        i++;
        c=NODE[i];
    }

    return 0;

}

but for some reason it won't break out of the loop when it reaches a whitespace
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: ss.fail() problem.

Posted 03 October 2012 - 04:13 PM

There won't be any whitespace at the end of the string, you could use the length/size of the string to find the end.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1