Thread: series of lines in text Get the Integer and Sum

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    series of lines in text Get the Integer and Sum

    Instructions : Read a series of lines from a text file, get the numeric values in each line, perform addition and display sum

    and this is my code

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        int sum = 0;
        char x[50];
        double d;
        
        ifstream inFile;
        inFile.open("input.txt");
        
        while(inFile.getline(x,50)) {
        cout<<x<<endl;
        d=atof(x);
        sum+=d;
                cout<< sum << endl;
                } 
        }
        
        system("pause");
    }
    and the text from the file is

    hello51.20world10.00again
    40programming
    file 20editsearch50 project.15

    the output should be

    hello51.20world10.00again
    61.2

    40programming
    40

    file 20editsearch50 project.15
    70.15

    can u help me? just give me the algorithm to sum and the string function to use
    Last edited by [Student]; 08-28-2011 at 09:05 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is not "a" string function to use. You'll probably have to go through your string, character-by-character, looking for characters that are digits. Then when you find one, you'll have to somehow get that number out of there and add it to your total. (You should also probably set total to zero before you start doing this.)

  3. #3
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    can u give me a sample what you mean? plz

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't start atof at the beginning of the string (necessarily). You have to find your number first -- you need to go through the characters checking whether each character is a digit in order to know where you should call atof.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by tabstop View Post
    You can't start atof at the beginning of the string (necessarily). You have to find your number first -- you need to go through the characters checking whether each character is a digit in order to know where you should call atof.
    Maybe, stringstreams would be simpler.
    ...Ignore when a character is there.
    ...Add to the sum when a double is there.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Can you use external libraries ? Maybe I'm too damn lazy but I see an application for boost::tokenizer there.

  7. #7
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    Quote Originally Posted by Alexandre View Post
    Can you use external libraries ? Maybe I'm too damn lazy but I see an application for boost::tokenizer there.
    where? need help

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I don't think a student is meant to be messing with boost at this stage.

    cctype (ctype.h) - C++ Reference
    Perhaps more a case of using isalpha() and isnum() to test each char (in a loop) and go from there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read lines of text
    By Cevris in forum C Programming
    Replies: 8
    Last Post: 05-12-2011, 07:19 AM
  2. Reading lines of a text file
    By george7378 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2011, 08:39 AM
  3. Alphabetizing Lines of Text
    By Adrian in forum C Programming
    Replies: 5
    Last Post: 12-03-2007, 08:28 PM
  4. lines of a text file
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 11-06-2001, 07:09 AM
  5. Text Mode to more than 25 lines
    By karsch1 in forum C Programming
    Replies: 3
    Last Post: 08-31-2001, 01:21 PM