Reputation: 31
please. I want a display for options from 'A' to 'H', however the "default invalid option" kept appearing. How do i prevent the "default invalid option from apprearing. Thanks so much if you could give me a hand.
void disp3Ave (void)
{
int day, total;
char j;
extern float PSI23ave[];
extern float PSI24ave[];
extern float PSI25ave[];
float average;
total=0;
printf("Day 23 or 24?\n");
scanf("%\d", &day);
if(day == 23)
{
printf("A: 0300.\n");
printf("B: 0600.\n");
printf("C: 0900.\n");
printf("D: 1200.\n");
printf("E: 1500.\n");
printf("F: 1800.\n");
printf("G: 2100.\n");
printf("H: 0000.\n");
scanf("%c", &j);
switch (j)
{
case 'a':
case 'A': total= PSI23ave[3] + PSI23ave[4] + PSI23ave[5];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'b':
case 'B': for (j=0;j<24;j++) total= PSI23ave[6] + PSI23ave[7] + PSI23ave[8];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'c':
case 'C': for (j=0;j<24;j++) total= PSI23ave[9] + PSI23ave[10] + PSI23ave[11];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'd':
case 'D': for (j=0;j<24;j++) total= PSI23ave[12] + PSI23ave[13] + PSI23ave[14];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'e':
case 'E': for (j=0;j<24;j++) total= PSI23ave[15] + PSI23ave[16] + PSI23ave[17];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'f':
case 'F': for (j=0;j<24;j++) total= PSI23ave[18] + PSI23ave[19] + PSI23ave[20];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'g':
case 'G': for (j=0;j<24;j++) total= PSI23ave[21] + PSI23ave[22] + PSI23ave[23];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
case 'h':
case 'H': for (j=0;j<24;j++) total= PSI24ave[0] + PSI24ave[1] + PSI24ave[2];
average = total/3;
printf("3-hr average = %.1f\n", average);
break;
default: printf("Invalid option.\n");
break;
}
}
Thanks in advance! cheers!
Upvotes: 0
Views: 49
Reputation: 36882
As user1990169 concluded, the issue is that the buffer reads the integer from stdin, but the newline character remains in the input buffer. This can be addressed in a number of ways, but the simplest is to add a call to getchar()
which will read one character from the input stream.
scanf("%d", &day); // remove the \ before d
getchar();
here's a small program to demonstrate:
int main(void) {
int day = -1;
char c = 'x';
scanf("%d", &day);
getchar(); // this line
scanf("%c", &c);
printf("read: %d %c\n", day, c);
}
if you remove the call to getchar()
you'll see that there is no wait before reading the character, it will just immediately read a newline into the c
variable.
There are more advanced options to deal with input separated by an unknown number of spaces using isspace
and ungetc
, but for your simple program, this should suffice.
Additionally, instead of having a case for the uppercase and lowercase, you can convert the character and only have one case label for each:
j = toupper(j); // #include <ctype.h>
Upvotes: 0
Reputation: 12705
It appears that when you enter the value for the int variable day
, the character '\n'
remains in the input buffer.
This gets stored in your variable j
and hence you get Invalid option.
Edit:
You can try scanf("\n%c", &j);
Upvotes: 3