I have something that works like this
Code:
int main()
{
int currentNumber;
int currentUnit[3];
int checkInput;
while ((checkInput = scanf("%d%2s", ¤tNumber, currentUnit)) != -1)
{
if (input OK) //do other stuff
else
{
int clearScanfBuffer = 0;
while ((clearScanfBuffer = getchar()) != '\n')
{
//just loop and do nothing and clear out bad data
}
}
}
//output some calculations
return(0);
}
What I'd like to do, and can't seem to figure out what to do, is move the buffer clear code from the else statement, into it's own function out of the main loop where I can do other stuff with the data. I can get what I want to work in the main loop, but I don't want a huge chunk of code in there if I can write it something like this
Code:
int main()
{
...
else
{
clear( //some arguments?? )
}
return(0);
}
void clear( ?? )
{
put the clearScanfBuffer code here
}
The jist of my question is can I "move" that clear buffer code to it's own function, and if I can, what do I need to pass to the function to do that?