I have a problem with my main while loop.
My problem is that the main while loop skips over theCode:int main() { int number; int blank; int length = 0; int keepGoing = 89; while (keepGoing == 89 || keepGoing == 121) { blank = 0; number = getchar(); if (number == 32) { blank = 1; } while (blank != 1) { if (number >= 48 && number <= 57) { putchar(number); length++; break; } } printf("\nWould you like to enter another equation? y/n \n"); keepGoing = getchar(); } return 0; }
and just ends. I have no idea what could be wrong as the code seems just fine to me. (But obviously it isn't or I wouldn't be asking this.)Code:keepGoing = getchar();
Well, fflush(stdin); is handy for clearing the buffer after a scanf, but I just noticed you don't have one at all in it, even after asking the question so theres no way to read in what they put?
I'm not sure why you'd want keepGoing = getchar(); but as far as I know.. that's kinda not do-able? to assign a command to an integer?
Rather do it on seperate lines.
It doesn't skip over that line. getchar returns a char and you declared number as int. So if you input 8 say it will return 56. Check the ascii chart. You should probably just use scanf. Put in a print statement to see whats actually going on.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein
If its absolutely necessary that you use getchar you can subtract and get the int value:
Code:number = getchar(); number = number - 48;
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein
as an aside, you also aren't handling upper case characters.
Let me just explain sort of what I'm doing here, because I'm not sure people really understand that. (Not anyone's fault, I never explained it).
This is the beginning of a basic calculator application. This is the part to get the input, however, our teacher wants us to use getchar() and putchar() for inputting and outputting numbers.
This loop checks to make sure the input is a valid statement of:
x *operator* y
There needs to be an operand before the operator and an operand after the operator. But there can be any amount of spaces in between that data. So that's what this loop is checking for.
Obviously that loop hasn't gotten to that point yet, I'm just figuring out why the keepGoing = getchar(); is not working correctly. And I have tried changing keepGoing to a char.
:EDIT:
I've just discovered something interesting. While the variable keepGoing is being tested in the while loop the keepGoing = getchar(); line just seems to be skipped. However, when I tried another variable that wasn't tested at all. It worked fine. And when I then tested that variable in the while loop, it would get skipped again.
EX:
That "skips" a = getchar();. However, when I write:Code:char a = 'y'; char b = 'y'; while (a == 'y') { ... a = getchar(); }
the program actually waits for my input.Code:char a = 'y'; char b = 'y'; while a == 'y') { ... b = getchar(); }
Does that make any sense to anyone?
Last edited by lithiummethoxide; 03-21-2009 at 06:09 PM. Reason: Needed to update information on problem.
I have had problems with printchar before. I think i grabs the enter after you type a char. Im not sure how to explain this problem to you, but ill show you how i usually fix it(patch).
Now comment out the two instance ofCode:int main(){ int number1,number2,dump; /*Simple infinite loop prompting for values and outputting them*/ while(1){ puts("\nEnter number1\n"); number1 = getchar(); dump = getchar(); //using dump to store \n printf("this is number1\n"); putchar(number1); puts("\nEnter number2"); number2 = getchar(); dump = getchar(); //using dump to store \n printf("this is number2\n"); putchar(number2); } return 0;
and see how it behaves different.Code:dump = getchar();
Ived used this patch for quite some time. To lazy to actually research. A proper explanation would be interesting.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein
Ah, thanks for that little workaround. I'll try that. Maybe a simple flush of the buffer might fix the problem...
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks