seventheyejosh Posted September 9, 2009 Share Posted September 9, 2009 Hello! More new to c++ questions. I'm using the following: cout << "Thanks for playing \n\nPress Enter to exit..."; // system("PAUSE"); workaround cin.get(); to simulate system("pause");, as not only have i read nothing but bad reviews about it, but i'm developing on a unix system. when I use this code: cin.getline(questions,256); in one of my programs to get an input string it works fine. but if i use the following: cin >> choice; as i only want an int, the program completely skips the last line and exits. it is strange, as it compiles just fine. to verify, i removed the line requireing input, just deleted it, and it worked just fine. save for no input Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/ Share on other sites More sharing options...
corbin Posted September 10, 2009 Share Posted September 10, 2009 If choice is declared as an int, that won't work. You could however, use sprintf, atoi or dec (example below). dec example (the downside to this method is that you don't know if 0 is an error or actually the value 0). cin >> dec >> choice; Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915786 Share on other sites More sharing options...
seventheyejosh Posted September 10, 2009 Author Share Posted September 10, 2009 It is still exiting straight away :\ I'll include the full code, if someone doesn't mind peeking. The 2 files are my madlib, which i made last week and works just fine with the cin strings, and the other is my cootie thing for this week that auto exits after input. I'm writing in pico/smultron on OSX and compiling using terminal and the g++ compiler. Thanks to anyone who takes time to peek well it says i can only upload certain filetypes. Would it be against the rules to rename them to whatever.cpp.php and then upload them? the files are only like 100 lines each, so i could just paste them here i guess.. Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915818 Share on other sites More sharing options...
corbin Posted September 10, 2009 Share Posted September 10, 2009 Don't know if it would be against the rules to rename them or not. Anyway, if we could see the whole code that might help. I guess you could use something like Pastebin. Also, you could try some basic debugging to make sure it is exiting where you think it is. Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915829 Share on other sites More sharing options...
seventheyejosh Posted September 10, 2009 Author Share Posted September 10, 2009 here is the code, and I'm pretty sure it is exiting where i think, because if i remove the cin line, it continues on, obviously without input, and gets right to the push return prompt. the working madlib code: #include <iostream> using namespace std; int main(){ char questions[15][256]; int c,i,j; cout << "*******************************\n"; cout << "* *\n"; cout << "* Welcome to Josh's Madlib *\n"; cout << "* ___________________________ *\n"; cout << "* *\n"; cout << "* -Make your window wide! *\n"; cout << "* -or, use small phrases! *\n"; cout << "* -Strings are ok! *\n"; cout << "* -Go crazy! *\n"; cout << "* *\n"; cout << "* ~Josh ONeal 9/4/09 *\n"; cout << "*******************************\n\n"; for(i=0,j=1;i<15;i++,j++){ cout << j << ". Enter a "; switch(i){ case 0: cout << "color"; break; case 1: cout << "food"; break; case 2: cout << "drink"; break; case 3: cout << "adjective"; break; case 4: cout << "verb"; break; case 5: cout << "thing"; break; case 6: cout << "verb"; break; case 7: cout << "adjective"; break; case 8: cout << "noun"; break; case 9: cout << "famous person"; break; case 10: cout << "thing"; break; case 11: cout << "clothing item"; break; case 12: cout << "body part"; break; case 13: cout << "verb"; break; case 14: cout << "girl's name"; break; }//end switch cout << ": "; cin.getline(questions[i],256); cout << endl; }// end for for(c=0;c<50;c++){ // system("CLEAR"); workaround cout << endl; }//end for cout << "\n*******************************\n\n" ; /* i googled funniest madlib, and this was in the top results, as I'm not too good at making them up */ cout << "Here are some " << questions[0] << " tips for running the best " << questions[5] << " party ever:\n\n"; cout << "Provide your guests with plenty of " << questions[1] << " and " << questions[2] << ".\nNo one likes to be hungry or " << questions[3] << "!\n\n"; cout << questions[4] << " your fanciest " << questions[11] << " and wear " << questions[0] << " shoes.\nYou wouldn't want your " << questions[12] << " to hurt while you're " << questions[13] << "ing with " << questions[14] << "!\n\n"; cout << "When " << questions[6] << "ing at a party, it's always polite to bring fresh " << questions[10] << "s or a box of " << questions[1] << ",\nat least that's what " << questions[9] << " says you should do!\n\n"; cout << "Last but not least, you should always wear " << questions[7] << " " << questions[8] << "s...\n\n"; cout << "\n*******************************\n\n" ; cout << "Thanks for playing \n\nPress Enter to exit..."; // system("PAUSE"); workaround cin.get(); return(0); }//end main and the broken one #include <iostream> using namespace std; int main(){ int choice,c; char* parts[6]; parts[0]="add the body"; parts[1]="add the head"; parts[2]="add an antenna, hat, or bow"; parts[3]="add an eye"; parts[4]="add a tongue, teeth, or lips"; parts[5]="add a leg"; cout << "*******************************\n"; cout << "* *\n"; cout << "* Welcome to Josh's Cootie *\n"; cout << "* ___________________________ *\n"; cout << "* *\n"; cout << "* -Make your window wide! *\n"; cout << "* -Simply input a number *\n"; cout << "* -that is between 1 and 6! *\n"; cout << "* *\n"; cout << "* ~Josh ONeal 9/9/09 *\n"; cout << "*******************************\n\n"; cout << "Enter your number: "; cin >> dec >> choice; // if this is removed it works fine for(c=0;c<50;c++){ // system("CLEAR"); workaround cout << endl; }//end for cout << "\n*******************************\n\n" ; if(choice>0 && choice<7){ cout << "You may " << parts[choice-1] << "!!!"; }else{ cout << "Your number must be between 1 and 6!!!"; }//end if cout << "\n*******************************\n\n" ; cout << "Thanks for playing \n\nPress Enter to exit..."; // system("PAUSE"); workaround cin.get(); return(0); }//end main if there are any other bad-programming-practice flaws you see i'd love to have some input on that. while the similarities are there, this is no php, and it's been a trek Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915834 Share on other sites More sharing options...
corbin Posted September 10, 2009 Share Posted September 10, 2009 "here is the code, and I'm pretty sure it is exiting where i think, because if i remove the cin line, it continues on, obviously without input, and gets right to the push return prompt." So you never see the "Thanks for playing " and what not stuff if you have the cin >> dec >> count line in? That's weird.... It works fine for me. I did: g++ test.cpp ./a And it executed fine. What params are you using to compile it? What is the output? Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915843 Share on other sites More sharing options...
seventheyejosh Posted September 10, 2009 Author Share Posted September 10, 2009 I do the following: Joshs-Mac:~ MacBook$ cd endeavors/c++/week3/ Joshs-Mac:week3 MacBook$ pico cootie.cpp // if editing Joshs-Mac:week3 MacBook$ g++ cootie.cpp -o cootie.out Joshs-Mac:week3 MacBook$ ./cootie.out and i get (minus whitespace): Joshs-Mac:week3 MacBook$ ./cootie.out ******************************* * * * Welcome to Josh's Cootie * * ___________________________ * * * * -Make your window wide! * * -Simply input a number * * -that is between 1 and 6! * * * * ~Josh ONeal 9/4/09 * ******************************* Enter your number: 3 ******************************* You may add an antenna, hat, or bow!!! ******************************* Thanks for playing Press Enter to exit...Joshs-Mac:week3 MacBook$ the problem is, it doesnt actually wait for me to hit enter and end the program. it just outputs the text and exits. if i remove the cin line, it triggers the invalid number if, obviously, but then it says thanks for playing and waits for the return press. in the other madlib thing it works just fine, but i'm using the character array Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915857 Share on other sites More sharing options...
corbin Posted September 10, 2009 Share Posted September 10, 2009 Ohhhh... I didn't think about that.... The problem is the behavior of cin << dec << choice.... I thought that it would discard non-numeric values, but apparently it reads from the buffer until the next character is not a digit. If you change cin.get() To cout << cin.get() You'll see "10" output (or perhaps the code for \r\n) since that's the code for \n. That means that in the cin >> dec >> choice thing, the new line is being stored.... Hrmm... One solution would be: cin >> dec >> choice; cin.get(); Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915871 Share on other sites More sharing options...
seventheyejosh Posted September 10, 2009 Author Share Posted September 10, 2009 it works! why exactly did that do that? and how come doing cin << choice; cin.clear(); cout >> "Enter..."; cin.get(); wouldnt work? wouldnt cin.clear() have emptied cin for me? also, i never posted that as code, just tried it while i was waiting. thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915879 Share on other sites More sharing options...
corbin Posted September 10, 2009 Share Posted September 10, 2009 Well, inside of cin, there is a buffer that keeps characters from reading the istream. When ever the code calls cin << dec << choice, it only takes from the buffer until it runs into a non numeric character. That means that if there is something else in the buffer (and there is because of the new line), then that will stay in the buffer and be the first thing fetched later. clear() does not clear the buffer. It resets the error flag. Quote Link to comment https://forums.phpfreaks.com/topic/173726-c-input-question/#findComment-915883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.