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;
}
|