Weird glitch

Aug 24, 2018 at 9:55am
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!
Aug 24, 2018 at 10:34am
You are mixing getline (which extracts the newline \n character from the stream) with the stream extraction operator >> (which doesn't).

When you do
inFile >> price
on line 72 you will extract the price, but won't move to the next line. So the next getline call will finish this line (with a blank).

There are quite a few ways around this, but if you change it to
inFile >> price >> ws
it will extract any following whitespace (including the newline). See
http://www.cplusplus.com/reference/istream/ws/
Alternately, you could have another getline(infile,dummy) after inFile >> price, where dummy is a sacrificial string variable into which to dump the rest of the line (which is probably empty).

This may happen in the rest of your code, but I haven't investigated.


It would be easier for others to debug if you kept the output simple; putting unnecessary $$$ symbols everywhere confuses things.
Last edited on Aug 24, 2018 at 10:41am
Aug 24, 2018 at 11:00am

Thanks for the help, the reason i use inFile >> price is because price is a double type and getline for item name because it may or may not include space.
Alternately, you could have another getline(infile,dummy) after inFile >> price, where dummy is a sacrificial string variable into which to dump the rest of the line (which is probably empty).

This is something new to me and would use it from now on. Anyway I solved my problem by just adding an ignore function right behind my index++ statement.
index++; inFile.ignore();

putting unnecessary $$$ symbols everywhere confuses things.

I see, it's just an UI I want to make, will removed next time before posting.
Topic archived. No new replies allowed.