Read Multiple Lines And ADD.

Hello,

I am trying to read the input file that has both Variables associated with integers. All I need to do is add the variables together. Basically I got the input and output file down, just confused with how to add the integers that are associated with different groups, like group A, group B, and so on.... Group E should be the total of all those numbers. I am not getting compiling errors.

The inputfile text name is in the program, and the input files consists of this:
14
C 12.50
B 31.75
A 12.00
C 43.75
E 28.50
D 48.25
E 33.50
A 29.50
B 51.00
D 46.75
C 19.50
E 54.75
B 45.50
D 15.25

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 #include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{
    char team;

    int team_count;

    double sum,
           total;

    ifstream fin;
    ofstream fout;

    fin.open("BTD_input.txt");
    fout.open("prog5_out_cav89.txt");

    if (!fin)
    {
        cout << "Input file open failure\n";
        return 1;
    }
    if (!fout)
    {
        cout << "Output file open failure\n";
        return 2;
    }


    fin >> team >> team_count;
    sum = 0;
    for (int i = 1; i <= team_count; i++)
    {
        fin >> team;
        sum = sum + team_count;
    }

    fout << "Beta Tau Delta\n\n"
         << "Team A: $" << sum << "\n";


    fin >> team >> team_count;
    for (int i = 1; i <= team_count; i++)
    {
        fin >> team;
        sum = sum + team_count;
    }

    fout << "Team B: $" << sum << "\n";

    fin >> team >> team_count;
    for (int i = 1; i <= team_count; i++)
    {
        fin >> team;
        sum = sum + team_count;
    }

    fout << "Team C: $" << sum << "\n";

    fin >> team >> team_count;
    for (int i = 1; i <= team_count; i++)
    {
        fin >> team;
        sum = sum + team_count;
    }

    fout << "Team D: $" << sum << "\n";

    fin >> team >> team_count;
    for (int i = 1; i <= team_count; i++)
    {
        fin >> team;
        sum = sum + team_count;
    }

    total = sum + team_count;

    fout << "Team E: $" << sum << "\n"
         << "Total Sales $" << total;


    fin.close();
    fout.close();
    return 0;
}
So your final code should look much shorter.
I've made sort of a skeleton here for you:
http://ideone.com/Da77xu
I get what it is supposed to do, but the number 14 is irrelevant at the beginning and messes everything up.
closed account (E0p9LyTq)
The number 14 is the number of valid data lines to follow.

Your program read the first line, 14, and then your program logic knows how many additional lines to read.
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{
    ifstream fin;
    ofstream fout;

  fin.open("BTD_input.txt");
    fout.open("prog5_out_cav89.txt");

    if (!fin)
    {
        cout << "Input file open failure\n";
        return 1;
    }
    if (!fout)
    {
        cout << "Output file open failure\n";
        return 2;
    }

    char team;

    int num_lines, sum = 0;
    fin >> num_lines;

    double A=0,B=0,C=0,D=0,E=0,
         total;

    for(int i=0; i<num_lines; ++i) {
		char team;
		double score;
		fin >> team >> score;

switch (team)
case 'A':
{

    for (int i = 0; i < num_lines; i++)
    {
        char A;
        double score;
        fin >> A >> score;
        sum = sum + score;

    }

    fout << "Beta Tau Delta\n\n"
         << "Team A: $" << sum << "\n";
}


    fin >> team >> num_lines;
    for (int i = 1; i <= num_lines; i++)
    {
        fin >> team;
        sum = sum + num_lines;
    }

    fout << "Team B: $" << sum << "\n";

    fin >> team >> num_lines;
    for (int i = 1; i <= num_lines; i++)
    {
        fin >> team;
        sum = sum + num_lines;
    }

    fout << "Team C: $" << sum << "\n";

    fin >> team >> num_lines;
    for (int i = 1; i <= num_lines; i++)
    {
        fin >> team;
        sum = sum + num_lines;
    }

    fout << "Team D: $" << sum << "\n";

    fin >> team >> num_lines;
    for (int i = 1; i <= num_lines; i++)
    {
        fin >> team;
        sum = sum + num_lines;
    }

    total = sum + num_lines;

    fout << "Team E: $" << sum << "\n"
         << "Total Sales $" << total;


    fin.close();
    fout.close();
    return 0;
}
}
I am having trouble adding team A
I would highly suggest writing out how you would do this on paper and compare that to the algorithm you're currently using (which is wrong.)

Chances are you'll end up with something similar to the link provided by kevinkjt2000 (which you seem to have conveniently ignored in your 'revised' code.)
Okay so imagine there is a basket of apples (fin) and every time you take an apple (fin >>) there will be one less in the basket. In your case you are taking too many apples, and everyone knows that having negative apples is bad (that ruins the whole basket).

Correct Pseudocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
read num_lines
for i in 1 to num_lines
    read team_character
    read score
    if team_character = 'A'
        A_score <- A_score + score
    if team_character = 'B'
        B_score <- B_score + score
    ...

print 'A score: ', A_score
print 'B score: ', B_score
...
print 'Total score: ', A_score + B_score + ...


me wrote:
So your final code should look much shorter.

When I said this earlier, I meant it. Somehow, you came back with code that was even longer than your original post. I am saying this again so that you do not over think the solution. You are trying too hard by writing too much code.
Last edited on
Thank you, that made sense to me!!
I got A to work, but why is B,C,and D giving me zeros?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{

    char team;

    double score = 0, sum = 0,
    A_score = 0, B_score = 0, C_score = 0, D_score = 0;

        ifstream fin;
    ofstream fout;

  fin.open("BTD_input.txt");
    fout.open("prog5_out_cav89.txt");

    if (!fin)
    {
        cout << "Input file open failure\n";
        return 1;
    }
    if (!fout)
    {
        cout << "Output file open failure\n";
        return 2;
    }


{

    while(fin >> team >> score) {
        if (team == 'A') {
            A_score = sum += score;
        }
    }

    fout << "Beta Tau Delta\n\n"
         << "Team A: $" << A_score << "\n";
}

   while(fin >> team >> score) {
        if (team == 'B') {
            B_score = sum += score;
        }
    }

    fout << "Team B: $" << B_score << "\n";


   while(fin >> team >> score) {
        if (team == 'C') {
           C_score = sum += score;
        }
    }

    fout << "Team C: $" << C_score << "\n";


    while(fin >> team >> score) {
        if (team == 'D') {
           D_score = sum += score;
        }
    }

    fout << "Team D: $" << D_score << "\n";



    fin.close();
    fout.close();
    return 0;
}

Last edited on
Nvm finished it, thank you for your help!!
Topic archived. No new replies allowed.