|
Post by varnil on Mar 31, 2008 0:32:49 GMT 1
very random question about c++, for some reason whenever its run it completely kills the machine its on: //#define until(t) white(!t) std::string next = ""; std::string const eol = "END"; std::vector<std::string> words; for(; { until(std::cin) std::cin>>next; if(next==eol) break; words.push_back(next); } std::sort(words.begin(),words.end()); for(int i;i<words.size();i++)std::cout<<words <<std::endl;
|
|
|
Post by metaview on Mar 31, 2008 8:52:51 GMT 1
It's reading from stdin, so it rather Windows or Linux but at least not Palm OS. I'm not quite sure what it does: you capture everything until you get a 0-byte at std::cin, then you store the captured word in a vector until you get a EOL from std::cin I'm not that sure in which case you get a 0-byte from std::cin. Maybe you want to look out for a space (0x20)?
|
|
|
Post by varnil on Apr 2, 2008 22:57:19 GMT 1
LOL i should've clarified the situation (I'm not that stupid, thankfully). I wasn't running this on a palm, it was a question one of my friends asked me and i couldn't answer... This is using the normal gcc++ toolchain i ran it on windows and linux both providing the same crash... cant figure out why.
Anyways more info on the bug; Ideally this is supposed to be simple (his 5th programming assignment ever): ask the user for words which are to be pushed into an array until the user types END, read in the words, sort them, and print them back out. Of course in my oppinion this is a very stupid way to write it, probably an attempt to avoid exceptions and shorten code or something, so i rewrote it normally for him and it worked. Still for the sake of knowledge i want to know what's wrong (I don't even have the opportunity to enter a word when i run it, it immediately kills the whole system and reboots ubuntu while leaving windows frozen (something that shouldn't even happen EVER...))
|
|
|
Post by Tinnus on Apr 3, 2008 1:54:06 GMT 1
Try removing that "until" thing.
|
|
|
Post by varnil on Apr 3, 2008 2:26:04 GMT 1
I asked him why that was there, he said he wanted to not catch the errors and not have to throw them ie not do something like this (peudocode) cin>>i; while(!cin){ cerr<<"user's an arktard\n";cin>>i"} so it does the same thing (replacing until with the literal while(! still has bad results). i thought this occured because cin is initially undefined and causes a crash yet i checked and thats not true. Well if u got any other ideas thats great!
|
|
|
Post by metaview on Apr 3, 2008 19:42:22 GMT 1
come on, write someting readable in plain C. I refuse to even try to understand this stupid <<stream>> syntax.
|
|
|
Post by varnil on Apr 3, 2008 19:44:54 GMT 1
lol that kindof depletes the purpose, but i agree with u on that! Thnx anyxays everyone
|
|
|
Post by Tinnus on Apr 3, 2008 20:39:07 GMT 1
That doesn't make any sense, just do cin >> var and it will read the string/value from input for you. I can see no sense in doing !cin since cin is always valid assuming you linked the program with g++.
Maybe he thought that "cin >> var" "returned" a value indicating if it could read a variable or not, but that's not like that--instead, when you do cin >. value (or scanf for taht matter), the function WAITS for the user to input something.
|
|
|
Post by varnil on Apr 4, 2008 22:08:45 GMT 1
are u sure about the g++ thing? what about parse errors (ie enter bob for an int)? Although if u're right that does explain it
|
|
|
Post by Tinnus on Apr 13, 2008 23:12:15 GMT 1
If you enter "bob" it simply won't read anything and will wait for you to enter a number until it returns.
|
|