How to create a loop?
Hey there, I have this piece of code. How do I make it read the file line after line? Now it only reads one line:
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
|
void loadlist(Passenger *&head, Passenger *&last, Passenger *current)
{
string t_first_n, t_last_n;
string t_name;
string line;
char p_flight[FLIGHTS_NUM];
int p_seat_num;
float p_price_paid;
head = NULL;
last = NULL;
ifstream pass_list; // Creates a stream for a file with chmod to write
pass_list.open("passlist.txt"); // Creates/opens file passlist.txt
while(current != NULL)
{
pass_list >> t_first_n >> t_last_n >> p_flight >> p_seat_num >> p_price_paid;
t_name = t_first_n + " " + t_last_n;
Passenger *temp = new Passenger; // Just filling in the list of passengers
temp -> name = t_name;
strcpy(temp -> flight, p_flight); // *
temp -> seat_num = p_seat_num;
temp -> price_paid = p_price_paid; // *
temp -> next = NULL;
head = temp; // *
last = temp;
current = current -> next; // Switches to the next passenger
}
pass_list.close(); // Safely closes current file
}
|
Last edited on
Some adjustments:
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
|
void loadlist(Passenger *&head, Passenger *&last, Passenger *current)
{
string t_first_n, t_last_n;
string t_name;
string line;
char p_flight[FLIGHTS_NUM];
int p_seat_num;
float p_price_paid;
head = NULL;
last = NULL;
ifstream pass_list; // Creates a stream for a file with chmod to write
pass_list.open("passlist.txt"); // Creates/opens file passlist.txt
while(pass_list >> t_first_n >> t_last_n >> p_flight >> p_seat_num >> p_price_paid)
{
t_name = t_first_n + " " + t_last_n;
Passenger *temp = new Passenger; // Just filling in the list of passengers
temp -> name = t_name;
strcpy(temp -> flight, p_flight); // *
temp -> seat_num = p_seat_num;
temp -> price_paid = p_price_paid; // *
temp -> next = NULL;
if(last)
last->next = temp;
else
head = temp; // *
last = temp;
}
pass_list.close(); // Safely closes current file
}
|
Notice that it's
not tested
It is working PERFECTLY well. Thank you very much! I think I understand now!
Topic archived. No new replies allowed.