Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sample.eng.ohio-state.edu!purdue!haven.umd.edu!umbc3.umbc.edu!umbc5.umbc.edu!gottlieb From: gottlieb@umbc5.umbc.edu (Robert Gottlieb) Newsgroups: comp.lang.c Subject: Reading in char from command line problem Message-ID: <1991Jun12.012040.26189@umbc3.umbc.edu> Date: 12 Jun 91 01:20:40 GMT Sender: newspost@umbc3.umbc.edu (News posting account) Organization: University of Maryland Baltimore County Lines: 90 Hi all, I'm having a problem with the following program. This is just a simple menu for a grading program. Right now I'm just using stubs to make sure it works. What happens is it will read a char from the command line, but for some reason I think it is getting the carriage return too. The reason I believe this is because it hits the default case after hitting the correct case. I tried using scanf with the * supression on the format statement, but that gave me the same thing. I also tried getchar. I deleted all of the functions that are called as they are just printf statements and the menu is also just a bunch of printf's, to save bandwidth here. Please let me know if you have any ideas how to get this to work. Thanks in advance. /* Robert Allen Gottlieb Internet: gottlieb@umbc5.umbc.edu */ /* Bitnet: gottlieb@umbc2.umbc.edu */ /* */ /* */ /* The opinions herein are not those of UMBC, but rather those of */ /* my cat: Junior. */ /* */ /* "To seek the sacred river Alph */ /* To walk the caves of ice */ /* To break my fast on honey dew */ /* And drink the milk of Paradise..." */ /* */ /* - Neil Peart/RUSH "Xanadu"/A Farewell To Kings */ #include void add(FILE *grade_file); void delete(FILE *grade_file); void show(FILE *grade_file); void compute(FILE *grade_file); void menu(void); void main (void) { FILE *grade_file; char selection; int n; if ((grade_file = fopen("grades", "rw")) == NULL){ printf ("\nNot enough memory!\n"); exit (-1); } menu(); while ((n = read(0, &selection, 1)) > 0){ switch (selection){ case 'A' : add (grade_file); break; case 'D' : delete (grade_file); break; case 'S' : show (grade_file); break; case 'C' : compute (grade_file); break; case 'E' : exit (0); break; default : printf ("\nIncorrect selection, try again\n"); break; } menu(); } fclose(grade_file); }