I'm supposed to input data from a nonspecific text file in the format of
112 45.75
101 38.00
203 124.25
203 30.00
222 65.00
112 74.50
214 97.75
101 87.00
222 102.50
101 21
0
and then produce an output text that looks like
101 (total)
112 (total)
etc...
I'm able to open the input file, I can write to an output file, but the problem I'm having is processing the data piece by piece. I've essentially assigned the first "set" of numbers to a variable y and the doubles I'm supposed to add to a variable z. I've been displaying to the console the added numbers because I know how to format their output. I just don't know how to separate them and add them. I've been attempting to use a sentinel value input with 0 as the value. Should I be using a method other than loops?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
int main ()
{
ifstream a;
ofstream b;
int homeroom; //first number
double money;//second number
double sum; //sum of numbers assigned to the particular "homeroom"
double total; //total of all "money"
constint END = 0; //the sentinel value I think I should use
a.open ("seniors_rule.txt"); //the name of the input file
b.open ("prog5_out_sw1354.txt"); // the name of the output file
sum = 0;
if (!a)
{
b << "Error: File not found.";
return 1;}while (!a.eof())
{
while (a >> homeroom >> money)
while (homeroom == 101)
{sum = sum + money;
cout << sum;
}
}
}
Also, If there is additional info you'd like to see or any suggestions to properly format these questions, please let me know. This is my first time using this forum and I'd really appreciate the feedback.
Thanks!