Loop help! Please!
Alright mates... this is my first post here because I finally found an assignment that really stumped me.
Basically I want a loop that lets you input integers into a vector until a tube "|" is inputed and then continue with the program.
For some reason... I cant make it work.
while(cin>>x) //terminates program upon |
while(cin) //thats just sloppy and it doesnt work anyways
while(cin.get()!='|')
{
cin>>x;
v.pushback(x);
cin.clear();
} //seems to work but it butchers the first keystroke of nonwhite data
ive really explored a variety of different things... breaks... throws and catches... but atm im pretty screwed...
any help?
thanks in advance!
[759 byte] By [
Jsteiner] at [2007-11-20 10:56:22]

# 1 Re: Loop help! Please!
bumb
# 2 Re: Loop help! Please!
Alright mates... this is my first post here because I finally found an assignment that really stumped me.
Basically I want a loop that lets you input integers into a vector until a tube "|" is inputed and then continue with the program.
For some reason... I cant make it work.
while(cin>>x) //terminates program upon |
while(cin) //thats just sloppy and it doesnt work anyways
while(cin.get()!='|')
{
cin>>x;
v.pushback(x);
cin.clear();
} //seems to work but it butchers the first keystroke of nonwhite data
ive really explored a variety of different things... breaks... throws and catches... but atm im pretty screwed...
any help?
thanks in advance!
Edit/Delete Message
# 3 Re: Loop help! Please!
while(cin>> x) {
v.pushback(x);
}
cin.clear();
Should work for you.
The loop will stop because '|' cannot be interpreted as int and cin will enter fail state.
Kurt
ZuK at 2007-11-9 1:26:47 >

# 4 Re: Loop help! Please!
Well... the entire program stops. when a | is entered to terminate the loop the entire program ends... no more input or output or anything.
it even terminates past keep_window_open()... ITS CRAZY!
# 5 Re: Loop help! Please!
Well... the entire program stops. when a | is entered to terminate the loop the entire program ends... no more input or output or anything.
it even terminates past keep_window_open()... ITS CRAZY!
is it a console application?
http://www.dev-archive.com/forum/showthread.php?t=434330
# 6 Re: Loop help! Please!
in visual c++ 2005
/*****************************/
#include "std_lib_facilities.h"
class bad_input{};
int main()
{
vector<int> v(0);
int x=0;
int n;
int tot=0;
cout<<"Please enter some numbers (Press '|' at prompt to stop):\n";
while(cin>>x)
{
v.push_back(x);
}
cin.clear();
cout<<"\nHow many numbers would you like to sum, starting from the first?\n";
cin>>n;
cout<<"\nThe sum of the first "<<n<<" numbers";
for(int i=0;i<3;i++)
{
tot=tot+v[i];
cout<<", "<<v[i];
}
cout<<" is "<<tot<<".";
cout<<"\n\n";
keep_window_open();
return 0;
}
/*********************************/
the while loop terminates the entire program... whiskey tango foxtrot?
# 7 Re: Loop help! Please!
i dont know what keep_window_open(); is exactly, but im guessing that it is not doing its job
that is a console application, try running the program through the command prompt, or try putting system("PAUSE"); where keep_window_open(); is
# 8 Re: Loop help! Please!
It might be possible that the input stream is not getting properly flushed. As of now, there is no portable way of properly flushing the input stream. Try putting cin.sync() after cin.clear() in your program. Also, shouldn't the condition of the for loop be i < n? What if someone enters a value for n which is greater than the number of inputted integers? Then, you would be accessing past the vector bounds and the program might crash.
# 9 Re: Loop help! Please!
Yes yes.. I changed it to 3 because i wanted to know if the error was from a range violation in teh vector... and it wasnt I jsut didnt change it back...
here goes on that cin.sync()...
# 10 Re: Loop help! Please!
OH MY GOD IT WORKED!
thank you kindly!
now please... if you could... explain to me why that worked?
# 11 Re: Loop help! Please!
char ch;
while ( cin >> ch , ch != '|' )
{
// contune....
}
hiren thakkar.
# 12 Re: Loop help! Please!
Check this out to have a better understanding than I can give you. :)
Istream sync() (http://www.cplusplus.com/reference/iostream/istream/sync.html)
Also note that the solution may have worked for you but it is non-portable. So, if you were working on a Unix machine, then this might not work.
# 13 Re: Loop help! Please!
[ merged threads ]
cilu at 2007-11-9 1:36:59 >
