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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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