Assume that the following data is stored in file “data.txt”:

2 5 7 1 12 -999

1 4 11 55 66 3 43 23 -999

9 87 56 34 22 12 21 -999

8 7 5 4 6 111 -999

The number -999 at the end of each line acts as a sentinel value and is not part of the data. Write a C++ program that reads these numbers from a file of unspecified length, calculates and displays the sum of all numbers in each line.

Dude, I'm not against helping you, but you need to read our rules:

Keep It Organized

  • Do provide evidence of having done some work yourself if posting questions from school or work assignments

If you continue to post nothing but homework questions without any evidence of thought or effort on your part, I'm going to start deleting.

Void main()
{
Int x, y;
Ifstream infile;
Ofstream outfile;
Infile.open(“input.txt”);
Outfile.open(“output.txt”);
If(!infile.is_open)
Cout<<”I can’t srr the file”;
Els
{
While (!infile.eof())
{
Infile>>x>>y;
Outfile<<x<<”+”<<y<<”=”<<x+y;
}
Infile.close();
Outfile.close();
System(“pause”);
}

this is the idea i have for this que. i only know how to add two numbers. i don't know how to do for 4 lines. plz help! :)

Don't need arrays.

Set *total* to zero
Read a number
if not -999 
    Add it to *total* 
  else 
    print *total*

And format your code.

Edited 4 Years Ago by WaltP

i m new to programming and arrays are totaly new because i am only good in normal programs not arrays so can u help and tell the full program ans!! i have 2 give it tomrw so if u can then thanks

We are not a homework cheat site. You have been given the steps needed to process one line. So process one line.

Also, read the member rules again. Note carefully the rule that states
Do provide evidence of having done some work yourself if posting questions from school or work assignments

Edited 4 Years Ago by WaltP

#include <iostream>
#include <fstream>
using namespace std;

void main()
{
    ifstream infile;
    ofstream outfile;
    infile.open("input.txt");
    outfile.open("output.txt");
    if(!infile.is_open)
        cout<<"i can not see the file";
    else
    {
        while(!infile.eof())
        {
            Set *total* to zero
            Read a number
            if not -999
                Add it to *total* 
            else
            print *total*
        }
        infile.close();
        outfile.close();
        system("pause");
    }
}

i m getting error while putting the code

while(!infile.eof())

can u tel where i m wrong

That completely depends on the error. Since you didn't bother to tell us what the error is, we have no idea.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool load_files()
{
    infile.open("input.txt");
    if (!infile.is_open)
        return false;
    outfile.open("output.txt");
    if(!infile.is_open)
        return false;

    return true;
}

void main()
{
    int y = 0;
    int x;
    int z = 0;
    ifstream infile;
    ofstream outfile;
    if (load_files() == false)
        return 1;
    if(!infile.is_open)
        cout<<"The input file cannot be located.";
    else
    {
        while(!infile.eof())
        {
            while (z == 0)
            {
                infile << x;
                if (x == -999)
                {
                    x = 0;
                    z = 1;
                }
                y += x;
                x = 0;
            }
        }
        outfile >> x;
        infile.close();
        outfile.close();
        system("pause");
    }

    return 0;
}

I might have mixed up the "<<" ">>" when dealing with the fstream.
Comments
We do NOT write answers for people, we help them write their own...
This question has already been answered. Start a new discussion instead.