Files into arrays

Pages: 12
Hello everyone,

I have a file which stores tickets information (Ticket number, ticket's holder id, ticket's holder name, ticket's holder phone number).
Now my question is how do i get these information into an array, i am asked to print out customer purchase after the user enters his ID ( of course he can have multiple tickets registered to his ID).
So how i do exactly do that.
Thanks in advance.
closed account (SECMoG1T)
you can create a structure to store a customer info :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct customer
{
    int           ticket_number;
    std::string   id; ///chose the best type int or string
    std::string   name;
    std::string  phone_num;

    customer(int tick_num, const std::string& id_, const std::string& name_, const std::string& phn_num)
   :ticket_number(tick_num),id(id_),name(name_),phone_number(phn_num){}
};

///how to create an entry
int tck_num = 12333;
std::string id("KDX234F"), name("john"),phone("0663782");
customer cust(tck_num,id,name,phone);

///create an array
const int SIZE = /* your desired size here */;
customer records[SIZE];

//load your file into the array 
Last edited on
Thank you,
But how do i exactly load my file into the array?
closed account (SECMoG1T)
use a while loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::ifstream in_file(/*your file path here*/);

///check if the file opened successfully and handle any errors

///assuming  (Ticket number, ticket's holder id, ticket's holder name, ticket's holder phone number). 
///are store per line in the file

int number;
std::string id,name,phone;

while(in_file>>number>>id>>name>>phone)
{
    ///create a customer entry and store the above info
    ///store the entry in the array.
}
Thank you again,
So how do i create a customer entry? (we don't have structures in our course and i basically didn't understand a thing of it).

i am so sorry for being a complete noob and probably troubling you.
closed account (SECMoG1T)
what have you covered in your course so far, need to know so i won't be misleading you.

strings?, arrays?, classes? ....... ??????
Last edited on
we basically only covered, files, functions, arrays (only 1D arrays), loops, if statements. thats about everything i guess.
also, i can post my code if that can help, although its kind of messy.
Last edited on
Ah,
strings of course, arrays yes, classes no i dont think so
closed account (SECMoG1T)
yea post you code.
Well let me post what is asked exactly first:

1. Search for the customer id in the records of the “ticket.txt” file. [ Hint: you can
first load the records into arrays].
2. If the customer id is NOT found, inform the employee about this. Then go back
to the main menu.
3. Otherwise, display all tickets sold for this customer.

ofcourse there is other parts of the project but this is the question where im kind of stuck so the code contains more things
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
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
///////////// Global Variables ///////////////////
int PlatinumTnum=0, GoldTnum=50, SilverTnum=200, BronzeTnum=400; //Tnum stands for Ticket number and all of these variables are counters.
int TC, UNticketnum, UNid, UNphone; // the prefix UN stands for Unassigned and we use that to read from the file, then we assign the stored number to the variables mentioned above.
string UNname;
ifstream in;
ofstream out;
int OneMore;
////////////////////////////////////////////////////////////////
// Prototypes:
void IssueNewTicket();
void tickets_stats();
int income_stats();
int random_winner();
////////////////////////////////////////////////
int main(){
	string name;
	int ID, phone;
	in.open("tickets.txt", ios::app);
	out.open("tickets.txt", ios::app);
    char entry;
	int TC;
    do{
        cout<<"The main menu:\n a. Issue a new ticket\n b. Cancel ticket reservation\n c. Print out customer purchase\n d. Show income statistics\n e. Choose random winner\n f. Exit\n";
        cin>>entry;
    do{
		 
		 if(entry=='a' || entry=='A'){
			 	// a while loop to read from tickets.txt in order to know the last ticket unique number of each category
	tickets_stats();
   cout<<"Choose a ticket category: \n 1. Platinum\n 2. Gold\n 3. Silver\n 4. Bronze\n";
		cin>>TC; //TC stands for Ticktet Category
		switch(TC){
			case 1:{
				if (PlatinumTnum<50)
				{PlatinumTnum+=1;
				cout<<"Please enter the customer's first name, ID and phone number consecutively.\n\n";
				cout<<"ticket ID is:  "<<PlatinumTnum<<"\n";
				cin>>name>>ID>>phone;
				out<<PlatinumTnum<<"\t"<<name<<"\t"<<ID<<"\t"<<phone<<"\n"; }
				else cout<<"Sorry, There is no more bookable Platinum tickets\n\n";
	IssueNewTicket();//calling a function that asks if they user wants to book a new/different ticket.
																																break;}
			case 2: {
				if (GoldTnum<200)
				{GoldTnum+=1;
				cout<<"Please enter the customer's first name, ID and phone number consecutively.\n\n";
				cout<<"ticket ID is:  "<<GoldTnum<<"\n";
				cin>>name>>ID>>phone;
				out<<GoldTnum<<"\t"<<name<<"\t"<<ID<<"\t"<<phone<<"\n"; }
				else cout<<"Sorry, There is no more bookable Gold tickets\n\n";
	IssueNewTicket();//calling a function that asks if they user wants to book a new/different ticket.
																																break;}
				case 3: {
				if (SilverTnum<400)
				{SilverTnum+=1;
				cout<<"Please enter the customer's first name, ID and phone number consecutively.\n\n";
				cout<<"ticket ID is:  "<<SilverTnum<<"\n";
				cin>>name>>ID>>phone;
				out<<SilverTnum<<"\t"<<name<<"\t"<<ID<<"\t"<<phone<<"\n"; }
				else cout<<"Sorry, There is no more bookable Gold tickets\n\n";
	IssueNewTicket();//calling a function that asks if they user wants to book a new/different ticket.
																																break;}
				case 4: {
				if (BronzeTnum<1000)
				{BronzeTnum+=1;
				cout<<"Please enter the customer's first name, ID and phone number consecutively.\n\n";
				cout<<"ticket ID is:  "<<BronzeTnum<<"\n";
				cin>>name>>ID>>phone;
				out<<BronzeTnum<<"\t"<<name<<"\t"<<ID<<"\t"<<phone<<"\n"; }
				else cout<<"Sorry, There is no more bookable Gold tickets\n\n";
	IssueNewTicket();//calling a function that asks if they user wants to book a new/different ticket.
																																break;}
    }

	}
		 if(entry=='d' || entry=='D'){
			 cout<<"\t\tTotal income is:\t"<<income_stats()<<" QR"<<endl;
			 cout<<"Detailed statistics: "<<endl;
			 cout<<PlatinumTnum<<" Platinum tickets were sold (x250 QR)\n"<<GoldTnum-50<<" Gold tickets were sold (x200 QR)\n"<<(SilverTnum-200)<<" Silver tickets were sold (x150 QR)\n"<<(BronzeTnum-400)<<" Bronze tickets were sold (x50 QR)\n"<<"Total income is "<<income_stats()<<" QR\n\n";
		 }
		 if(entry=='e' || entry=='E') {
		 if(random_winner()!=0)
		 cout<<"the lucky winner is the person with the ticket id number: "<<random_winner()<<endl;
		 else cout<<"the chosen random number did not match any sold ticket number, try again.";}
	if (TC!=1 && TC!=2 && TC!=3 && TC!=4) cout<<"Invalid choise, please choose again";}while(TC!=1 &&TC!=2 && TC!=3 && TC!=4 || OneMore==1);
	}
	while(entry != 'f' && entry != 'F');

	return 0;}


void IssueNewTicket(){
	cout<<"Do you want to book a new or a different ticket?\n1. Yes\n2. No\n";
	cin>>OneMore;}
void tickets_stats(){
	int total_income;
	while(in>>UNticketnum>>UNname>>UNid>>UNphone){ 

		if (UNticketnum<=50) PlatinumTnum+=1;
	   else if (UNticketnum>50 && UNticketnum<=200) GoldTnum+=1;
	   else if (UNticketnum>200 && UNticketnum<=400) SilverTnum+=1;
	   else if (UNticketnum>400 && UNticketnum<=1000) BronzeTnum+=1;}
}


int income_stats(){
	int total_income;
	while(in>>UNticketnum>>UNname>>UNid>>UNphone){ 

		if (UNticketnum<=50) PlatinumTnum+=1;
	   else if (UNticketnum>50 && UNticketnum<=200) GoldTnum+=1;
	   else if (UNticketnum>200 && UNticketnum<=400) SilverTnum+=1;
	   else if (UNticketnum>400 && UNticketnum<=1000) BronzeTnum+=1;}

	total_income = (PlatinumTnum*250) + ((GoldTnum-50)*200) + ((SilverTnum-200)*150) + ((BronzeTnum-400)*50);
		return total_income;}

int random_winner(){
	int min=1, max=1000;
	string winner_info;
	srand(time(0));
	int ticket_number_winner = 1+(rand()%1000);
	while(in>>UNticketnum>>UNname>>UNid>>UNphone){
	if(ticket_number_winner==UNticketnum)
	return ticket_number_winner;
	else return 0;}}
Yeah, it is a mess.
closed account (SECMoG1T)
you can also store the customer data in four arrays, if yuo can't use classes/structs
so here a customers info would be spread uot in all four in the same index;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int         number_array[SIZE]{};
std::string id_array[SIZE]{};
std::string name_array[SIZE]{};
std::string phone_array[SIZE]{};

int index = 0;

while(in_file>>number>>id>>name>>phone)
{
    number_array[index] = number;
    id_array[index]     = id;
    name_array[index]   = name;
    phone_array[index]  = phone;

    ++index;
}
well i made the code like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 string number;
			 string id;
		

		     int number_array[100]={};
			std::string id_array[100]={};
			std::string name_array[100]={};
			std::string phone_array[100]={};

			int index = 0;

			while(in>>number>>id>>name>>phone)
		{
			number_array[index] = number; //
			 id_array[index]     = id;
			 name_array[index]   = name;
			 phone_array[index]  = phone;
			
		 ++index;
}

Last edited on
closed account (SECMoG1T)
mine was just an example , you should use the type compatible with your entries

1
2
///from your main number is an int
                         int number; ///change to int 

closed account (SECMoG1T)
you have lots of errors, i'll try see if i can help you correct them.
-globals
-you forgot to check if your file opened correctly
-issueNewTicket doesn't do anything
-store the data in arrays and read it from there.

are you allowed to add functions in there?
Last edited on
yeah i know
but that basically loads the files records into arrays right?
uhh it does it for everyline each right?
like: 1 john id phone, it stores each one of those in a different array right? but all have the same index number, like name[1] is john?
That would be really appreciated thank you
closed account (SECMoG1T)
sure, are you allowed to add new functions?
yeah sure, i built this one from scratch.
Pages: 12