I am stupefied when I came across this weird glitch. It is a fstream project.
Writing data to text file works fine, but when I read the file, no errors occured instead it gave me this obnoxious glitch. Enter 1 for writing into a file and 2 to read it. Help me see if its my pc malfunctioning which I think thats not the case, had tested it in VS 2017 and Dev-C++ and render the same outcome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
void record_data();
void menu();
void module(short xchoice);
void read_data();
int main()
{
menu();
system("pause");
return 0;
}
void menu() {
cout << "[][][][][][][][][][][][][][]\n";
cout << "[][][] \n";
cout << "[][] \n";
cout << "[] Main Menu \n";
cout << "[][] \n";
cout << "[][][] \n";
cout << "[][][][][][][][][][][][][][]\n";
cout << "Enter your choice: "; short choice; cin >> choice;
module(choice);
}
void module(short xchoice) {
switch (xchoice) {
case 1:
record_data();
break;
case 2:
read_data();
break;
}
}
void record_data() {
ofstream outFile("records.txt", ios::app);
int day, month, year; double price;
string item; char choice;
cout << "Please enter date. ";
cout << "Day: "; cin >> day; cout << "Month: "; cin >> month; cout << "Year: "; cin >> year;
do {
cin.ignore();
outFile << day << '-' << month << '-' << year << ',';
cout << "Name: "; getline(cin, item);
cout << "Price: "; cin >> price;
outFile << item << '*' << price << endl;
cin.ignore();
cout << "To add another item, enter 'Y'\n";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
outFile.close();
cout << "End of input..Hit enter to continue. . .";
cin.ignore();
cin.get();
menu();
}
void read_data() {
ifstream inFile("records.txt");
double price; int index = 0;
string item, date;
cout << left;
cout << "History\n\n";
cout << "\t$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout << "\t$$ $ $ $ $$\n";
cout << "\t$$ Index $ Date $ Item $ Price $$\n";
cout << "\t$$ $ $ $ $$\n";
cout << "\t$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
while (getline(inFile, date, ','), getline(inFile, item, '*'), inFile >> price) {
index++;
cout << "\t$$ " << setw(10) << index << setw(15) << date << setw(15) << item << setw(15) << fixed << setprecision(2) << price << " $$\n";
}
inFile.close();
cout << "End of file\n";
cin.ignore();
cin.get();
menu();
}
|
Note: <tab> is used to seperate variables
When I read from the file, the output should be: (briefly describing)
Index<tab>Date<tab>Item<tab>Price
1<tab>3-6-2001<tab>Potato<tab>5.30
2<tab>3-6-2001<tab>Tomato<tab>4.30
3<tab>4-6-2001<tab>Nugget<tab>2.00
Instead it gave me this:
Index<tab>Date<tab>Item<tab>Price
1<tab>3-6-2001<tab>Potato<tab>5.30
2<tab>
3-6-2001<tab>Tomato<tab>4.30
3<tab>
4-6-2001<tab>Nugget<tab>2.00
I am very frustrated right now, any help will be appreciated!