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
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace 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;
}
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace 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 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.