|
From: lstar36 <ls...@fl...> - 2000-11-12 02:54:03
|
The function getchar() is a macro which leaves a char in the buffer so =
the second time throught the while loop you get the error print. Use =
getche(). Also it is better programming to conver the keyboard input =
to an upper or lower case with the function toupper() and then use only =
a single case. Also use a do{ while()} loop to make the program easier =
for someone else to understand.
example:
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
int grade;
int aCount =3D 0, bCount =3D 0;
printf("Enter the letter grades.\n");
printf("Enter the letter x to end input.\n");
do
{
grade =3D toupper(getche());
switch(grade)
{
case 'A':
++aCount;
break;
case 'B':
++bCount;
break;
case 'X':
printf("\nend of input\n");
break;
default:
printf("\nIncorrect letter grade entered.\n");
printf("Enter a new grade.\n");
break;
}
}
while (grade !=3D 'X');
printf("%d\n", aCount);
printf("%d\n", bCount);
system("PAUSE");
return 0;
}----- Original Message -----=20
From: Saundra Schwarz=20
To: dev...@li...=20
Sent: Saturday, November 11, 2000 12:01 PM
Subject: [Dev-C++] switch statement
Hi, I've been having difficulty getting my switch statements to work =
properly. When I run the program no matter what I enter I get the =
default response. Please help. I've inculded a sample from my text. I =
need the switch statement to work for an assignment.
#include <stdlib.h>
int main()
{
int grade;
int aCount =3D 0, bCount =3D 0;
printf("Enter the letter grades.\n");
printf("Enter the -1 to end input.\n");
while( (grade =3D getchar()) !=3D -1)
{
switch(grade)
{
case 'a': case 'A':
++aCount;
break;
case 'b': case 'B':
++bCount;
break;
default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}
printf("%d\n", aCount);
printf("%d\n", bCount);
system("PAUSE");
return 0;
}
|