Debugging Input into a vector

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
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?
Does it work if you just typed "end" instead of "end 2 3" ?
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
Have you stepped line by line with the debugger yet?
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
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.
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.
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.