Here is the code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main ()
{
ifstream fin;
ofstream fout;
int item1=0, item2=0, item3=0, item4=0, item5=0, amount;
char item;
fin.open("input.txt");
fout.open("output.txt");
if(fin.fail())
{
cerr << "Input did not open\n";
exit (2);
}
if(fout.fail())
{
cerr << "Output file did not open\n";
exit(2);
}
while (fin >> item >> amount)
{
switch (item)
{
case '1': item1+=amount;break;
case '2': item2+=amount;break;
case '3': item3+=amount;break;
case '4': item4+=amount;break;
case '5': item5+=amount;break;
}
}
fout << "1" << setw(10) << item1 << endl;
fout << "2" << setw(10) << item2 << endl;
fout << "3" << setw(10) << item3 << endl;
fout << "4" << setw(10) << item4 << endl;
fout << "5" << setw(10) << item5 << endl;
cout << "Item 1 count: " << item1 << endl;
cout << "Item 2 count: " << item2 << endl;
cout << "Item 3 count: " << item3 << endl;
cout << "Item 4 count: " << item4 << endl;
cout << "Item 5 count: " << item5 << endl;
cout << "Amount " << amount << endl;
fin.close();
fout.close();
}
If all I wanted to do was get the first colum of the input file to display, I think this is working. BUT, the input file has two colums for its data:
5 395 254.12
4 2043 308.81
2 1362 73.08
1 1344 289.19
4 473 308.81
3 625 36.65
1 1450 289.19
4 727 308.81
3 848 36.65
5 134 254.12
4 998 308.81
2 2215 73.08
1 2235 289.19
3 1814 36.65
5 1589 254.12
1 2326 289.19
1 1027 289.19
4 1523 308.81
3 736 36.65
1 1767 289.19
3 1193 36.65
5 2065 254.12
3 2291 36.65
5 84 254.12
1 1481 289.19
3 1941 36.65
2 1 73.08
3 1350 36.65
5 1372 254.12
1 1927 289.19
2 743 73.08
3 898 36.65
1 1540 289.19
4 2023 308.81
5 2018 254.12
5 1486 254.12
1 1086 289.19
3 2208 36.65
3 2206 36.65
3 1128 36.65
2 1562 73.08
2 178 73.08
4 942 308.81
1 1155 289.19
5 1107 254.12
1 727 289.19
1 534 289.19
1 2177 289.19
1 1846 289.19
2 754 73.08
1 641 289.19
2 1826 73.08
5 124 254.12
5 1471 254.12
3 972 36.65
4 944 308.81
3 124 36.65
2 2154 73.08
2 1816 73.08
2 138 73.08
2 565 73.08
3 1908 36.65
3 922 36.65
1 524 289.19
2 2320 73.08
4 991 308.81
1 1909 289.19
1 1006 289.19
5 1212 254.12
2 2014 73.08
5 1015 254.12
5 485 254.12
2 1226 73.08
2 568 73.08
2 1107 73.08
4 923 308.81
5 1552 254.12
3 1279 36.65
4 2173 308.81
1 1082 289.19
2 1452 73.08
3 962 36.65
4 2179 308.81
4 1244 308.81
5 618 254.12
2 516 73.08
2 442 73.08
2 74 73.08
1 197 289.19
2 1088 73.08
4 74 308.81
4 687 308.81
4 1709 308.81
2 1082 73.08
2 354 73.08
5 1571 254.12
1 538 289.19
3 201 36.65
2 517 73.08
5 1587 254.12
4 1982 308.81
1 2248 289.19
4 317 308.81
3 1494 36.65
5 1831 254.12
4 1931 308.81
4 346 308.81
1 415 289.19
4 1482 308.81
3 361 36.65
5 1406 254.12
2 1052 73.08
3 1210 36.65
2 2271 73.08
4 1236 308.81
5 515 254.12
4 148 308.81
1 154 289.19
3 2076 36.65
2 1206 73.08
The first colum is the ID tag (numbers 1-5). The second is the amount of each ID. The third is the total price of the second colum. So the first row:
There are 395 of "5" costing a total of 254.12 .
The problem is all I can get is the second colum to show up when I start the code. Anyone know whats wrong here?