Thread: joining two chars together and converting to INT

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Question joining two chars together and converting to INT

    I'm a newbie to C++ and figured this would be a good place to ask for guidance.

    I need to write a program that reads input from a txt file which contains "grades" in numeric format, I.E. 92, 55, 88 and take those grades and calculate the average.

    Now my program uses and input stream and loops through the file contents one character at a time, the values are stored in a CHAR variable called "next".

    Basically I'm wondering how I can take a grade which could be 1 to 3 characters and concatenate it, then in turn convert into an INT so I may produce the average with it.

    Here is how the txt file is formatted:

    lastname firstname grade1 grade2 grade3 etc. etc.

    doe john 100 84 82 93 45 55 82
    blue joe 28 48 98 84 93 100 89 28 84 99

    Since I only take in one CHAR at a time, I need to combine the chars before the next space occurs. I then would need to tally the grades all together into an INT so I may average it out.

    Any help would be most appreciated!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Now my program uses and input stream and loops through the file contents one character at a time, the values are stored in a CHAR variable called "next".

    Basically I'm wondering how I can take a grade which could be 1 to 3 characters and concatenate it, then in turn convert into an INT so I may produce the average with it.
    Is the "one char at a time" approach a lesson requirement? Because otherwise you're using the wrong approach to the problem. You should use getline() to read a line at a time, then an istringstream to parse the line into parts.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    Quote Originally Posted by CornedBee View Post
    Is the "one char at a time" approach a lesson requirement? Because otherwise you're using the wrong approach to the problem. You should use getline() to read a line at a time, then an istringstream to parse the line into parts.
    It is a lesson requirement. We have yet to learn about getline() and isstringstream.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    =P
    Code:
    istringstream
    and
    Code:
    isstringstream
    should be
    Code:
    stringstream
    recursive typo...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Read in a character. If that character is a number, then that is the current grade.
    Read in another character. If that character is a number, then multiply the previous number by 10 and add the current character.

    Continue until you reach a non-numeric character.

    It might help to know that to get the number from a character that you know is a digit, just subtract the character for zero. So:
    Code:
    char digit = '5';
    int number = digit - '0'; // number is now 5.
    >> istringstream should be stringstream
    Actually, that one was not a typo. An istringstream is a special type of stringstream for input, and would be appropriate for this task if it was allowed for the assignment.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Hey tehprince, since you're already getting help here, how about I delete your thread on Daniweb?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed