Reading in char from command line problem

Robert Gottlieb gottlieb at umbc5.umbc.edu
Wed Jun 12 11:20:40 AEST 1991


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 at umbc5.umbc.edu  */
/*                               Bitnet:   gottlieb at 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 <stdio.h>

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);


}



More information about the Comp.lang.c mailing list