Writing a file input and output program.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 25
Reputation: PaladinHammer is an unknown quantity at this point 
Solved Threads: 0
PaladinHammer PaladinHammer is offline Offline
Light Poster

Writing a file input and output program.

 
0
  #1
Sep 20th, 2008
Here is the code:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. ifstream fin;
  9. ofstream fout;
  10. int item1=0, item2=0, item3=0, item4=0, item5=0, amount;
  11. char item;
  12.  
  13. fin.open("input.txt");
  14. fout.open("output.txt");
  15.  
  16. if(fin.fail())
  17. {
  18. cerr << "Input did not open\n";
  19. exit (2);
  20. }
  21. if(fout.fail())
  22. {
  23. cerr << "Output file did not open\n";
  24. exit(2);
  25. }
  26. while (fin >> item >> amount)
  27. {
  28. switch (item)
  29. {
  30. case '1': item1+=amount;break;
  31. case '2': item2+=amount;break;
  32. case '3': item3+=amount;break;
  33. case '4': item4+=amount;break;
  34. case '5': item5+=amount;break;
  35. }
  36. }
  37. fout << "1" << setw(10) << item1 << endl;
  38. fout << "2" << setw(10) << item2 << endl;
  39. fout << "3" << setw(10) << item3 << endl;
  40. fout << "4" << setw(10) << item4 << endl;
  41. fout << "5" << setw(10) << item5 << endl;
  42.  
  43. cout << "Item 1 count: " << item1 << endl;
  44. cout << "Item 2 count: " << item2 << endl;
  45. cout << "Item 3 count: " << item3 << endl;
  46. cout << "Item 4 count: " << item4 << endl;
  47. cout << "Item 5 count: " << item5 << endl;
  48. cout << "Amount " << amount << endl;
  49. fin.close();
  50. fout.close();
  51.  
  52. }

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:

  1. 5 395 254.12
  2. 4 2043 308.81
  3. 2 1362 73.08
  4. 1 1344 289.19
  5. 4 473 308.81
  6. 3 625 36.65
  7. 1 1450 289.19
  8. 4 727 308.81
  9. 3 848 36.65
  10. 5 134 254.12
  11. 4 998 308.81
  12. 2 2215 73.08
  13. 1 2235 289.19
  14. 3 1814 36.65
  15. 5 1589 254.12
  16. 1 2326 289.19
  17. 1 1027 289.19
  18. 4 1523 308.81
  19. 3 736 36.65
  20. 1 1767 289.19
  21. 3 1193 36.65
  22. 5 2065 254.12
  23. 3 2291 36.65
  24. 5 84 254.12
  25. 1 1481 289.19
  26. 3 1941 36.65
  27. 2 1 73.08
  28. 3 1350 36.65
  29. 5 1372 254.12
  30. 1 1927 289.19
  31. 2 743 73.08
  32. 3 898 36.65
  33. 1 1540 289.19
  34. 4 2023 308.81
  35. 5 2018 254.12
  36. 5 1486 254.12
  37. 1 1086 289.19
  38. 3 2208 36.65
  39. 3 2206 36.65
  40. 3 1128 36.65
  41. 2 1562 73.08
  42. 2 178 73.08
  43. 4 942 308.81
  44. 1 1155 289.19
  45. 5 1107 254.12
  46. 1 727 289.19
  47. 1 534 289.19
  48. 1 2177 289.19
  49. 1 1846 289.19
  50. 2 754 73.08
  51. 1 641 289.19
  52. 2 1826 73.08
  53. 5 124 254.12
  54. 5 1471 254.12
  55. 3 972 36.65
  56. 4 944 308.81
  57. 3 124 36.65
  58. 2 2154 73.08
  59. 2 1816 73.08
  60. 2 138 73.08
  61. 2 565 73.08
  62. 3 1908 36.65
  63. 3 922 36.65
  64. 1 524 289.19
  65. 2 2320 73.08
  66. 4 991 308.81
  67. 1 1909 289.19
  68. 1 1006 289.19
  69. 5 1212 254.12
  70. 2 2014 73.08
  71. 5 1015 254.12
  72. 5 485 254.12
  73. 2 1226 73.08
  74. 2 568 73.08
  75. 2 1107 73.08
  76. 4 923 308.81
  77. 5 1552 254.12
  78. 3 1279 36.65
  79. 4 2173 308.81
  80. 1 1082 289.19
  81. 2 1452 73.08
  82. 3 962 36.65
  83. 4 2179 308.81
  84. 4 1244 308.81
  85. 5 618 254.12
  86. 2 516 73.08
  87. 2 442 73.08
  88. 2 74 73.08
  89. 1 197 289.19
  90. 2 1088 73.08
  91. 4 74 308.81
  92. 4 687 308.81
  93. 4 1709 308.81
  94. 2 1082 73.08
  95. 2 354 73.08
  96. 5 1571 254.12
  97. 1 538 289.19
  98. 3 201 36.65
  99. 2 517 73.08
  100. 5 1587 254.12
  101. 4 1982 308.81
  102. 1 2248 289.19
  103. 4 317 308.81
  104. 3 1494 36.65
  105. 5 1831 254.12
  106. 4 1931 308.81
  107. 4 346 308.81
  108. 1 415 289.19
  109. 4 1482 308.81
  110. 3 361 36.65
  111. 5 1406 254.12
  112. 2 1052 73.08
  113. 3 1210 36.65
  114. 2 2271 73.08
  115. 4 1236 308.81
  116. 5 515 254.12
  117. 4 148 308.81
  118. 1 154 289.19
  119. 3 2076 36.65
  120. 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Writing a file input and output program.

 
0
  #2
Sep 20th, 2008
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.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. ifstream fin;
  10. ofstream fout;
  11. float amount, itemPrice[5];
  12. int item, quantity, itemQuantity[5];
  13.  
  14. /* intitalize your arrays */
  15. for (int i = 0; i < 5; i++){
  16. itemPrice[i] = 0;
  17. itemQuantity[i] = 0.0;
  18. }
  19.  
  20. fin.open("data.txt");
  21. fout.open("output.txt");
  22.  
  23. if(fin.fail())
  24. {
  25. cerr << "Input did not open\n";
  26. exit (2);
  27. }
  28. if(fout.fail())
  29. {
  30. cerr << "Output file did not open\n";
  31. exit(2);
  32. }
  33. /* read the input columns and add them to the array */
  34. while (fin >> item >> quantity >> amount)
  35. {
  36. itemQuantity[item -1] += quantity;
  37. itemPrice[item -1] += amount;
  38. }
  39.  
  40. /* write the columns out to another file */
  41. for (int i = 0; i < 5; i++){
  42. fout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  43. }
  44.  
  45. /* write the columns to stdout or the screen */
  46. for (int i = 0; i < 5; i++){
  47. cout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  48. }
  49.  
  50. fin.close();
  51. fout.close();
  52.  
  53. }
Last edited by stilllearning; Sep 20th, 2008 at 3:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: PaladinHammer is an unknown quantity at this point 
Solved Threads: 0
PaladinHammer PaladinHammer is offline Offline
Light Poster

Re: Writing a file input and output program.

 
0
  #3
Sep 20th, 2008
Originally Posted by stilllearning View Post
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.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. ifstream fin;
  10. ofstream fout;
  11. float amount, itemPrice[5];
  12. int item, quantity, itemQuantity[5];
  13.  
  14. /* intitalize your arrays */
  15. for (int i = 0; i < 5; i++){
  16. itemPrice[i] = 0;
  17. itemQuantity[i] = 0.0;
  18. }
  19.  
  20. fin.open("data.txt");
  21. fout.open("output.txt");
  22.  
  23. if(fin.fail())
  24. {
  25. cerr << "Input did not open\n";
  26. exit (2);
  27. }
  28. if(fout.fail())
  29. {
  30. cerr << "Output file did not open\n";
  31. exit(2);
  32. }
  33. /* read the input columns and add them to the array */
  34. while (fin >> item >> quantity >> amount)
  35. {
  36. itemQuantity[item -1] += quantity;
  37. itemPrice[item -1] += amount;
  38. }
  39.  
  40. /* write the columns out to another file */
  41. for (int i = 0; i < 5; i++){
  42. fout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  43. }
  44.  
  45. /* write the columns to stdout or the screen */
  46. for (int i = 0; i < 5; i++){
  47. cout << i+1 << setw(10) << itemQuantity[i] << setw(10) << itemPrice[i] << endl;
  48. }
  49.  
  50. fin.close();
  51. fout.close();
  52.  
  53. }
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Writing a file input and output program.

 
0
  #4
Sep 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: PaladinHammer is an unknown quantity at this point 
Solved Threads: 0
PaladinHammer PaladinHammer is offline Offline
Light Poster

Re: Writing a file input and output program.

 
0
  #5
Sep 20th, 2008
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?

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. ifstream fin;
  9. ofstream fout;
  10. int id1=0, id2=0, id3=0, id4=0, id5=0, unitcost1=0, unitcost2=0, unitcost3=0,
  11. unitcost4=0, unitcost5=0;
  12. char id, unitcost;
  13. float amount, priceamount;
  14.  
  15. fin.open("input.txt");
  16. fout.open("output.txt");
  17.  
  18. if(fin.fail())
  19. {
  20. cerr << "Input did not open\n";
  21. exit (2);
  22. }
  23. if(fout.fail())
  24. {
  25. cerr << "Output file did not open\n";
  26. exit(2);
  27. }
  28. while (fin >> id >> amount >> unitcost >> priceamount )
  29. {
  30. switch (id)
  31. {
  32. case '1': id1+=amount;break;
  33. case '2': id2+=amount;break;
  34. case '3': id3+=amount;break;
  35. case '4': id4+=amount;break;
  36. case '5': id5+=amount;break;
  37. }
  38. switch (unitcost)
  39. {
  40. case '1': unitcost1+=priceamount;break;
  41. case '2': unitcost2+=priceamount;break;
  42. case '3': unitcost3+=priceamount;break;
  43. case '4': unitcost4+=priceamount;break;
  44. case '5': unitcost5+=priceamount;break;
  45. }
  46. }
  47. fout << "1" << setw(10) << id1 << setw(10) << endl;
  48. fout << "2" << setw(10) << id2 << setw(10) << endl;
  49. fout << "3" << setw(10) << id3 << setw(10) << endl;
  50. fout << "4" << setw(10) << id4 << setw(10) << endl;
  51. fout << "5" << setw(10) << id5 << setw(10) << endl;
  52.  
  53. fout << "1" << setw(10) << unitcost1 << setw(10) << endl;
  54. fout << "2" << setw(10) << unitcost2 << setw(10) << endl;
  55. fout << "3" << setw(10) << unitcost3 << setw(10) << endl;
  56. fout << "4" << setw(10) << unitcost4 << setw(10) << endl;
  57. fout << "5" << setw(10) << unitcost5 << setw(10) << endl;
  58.  
  59. cout << "Item 1 count: " << id1 << " total cost of the item: " << unitcost1 << endl;
  60. cout << "Item 2 count: " << id2 << " total cost of the item: " << unitcost2 << endl;
  61. cout << "Item 3 count: " << id3 << " total cost of the item: " << unitcost3 << endl;
  62. cout << "Item 4 count: " << id4 << " total cost of the item: " << unitcost4 << endl;
  63. cout << "Item 5 count: " << id5 << " total cost of the item: " << unitcost5 << endl;
  64.  
  65.  
  66. fin.close();
  67. fout.close();
  68.  
  69. }
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?
Last edited by PaladinHammer; Sep 20th, 2008 at 5:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Writing a file input and output program.

 
0
  #6
Sep 20th, 2008
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.
Last edited by stilllearning; Sep 20th, 2008 at 5:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: PaladinHammer is an unknown quantity at this point 
Solved Threads: 0
PaladinHammer PaladinHammer is offline Offline
Light Poster

Re: Writing a file input and output program.

 
0
  #7
Sep 20th, 2008
I just tried what you said stilllreaning, and now I'm getting "0"'s for everything. Here's the code:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. ifstream fin;
  9. ofstream fout;
  10. int id;
  11. int amount, amount1, amount2, amount3, amount4, amount5;
  12. float unitcost, unitcost1=0.0, unitcost2=0.0, unitcost3=0.0, unitcost4=0.0, unitcost5=0.0;
  13.  
  14.  
  15. fin.open("input.txt");
  16. fout.open("output.txt");
  17.  
  18. if(fin.fail())
  19. {
  20. cerr << "Input did not open\n";
  21. exit (2);
  22. }
  23. if(fout.fail())
  24. {
  25. cerr << "Output file did not open\n";
  26. exit(2);
  27. }
  28. while (fin >> id >> amount >> unitcost )
  29. {
  30. switch (id)
  31. {
  32. case '1': amount1+=amount;
  33. unitcost1+=unitcost;
  34. break;
  35. case '2': amount2+=amount;
  36. unitcost2+=unitcost;
  37. break;
  38. case '3': amount3+=amount;
  39. unitcost3+=unitcost;
  40. break;
  41. case '4': amount4+=amount;
  42. unitcost4+=unitcost;
  43. break;
  44. case '5': amount5+=amount;
  45. unitcost5+=unitcost;
  46. break;
  47. }
  48. }
  49. fout << "1" << setw(10) << amount1 << setw(10) << endl;
  50. fout << "2" << setw(10) << amount2 << setw(10) << endl;
  51. fout << "3" << setw(10) << amount3 << setw(10) << endl;
  52. fout << "4" << setw(10) << amount4 << setw(10) << endl;
  53. fout << "5" << setw(10) << amount5 << setw(10) << endl;
  54.  
  55. fout << "1" << setw(10) << unitcost1 << setw(10) << endl;
  56. fout << "2" << setw(10) << unitcost2 << setw(10) << endl;
  57. fout << "3" << setw(10) << unitcost3 << setw(10) << endl;
  58. fout << "4" << setw(10) << unitcost4 << setw(10) << endl;
  59. fout << "5" << setw(10) << unitcost5 << setw(10) << endl;
  60.  
  61. cout << "Item 1 count: " << amount1 << " total cost of the item(s): $" << unitcost1 << endl;
  62. cout << "Item 2 count: " << amount2 << " total cost of the item(s): $" << unitcost2 << endl;
  63. cout << "Item 3 count: " << amount3 << " total cost of the item(s): $" << unitcost3 << endl;
  64. cout << "Item 4 count: " << amount4 << " total cost of the item(s): $" << unitcost4 << endl;
  65. cout << "Item 5 count: " << amount5 << " total cost of the item(s): $" << unitcost5 << endl;
  66.  
  67. cout << "The average cost of the items is: " << amount/unitcost << endl;
  68.  
  69.  
  70. fin.close();
  71. fout.close();
  72.  
  73. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: PaladinHammer is an unknown quantity at this point 
Solved Threads: 0
PaladinHammer PaladinHammer is offline Offline
Light Poster

Re: Writing a file input and output program.

 
0
  #8
Sep 20th, 2008
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!!!!!!!!!!!!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Writing a file input and output program.

 
0
  #9
Sep 21st, 2008
lol .. congratulations
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC