seperating, processing, and assigning variables from input

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?

here's what I have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace 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"
const int 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!
Last edited on
You might try vectors.

http://www.cplusplus.com/reference/vector/vector/

Make two parallel vectors that contain the numbers and totals, then sort the first vector (don't forget to keep the second parallel).
Topic archived. No new replies allowed.