Debugging Input into a vector

Sep 21, 2010 at 3:22pm
I'm fairly new to c++, so I apologize in advance if the answer is obvious.

Basically, I'm writing a program that will find the mode of a series of numbers, and output it, but the program gets stuck inside the while loop for inputs.

The while loop is meant to allow you to input numbers (as strings), and push them back into the vector. Inputting "end" should terminate the loop.


vector<String> list;
cout << "Please input a list of numbers. When done, type \"end\" \n";
String next_num = "";
String final_num = "";
int next_times = 0;
int final_times = 0;
bool cont = true;

while(cont)
{
cin >> next_num;
cout << next_num << endl;
if(next_num.compare("end") != 0)
{
list.push_back(next_num);
cout << "Just pushed " << next_num << endl;
}
else
{
cout << "Trying to exit loop";
cont = false;
}
}




unfortunately, the output, is something like this:

Please input a list of numbers. When done, type "end"
5
5
Just pushed 5
4
4
Just pushed 4
4
4
Just pushed 4
test
test
Just pushed test
end 2 3
ctrl+c ends program

Bold indicates the input.
It seems to be checking the if statement before the first cout statement, because it does not read "end" back to you. Additionally, it seems as if the program is stuck in the loop, without pushing into the vector or printing, after "end" is typed.
I've asked several people, and no one seems to be able to tell me what's wrong.
Last edited on Sep 21, 2010 at 3:23pm
Sep 21, 2010 at 3:45pm
It works. At least for me. I compiled it with MinGW, it exits after the "end 2 3". I don't know what is the problem. Perhaps you could try compiling it with some other compiler?
Sep 21, 2010 at 3:46pm
Does it work if you just typed "end" instead of "end 2 3" ?
Sep 21, 2010 at 3:51pm
Here are two samples of my testing. [] indicates input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Please input a list of numbers. When done, type "end" 
[5]
5
Just pushed 5
[4]
4
Just pushed 4
[4]
4
Just pushed 4
[test]
test
Just pushed test
[end 2 3]
end    // Note that cin << will read only until it encounters a blank character (' ', '\t', '\n')
Trying to exit loop


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Please input a list of numbers. When done, type "end" 
[1]
1
Just pushed 1
[2]
2
Just pushed 2
[3]
3
Just pushed 3
[4]
4
Just pushed 4
[end]
end
Trying to exit loop
Last edited on Sep 21, 2010 at 3:52pm
Sep 21, 2010 at 4:12pm
Have you stepped line by line with the debugger yet?
Sep 21, 2010 at 4:16pm
What's a String type? In c++ there is a std::string (note the lowercase form of string). If you have developed your own string class then we would need to see the code for that. Dserbia says it works but I don't see how anyone could even compile what was posted without modifying it to be honest. I can get the same results as Dserbia by modifying the program to use std::string and embedding the code within a proper main function.
Last edited on Sep 21, 2010 at 4:22pm
Sep 21, 2010 at 7:51pm
I did have an included file at the beginning, I believe it's just a standard String class though.

The lines that print what the number is, and what it pushed to the vector were lines that were added to try to determine where it was messing up. We have not, however, worked with a debugger yet.

The end 2 3 was unintentional.
It would have been
[end]
[2]
[3]
I input the word end, and it prompted me for another input afterwords, without printing "end" back to me, printing "Just pushed end" or "Trying to exit loop"

I then input 2, and it didn't print anything out, just prompted me for another input, which I typed 3, and which also failed.

It seems like it's getting stuck in the loop, without being able to do anything.

We've been compiling using puTTy and writing using notepad++. I'll try a different one to see if that works.
Sep 21, 2010 at 9:48pm
If you want help you need to post something that compiles for others. There is no String type in C++, only string. It would help to use a free version of visual studio express so that you can try this with a full featured debugger. It is very simple to step through the program and determine the problem but I cannot compile your code so there is no way for me to help you.
Sep 21, 2010 at 10:45pm
Alright, well I appreciate it anyway.
A friend of mine helped me rewrite the code, and it works now, for some odd reason.
Thanks for the help anyway guys :]
Topic archived. No new replies allowed.