Menu

ignore inputed chars

2003-02-07
2012-09-26
  • Nobody/Anonymous

    How can you eliminate commas from inputed numbers? I've tried cin.ignore(','); But that doesnt work.

    ex.
    -----------------------------------------------
    cout<<"Input for 'a'\n";
    cin.ignore(',');
    cin>>a;

    -------------------------------------------------
    --nilson

     
    • Anonymous

      Anonymous - 2003-02-07

      Perhaps input the whole string and write your own code to parse it separately. This probably allows more robust error checking.

      You could use strtok() to do this. I know it is from the C library, but I don't get too religious about that.

      Clifford

       
    • Nobody/Anonymous

      How would you use strtok()?

      --nilson

       
      • Anonymous

        Anonymous - 2003-02-08

        The C++ purists will hate this, but I am an embedded systems programmer, and usually avoid the C++ standard library. That is no reason for you to do so if there is a better C++ approach. but here goes:

        Where instring is a C style string of comma separated numeric integer values:

        char* token ;
        int i ;
        int valarray[MAX_VAL_COUNT]

        token = token strtok( instring, "," ) ;
        for( i = 0; i < MAX_VAL_COUNT && token != 0; i++ )
        {
            valarray[i] = atoi( token ) ;

            token = token strtok( 0, "," ) ;
        }

        Note that strtok() modifies instring by inserting null characters - the token are not copied, if it is necessary to keep the original string intact, copy it first. It ignores whitespace. so the tokens for the string "123, 55, 66" are "123", "55", and "66". These are converted in this example using atoi(). The separator string parameter od strtok() can contain multiple characters.

         
    • Nobody/Anonymous

      Just a note, don't mix  c headers with c++.  The headers as  stdlib.h  in c, in c++ use  cstdlib the C++ version of this header.. Eventhough the understanding is c++ is just  a superset of c, the differences in the headers are important .

       
    • Nobody/Anonymous

      I am still finishing touches on that program I was working on before, when Wayne helped me with the math, but I cant figure out how you remove the unwanted charachter ',' from the integer. It seems like cin.ignore(','); would work, but it doesnt, I get errors.Please help. By the way, the program is C++

      --nilson

       
    • Nobody/Anonymous

      ignore() takes 2 parameters, here's the prototype for it:

      istream &ignore(streamsize num=1, int_typ delim=EOF);

      it reads and discards characters until either num characters have been ignored, or the delim character is reached.
      Doesn't look like it does what you want, I would input the number as a sting,  remove the commas, and convert it back to a number.

      blackadder

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.