Outputting inputted number from the list of .txt file(FILE HANDLING)

I want to make a store like program.
I have my items in my .txt file, outputted it in my program.
I created a new function for a user to pick a reference number he/she will want to buy.
The problem is it only outputs the last inputted number on the program.

Example:
Type the reference number you want to buy: 1
Type the reference number you want to buy: 2

These are the items you want to buy:
[2] Murdered: Soul Suspect $13.49//this is my problem it should be like this

----------------------------------------------------------
These are the items you want to buy:
[1] Assassin's Creed IV: Black Flag $29.99
[2] Murdered: Soul Suspect $13.49

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  #include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

struct GAMES
{
	char games[100];
	double price;
};

GAMES g[11];
void line(char, int);
void games();
void choose();
void checkout();

int main()
{
	system("COLOR 9");
	cout << "\n\n\n\n\n\n\n";
	line('=', 80);
	cout << "\n";
	cout << "                          Welcome to PS4 Store!";
	cout << "\n     Please press any key to be able to be redirected to the list of games.";
	cout << "\n\n";
	line('=', 80);
	system("pause>0");
	games();
	system("pause>0");
}

void games()
{
	system("cls");
	system("COLOR 3");

	cout << "           Limited Time Offer: Free Game PT for every purchase!\n\n";
	ifstream fin;
	fin.open("c:\\Transaction\\ps4.txt");

	for (int a = 0; a < 10; a++){
		fin.getline(g[a].games, 100);
		fin >> g[a].price;
		fin.ignore();
	}

	cout << setw(5) << "Reference No."
		<< setw(15) << "Games";

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	for (int i = 0; i < 10; i++){
		cout << endl;
		cout << setw(5) << i + 1;
		cout << "                " << g[i].games;
		cout << "  -   $" << g[i].price;
	}
	cout << endl;
	fin.close();
	choose();
}

void choose()
{
	ifstream fin;
	fin.open("c:\\Transaction\\ps4.txt");
	int b, no;
	line('=', 80);
	cout << "How many games do you want to buy? ";
	cin >> b;

	for (int i = 0; i < b; i++)
	{
		line('=', 80);
		cout << "Type the reference number you want to buy: ";
		cin >> no;
	}

	for (int a = 0; a < 10; a++){
		fin.getline(g[a].games, 100);
		fin >> g[a].price;
		fin.ignore();
	}
	
	system("cls");
	system("COLOR B");
	cout << "These are the items you want to buy: \n\n";
	if (no == 1)
		cout << "[1]     " << g[0].games << setw(15) << "$" << g[0].price << endl;
	if (no == 2)
		cout << "[2]     " << g[1].games << setw(15) << "$" << g[1].price << endl;
	if (no == 3)
		cout << "[3]     " << g[2].games << setw(15) << "$" << g[2].price << endl;
	if (no == 4)
		cout << "[4]     " << g[3].games << setw(15) << "$" << g[3].price << endl;
	if (no == 5)
		cout << "[5]     " << g[4].games << setw(15) << "$" << g[4].price << endl;
	if (no == 6)
		cout << "[6]     " << g[5].games << setw(15) << "$" << g[5].price << endl;
	if (no == 7)
		cout << "[7]     " << g[6].games << setw(15) << "$" << g[6].price << endl;
	if (no == 8)
		cout << "[8]     " << g[7].games << setw(15) << "$" << g[7].price << endl;
	if (no == 9)
		cout << "[9]     " << g[8].games << setw(15) << "$" << g[8].price << endl;
	if (no == 10)
		cout << "[10]    " << g[9].games << setw(15) << "$" << g[9].price << endl;

//i wanted to use loop here but if(no == a)<--- a errors
//is there a possible way to fix this and if possible use a for loop?
}

void line(char ch, int ctr)
{
	for (int i = 0; i < ctr; i++) {
		cout << ch;
	}
	cout << endl;
}
Last edited on
You want to build up a list of all of the user selections and then iterate over that list.
Your block of if's are not in a loop, so it's only ever going to print out one number in its current condition.

edit: rough example:
build up a history of selections:

1
2
3
4
5
6
7
8
9
10
11
12
vector<int> gameSelections;
	int choice(0);

	for (int i = 0; i < b; i++)
	{
		line('=', 80);
		cout << "Type the reference number you want to buy: ";
                // read in as normal but also "remember selection by
                // adding to a collection to refer to later.
		cin >> choice;
		gameSelections.push_back(choice);
	}


then iterate over this list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for (int no : gameSelections) {	

		if (no == 1)
			cout << "[1]     " << g[0].games << setw(15) << "$" << g[0].price << endl;
		if (no == 2)
			cout << "[2]     " << g[1].games << setw(15) << "$" << g[1].price << endl;
		if (no == 3)
			cout << "[3]     " << g[2].games << setw(15) << "$" << g[2].price << endl;
		if (no == 4)
			cout << "[4]     " << g[3].games << setw(15) << "$" << g[3].price << endl;
		if (no == 5)
			cout << "[5]     " << g[4].games << setw(15) << "$" << g[4].price << endl;
		if (no == 6)
			cout << "[6]     " << g[5].games << setw(15) << "$" << g[5].price << endl;
		if (no == 7)
			cout << "[7]     " << g[6].games << setw(15) << "$" << g[6].price << endl;
		if (no == 8)
			cout << "[8]     " << g[7].games << setw(15) << "$" << g[7].price << endl;
		if (no == 9)
			cout << "[9]     " << g[8].games << setw(15) << "$" << g[8].price << endl;
		if (no == 10)
			cout << "[10]    " << g[9].games << setw(15) << "$" << g[9].price << endl;
	}
}


you could tidy up those if's a lot. firstly by using if else's or an enum.
Last edited on
Thank you very much! It really helped me a lot! ^^
I have one question. I want to add there the total amount of price below. How do i do that since the price is dynamic depending on the user?
before the for..
 
int totalCost = 0.0;


then in every for condition (which is why these load of if's is not great) do this:

1
2
3
if (no == 1)
			cout << "[1]     " << g[0].games << setw(15) << "$" << g[0].price << endl;
                        totalCost+= g[0].price; // (use whatever index you're at) 
Last edited on
Thank you for the help!
Topic archived. No new replies allowed.