STL queue class

This program reads an even number of characters. The program then prints the first character, third character, fifth character, and so on. On a second output line, the program prints the second character, fourth character, sixth character, and so on. I must use two queues to store the characters.

The program runs fine, but as soon as it's ready to display the result, I come across an error window that reads: Debug Assertion failed! with the options [Abort] [Retry] [Ignore]

I hit ignore and it displays the expected result correctly.

I believe the problems are in the FOR loops, reading an empty array element. How can I fix this? Other than that, this program is "almost" as good as done.

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


#include <iostream>
#include <stack>
#include <string>
#include <queue>
using namespace std;

int main()

{
	queue <string> input;
	queue <string> even;	
	queue <string> odd;			
	string item , newItem;
	int more;
	string item_size;
	char junk;

	 do {
			
				int i = 0;
                cout << "Enter a sentence ('.' to stop): ";
                cin >> item;
                while (cin && item != ".")
				{
					if (item.length()%2==1)
						input.push(item);
					else
						input.push(item);
                     cin >> item;
				}


				while(!input.empty())
				{
					item = input.front();
					item_size = item;
					input.pop();

					if(item[0] != NULL)
					{

						for(int j = 1; j < item_size.length(); j++)
						{
							if (j % 2 == 0)
							{
								j++;
							}

							newItem = item[j];
							odd.push(newItem);
						}


					for(int j = 0; j < item_size.length(); j++)
						{
							if (j % 2 != 0)
							{
								j++;
							}

							newItem = item[j];
							even.push(newItem);
						}
						
					}

				}


			while (!even.empty())
				{
                    cout << even.front() << " ";   // print the top character
                    even.pop();          // remove top character
                }

			cout << endl;

	       while (!odd.empty())
				{
                        cout << " " << odd.front();   // print the top character
                        odd.pop();          // remove top character
                }


	            cout << endl;

                cout << "More (0/1)?";
                cin >> more;
                cin.get(junk);

	 } while(more == 1);

	cout << endl;
	system("pause");
	return 0;

}






Enter a sentence ('.' to stop): happy
.

//ERROR WINDOW APPEARS HERE WITH THREE OPTIONS, CLICK [IGNORE].

h p y
 a p
More (0/1)?


Last edited on
Why did you declare these variables

1
2
3
4
5
6
7
	queue <string> input;
	queue <string> even;	
	queue <string> odd;			
	string item , newItem;
	int more;
	string item_size;
	char junk;


outside the do-while loop if they all except 'more' and 'queue(s)' are used only inside the loop?!

And I do not understand such a silly code as

1
2
3
4
if (item.length()%2==1)
	input.push(item);
else
	input.push(item);


What does it mean?
Last edited on
this is one of the most over complicated programs I've seen so far...

the assertion will arise on line 51 and 63. reason: due to the preceding j++; the last access to item[j]; is out of bounds
Topic archived. No new replies allowed.