making a queue of c-strings

I need to create a queue of c-strings, but when I push a new c-string onto the
queue, all the items in the queue appear to point to the same c-string.
In this code, data is ifstreamed in from a text file then ultimately assigned to
an instance of a class, "account".
The use of a queue here might seem unncessary but for my class project we are required to make use of 3 out of 4 specific STL classes (vectors, lists, stacks, and queues, so far I've used list and vector) my teacher suggested this function is a good spot to implement a stack or queue. I had the function working before without the addition and now I need to add and demonstrate and the use of a STL queue/stack.
In the show_trace, the front and back functions show the same c-string value after every loop iteration.
When I looked online mostly saw mention of string class with queues, I considered switching my variable to string class, which Id rather avoid if I could just make a queue of c-strings.

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
void import_existing_file( list<Savings_Account>& new_account, ifstream& ifile)
{

Savings_Account account;
char buffer[INPUT_SIZE];
queue<char*> new_item;

    while (!ifile.eof())
    {
	for(int count=0; count<9; count++)
	{
	ifile>>buffer;
	new_item.push(buffer);
		#if SHOW_TRACE
			cout<<" count: "<<count<< " queue front: "<<new_item.front() 
			<<" back: "<<new_item.back()<< " size: "<<new_item.size()<<endl;
		#endif
	}
 
	account.set_last_name(new_item.front());
		#if SHOW_TRACE
		cout<<" in queue: "<<new_item.front()<<endl;
		#endif
	new_item.pop();

	account.set_first_name(new_item.front());
	new_item.pop();

	account.set_middle_initial(new_item.front());
	new_item.pop();

	account.set_ssn(new_item.front());
	new_item.pop();

	account.set_area_code(new_item.front());
	new_item.pop();

	account.set_phone_number(new_item.front());
	new_item.pop();

	account.set_balance(new_item.front());
	new_item.pop();

	account.set_account_id(new_item.front());
	new_item.pop();

	account.set_password(new_item.front());
	new_item.pop();

        new_account.push_back(account);
    }
.
Last edited on
You have only one buffer for the input so you overwrite it every time.
If you need to use c-string you need to allocate a buffer for each input with new.
You also need to remember to delete them at the end of the program.
Topic archived. No new replies allowed.