I'm looking for an alternative for getch() and system("cls") since everyone says it's a no-no. I know it's fine for school work but I want to have "good" programming habits.
what i'm trying to use right now is scanf() to replace getch() at the end of the program. lol as for system(), i don't know any aside from making tons of \n with printf().
-----
also, since i'm already here, can someone please explain why the "buffer" needs to be cleaned when i do something particularly with char types. i never understood that. i know a way or two to work it out but it's better to know what really happens IMO. i was told to use "\n%c" at scanf() and a getchar() after scanf().
Although the solutions are in C++, I think you can figure out the C equivalents.
Happy cod- oh, about that last question. That little bit you have there ("\n%c") is to prevent your character variable from containing a newline that also makes its way into the buffer when you hit "Enter".
that article uses flush keyword without the use of std:: prefix like the rest of the code. also flush does not fix the problem of cin syncronization. i tried it and the same thing happened. it simply printed press key to exit then exited without the key entered. the code below fixes the issue when accepting code that has cin.
1 2 3 4 5 6 7 8 9
void PressEnterToContinue()
{
std::cin.sync(); // use synch not flush. flush doesnt work
std::cout << "Press ENTER to continue... ";
std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}
Thanks for the articles! that section is full of info.
im reluctant in doing hundreds of "\n" with printf(), but i'll try.
i think i'll use this over getch(). i'll just have to study how fflush works.
1 2 3 4 5 6 7 8 9
#include <stdio.h>
void PressEnterToContinue()
{
int c;
printf( "Press ENTER to continue... " );
fflush( stdout );
do c = getchar(); while ((c != '\n') && (c != EOF));
}
also, how do i know if a "buffer" overflowed or needs to be cleared and whatnot? does this only happen to char types? will it ever happen to integers? how does using the getchar() and "\n%c" do its magic? D: