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?

Recommended Answers

All 8 Replies

You need an array that will store both your total quantity and the corresponding total price for that quantity.

You have 5 items in all and each item has a total quantity and a total price.

Edit:

My mistake earlier.

Your qty is an int and your price is a float, so you can use two arrays one to store the total qty and one to store the corresponding price.

int itemQty[5];
float itemPrice[5];

and read in fptr >> item >> quantity >> price;

Here is a simpler implementation. In such cases as yours its much easier to use arrays and loops.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{
  ifstream fin;
  ofstream fout;
  float amount, itemPrice[5];
  int   item, quantity, itemQuantity[5]; 

  /* intitalize your arrays */
  for (int i = 0; i < 5; i++){
    itemPrice[i]    = 0;
    itemQuantity[i] = 0.0;
  }

  fin.open("data.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);
  }
  /* read the input columns and add them to the array */
  while (fin >> item >> quantity >> amount)
  {
    itemQuantity[item -1] += quantity;
    itemPrice[item -1]    += amount;
  }

  /* write the columns out to another file */
  for (int i = 0; i < 5; i++){
    fout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  }

 /* write the columns to stdout or the screen */ 
  for (int i = 0; i < 5; i++){
    cout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  }

  fin.close();
  fout.close();

}

You need an array that will store both your total quantity and the corresponding total price for that quantity.

You have 5 items in all and each item has a total quantity and a total price.

Edit:

My mistake earlier.

Your qty is an int and your price is a float, so you can use two arrays one to store the total qty and one to store the corresponding price.

int itemQty[5];
float itemPrice[5];

and read in fptr >> item >> quantity >> price;

Here is a simpler implementation. In such cases as yours its much easier to use arrays and loops.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{
  ifstream fin;
  ofstream fout;
  float amount, itemPrice[5];
  int   item, quantity, itemQuantity[5]; 

  /* intitalize your arrays */
  for (int i = 0; i < 5; i++){
    itemPrice[i]    = 0;
    itemQuantity[i] = 0.0;
  }

  fin.open("data.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);
  }
  /* read the input columns and add them to the array */
  while (fin >> item >> quantity >> amount)
  {
    itemQuantity[item -1] += quantity;
    itemPrice[item -1]    += amount;
  }

  /* write the columns out to another file */
  for (int i = 0; i < 5; i++){
    fout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  }

 /* write the columns to stdout or the screen */ 
  for (int i = 0; i < 5; i++){
    cout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  }

  fin.close();
  fout.close();

}

Hey thanks for the reply still learning. Your coding does work! Problem is I have to use a switch for it, hence why I put one in there. I'm going to screw around with it for awhile, but if you know a way to use the a switch with this, I'd greatly appreciate it.

Well you could add a switch ... but it would just mean doing redundant stuff ...

switch(item){
case 1:
itemQuantity[item -1] += quantity;
itemPrice[item -1] += amount;
case 2:
itemQuantity[item -1] += quantity;
itemPrice[item -1] += amount;
case 3:
.....

}

If you are familiar with functions and passing arguments, then you could write a function to do the additions and call it for each switch case, again not an advisable scenario.

If you are also not supposed to use an array, then you will need to use 5 int and 5 float variables to store the data corresponding to each item.

The problem I'm having is I've never seen an array before. I haven't the slightest idea of what I'm looking at, I only know that it works.

Thats why I'm using switches. The professor asked for them, and I know how to use them, but only when it comes to a two colum input file. The third colum is whats killing me.

EDIT: Below is the new code. With the exception of Colums 1, 3, and 5, everything else works. Why?

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main ()
{
    ifstream fin;
    ofstream fout;
    int id1=0, id2=0, id3=0, id4=0, id5=0, unitcost1=0, unitcost2=0, unitcost3=0,
    unitcost4=0, unitcost5=0;
    char id, unitcost;
    float amount, priceamount;

    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 >> id >> amount >> unitcost >> priceamount )
{
    switch (id)
    {
    case '1': id1+=amount;break;
    case '2': id2+=amount;break;
    case '3': id3+=amount;break;
    case '4': id4+=amount;break;
    case '5': id5+=amount;break;
    }
    switch (unitcost)
    {
    case '1': unitcost1+=priceamount;break;
    case '2': unitcost2+=priceamount;break;
    case '3': unitcost3+=priceamount;break;
    case '4': unitcost4+=priceamount;break;
    case '5': unitcost5+=priceamount;break;
    }
}
    fout << "1" << setw(10) << id1 << setw(10) << endl;
    fout << "2" << setw(10) << id2 << setw(10) << endl;
    fout << "3" << setw(10) << id3 << setw(10) << endl;
    fout << "4" << setw(10) << id4 << setw(10) << endl;
    fout << "5" << setw(10) << id5 << setw(10) << endl;

    fout << "1" << setw(10) << unitcost1 << setw(10) << endl;
    fout << "2" << setw(10) << unitcost2 << setw(10) << endl;
    fout << "3" << setw(10) << unitcost3 << setw(10) << endl;
    fout << "4" << setw(10) << unitcost4 << setw(10) << endl;
    fout << "5" << setw(10) << unitcost5 << setw(10) << endl;

    cout << "Item 1 count: " << id1 << " total cost of the item: " << unitcost1 << endl;
    cout << "Item 2 count: " << id2 << " total cost of the item: " << unitcost2 << endl;
    cout << "Item 3 count: " << id3 << " total cost of the item: " << unitcost3 << endl;
    cout << "Item 4 count: " << id4 << " total cost of the item: " << unitcost4 << endl;
    cout << "Item 5 count: " << id5 << " total cost of the item: " << unitcost5 << endl;
    

    fin.close();
    fout.close();

}

Is successful in showing the total amount of items adding together colum 2 and listing it as colum 1. But, only numbers 2 and 3 work for colum 3. Why?

If you have never seen an array before, let me try to explain it differently:

These could be your input variables. Your item is an integer, your quantity is an integer and your amount is a float. Initialize the variables where you want to store the totals so that they get summed up correctly.

int item;
int qty, qty1 = 0, qty2 = 0, qty3 = 0, qty4 = 0, qty5 = 0;
float amt, amt1 = 0.0, amt2 = 0.0, amt3 = 0.0, amt4 = 0.0 , amt5 =0.0;

Now when you read the data from the file, you can read in all three columns using

fin >> item >> qty >> amt ;

then for example let us take case number for item 1.

case 1:
qty1 += qty; /* stores total quantity for item 1 */
amt1 += amt; /* stores total amount for item 1 */
break;

and similarly for the rest of the cases.

At the end of the file read loop, you should have the total quantity and total price for item 1 in qty1 and amt1 respectively

then you can print it out, or write it to your output file.

I just tried what you said stilllreaning, and now I'm getting "0"'s for everything. Here's the code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main ()
{
    ifstream fin;
    ofstream fout;
    int id;
    int amount, amount1, amount2, amount3, amount4, amount5;
    float unitcost, unitcost1=0.0, unitcost2=0.0, unitcost3=0.0, unitcost4=0.0, unitcost5=0.0;


    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 >> id >> amount >> unitcost )
{
    switch (id)
    {
    case '1': amount1+=amount;
              unitcost1+=unitcost;
              break;
    case '2': amount2+=amount;
              unitcost2+=unitcost;
              break;
    case '3': amount3+=amount;
              unitcost3+=unitcost;
              break;
    case '4': amount4+=amount;
              unitcost4+=unitcost;
              break;
    case '5': amount5+=amount;
              unitcost5+=unitcost;
              break;
    }
}
    fout << "1" << setw(10) << amount1 << setw(10) << endl;
    fout << "2" << setw(10) << amount2 << setw(10) << endl;
    fout << "3" << setw(10) << amount3 << setw(10) << endl;
    fout << "4" << setw(10) << amount4 << setw(10) << endl;
    fout << "5" << setw(10) << amount5 << setw(10) << endl;

    fout << "1" << setw(10) << unitcost1 << setw(10) << endl;
    fout << "2" << setw(10) << unitcost2 << setw(10) << endl;
    fout << "3" << setw(10) << unitcost3 << setw(10) << endl;
    fout << "4" << setw(10) << unitcost4 << setw(10) << endl;
    fout << "5" << setw(10) << unitcost5 << setw(10) << endl;

    cout << "Item 1 count: " << amount1 << " total cost of the item(s): $" << unitcost1 << endl;
    cout << "Item 2 count: " << amount2 << " total cost of the item(s): $" << unitcost2 << endl;
    cout << "Item 3 count: " << amount3 << " total cost of the item(s): $" << unitcost3 << endl;
    cout << "Item 4 count: " << amount4 << " total cost of the item(s): $" << unitcost4 << endl;
    cout << "Item 5 count: " << amount5 << " total cost of the item(s): $" << unitcost5 << endl;

    cout << "The average cost of the items is: " << amount/unitcost << endl;
    

    fin.close();
    fout.close();

}

I GOT IT! I FREAKING GOT IT!!!! ALL I HAD TO DO WAS PLACE "id" AS A "char" AND THE DAMN THING WORKS! IT FREAKING WORKS!!!!

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYEEEEEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSS!!!!!!!!!!!!!!!!!!!!!!

lol .. congratulations :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.