| | |
Writing a file input and output program.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
Here is the code:
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:
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?
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
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?
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
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.
C++ Syntax (Toggle Plain Text)
#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(); }
Last edited by stilllearning; Sep 20th, 2008 at 3:46 pm.
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#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(); }
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
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.
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
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?
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?
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?
C++ Syntax (Toggle Plain Text)
#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(); }
Last edited by PaladinHammer; Sep 20th, 2008 at 5:39 pm.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
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.
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
I just tried what you said stilllreaning, and now I'm getting "0"'s for everything. Here's the code:
C++ Syntax (Toggle Plain Text)
#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(); }
![]() |
Similar Threads
- Send data on a serial port (C++)
- fstream Tutorial (C++)
- Function & writing to file problem (Python)
- Need help reading and writing to file (C++)
- Doesn't open for file input successfully.... why? (C++)
- Custom Class - File I/O (C++)
- Help with Java program writing (Java)
- Help! Unknown bug in Writing to file (C)
- file open code in VB (Visual Basic 4 / 5 / 6)
- Linux Shell Script needs help writing program (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: String array problem
- Next Thread: help me with my code please....
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





