Hello,
First the code-
I am very new to C but not new to programming (although it has been a long time since I did any of it). I have the K&R book and have found it to be as confusing as it is informative. Right now I am attempting the above as an exercise to control the return of the program. I have no idea if this is a very stupid way of doing it, but it's what I have managed so far.Code:#include<stdio.h> int main() { char input; input = NULL; while(input != 'Q') { printf("Enter Letter:"); scanf("%c", &input); } return 0; }
The problem- Whenever I run this program it does as it should in my terminal window (I'm using a bash terminal on mac os x btw) and prints "Enter Letter:" to the screen and waits for my input. If I enter "Q" it exits the loop and returns the program perfectly. However, if I try entering any other type of data it newlines and prints "Enter Letter:" once, and then again for each character I have inputted.
For example-
causes the next line to beCode:Enter Letter:A
I am figuring that the problem is my lack of understanding of how scanf actually works and I must admit I am a bit lost in how C seems to handle data. I have also tried getchar() and it gives the same result with the testing I have done so far.Code:Enter Letter:Enter Letter:
Also, I am new to posting on message boards so I hope I have not posted anything incorrectly. Any tips for making my future postings easier for people to read are greatly appreciated.
Thank you to any and all who reply.
Babel



LinkBack URL
About LinkBacks


