type checking?

This is a discussion on type checking? within the C++ Programming forums, part of the General Programming Boards category; Dear All! I am trying to read lines from an input file. in one line There a are some words ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    23

    Question type checking?

    Dear All!

    I am trying to read lines from an input file. in one line There a are some words and double type numbers. I would like to assign these values to some string variables and double variables. For that reason I have to know the type of variable.
    What is the best way? Any help appreciated. thanks!

    code part ia mtrying to read the input file is the following: you can complete it.
    Code:
     string line, temp;
     double number;
     while(getline(fmonju,line)){
      if(!line.empty()){
        istringstream iss(line);
         while(iss.good()){
            iss>>temp; // ????
            iss>>number; // ???
         } 
      }                          
     }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you saying that the data in your input file MAY be a string, OR a double? You probably will be best off scanning the line for digits, then attempt to read the number from the first digit you find...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    23
    Yes, input file inlcude many lines of data. in one line there are 5 or 6 columns (it may change). these columns may be string or a double like that:

    1 3 ABB DD 5
    2 4 GT ty 4
    . . . .. .
    . . . . .
    . . . . .

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Then you need to read the entire line, then "split" it into portions "by hand", by checking the starting letter of each "chunk" of input, and decide if it's a number or text.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,026
    You'll need to write your own class. The class will have a string and a double in it and will know what type of cell it is.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Another question is what you are going to do with temp and number (you are not trying to read some randomly organized file for no good reason?).

    Anyhow, you might just try to convert every word to a double and if that fails, you have a string. boost has lexical_cast, but you could also write a function that attempts the conversion using stringstream.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    23
    King Mir : I did not understand what type of class you mean. I will be pleased if you could give a simple example.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    23
    Another question is what you are going to do with temp and number (you are not trying to read some randomly organized file for no good reason?).
    I will do some simple change in the number, I will write into another file.

    Anyhow, you might just try to convert every word to a double and if that fails, you have a string. boost has lexical_cast, but you could also write a function that attempts the conversion using stringstream.
    I am not good at using string streams, could you give a simple example. Thank you.

  9. #9
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Don't quote me on that... ...seriously

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Here's one way to look at it: every possible number can be represented as a string, but not vice versa. So you could read in each number/string as a string, look at it for digits, and if there are any, convert the string to a number.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,026

    Thumbs up

    Quote Originally Posted by wolfindark View Post
    King Mir : I did not understand what type of class you mean. I will be pleased if you could give a simple example.
    It would look something like this:
    Code:
    class StringOrDouble{
        private:
            std::string stringVersion;
            double doubleVersion;
            boolean typeIsDouble;
        public:
            boolean isDouble(){
                return typeIsDouble;
            }
            std::string getString(){
                  if(typeIsDouble)
                      throw myException();
                  return stringVersion;
           }
           void set(std::string){
                 //code for determining if value is a double goes here.
                 //should also set stringVersion or doubleVersion.
           }
    };
    Last edited by King Mir; 12-15-2007 at 10:05 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're confusing camel notation with "underscore notation". (string_version, stringVersion, . . . .)

    You don't need to write your own class, it would just be a good idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,026
    Quote Originally Posted by dwks View Post
    You're confusing camel notation with "underscore notation". (string_version, stringVersion, . . . .)

    You don't need to write your own class, it would just be a good idea.
    Fixed the notation.

    Chances are, a class is the way to go. A class is the best way to store a value that can be one of two types. This is C++ after all, not C.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  3. Data type checking?
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2003, 04:24 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Type checking
    By Araenor in forum C Programming
    Replies: 10
    Last Post: 08-29-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21