Doubt with scaning characters
Hi all, for a CS project I need to make a menu, so the user must input values from 1 to 12 and q to exit the program.
My main problem is that I am using getchar() to scan the menu selection.
So inputs from 1 to 9 and q works, but 10 to 12 doesn't since getchar only supports ONE character.
My first changes were to establish the switch cases to integers between range of 10 to 12.
But still I need a suggestion of how to make the input 10, 11 and 12 to work.
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "prints.h"
#include "exams.h"
#include "guessyn.h"
#include "guessmn.h"
#include "diceg.h"
#include "largestn.h"
#include "distance.h"
#include "areapara.h"
#include "areatrape.h"
#include "perimepoly.h"
#include "arrayv.h"
#include "arrayc.h"
int main ()
{
int selection;
do{
printf("Choose a skill to practice:\n");
printf("1) Print shapes\n");
printf("2) Exam score\n");
printf("3) I guess your number\n");
printf("4) Guess my number\n");
printf("5) Dice game\n");
printf("6) Largest number\n");
printf("7) Distance\n");
printf("8) Area of parallelogram\n");
printf("9) Area of trapezoid\n");
printf("10) Perimeter of polygon\n");
printf("11) Array vowel\n");
printf("12) Array consonant\n");
printf("q) Quit\n");
printf("\n");
printf("Enter selection: ");
selection = getchar();
while ( getchar() != '\n');
while ( selection != '1' && selection != '2' && selection != '3' && selection != '4' && selection != '5' && selection != '6'
&& selection != '7' && selection != '8' && selection != '9' && selection != 10 && selection != 11 && selection != 12
&& selection != 'q' )
{
printf("\n");
printf("Wrong input!!! Please try again...\n");
printf("\n");
printf("Enter selection: ");
selection = getchar();
while ( getchar() != '\n');
}
printf("\n");
switch( selection ){
case '1': prints(); break;
case '2': exams(); break;
case '3': guessyn(); break;
case '4': guessmn(); break;
case '5': diceg(); break;
case '6': largestn(); break;
case '7': distance(); break;
case '8': areapara(); break;
case '9': areatrape(); break;
case 10: perimepoly(); break;
case 11: arrayv(); break;
case 12: arrayc(); break;
case 'q':
printf("Thanks for playing!");
return EXIT_SUCCESS;
}
printf("\n");
}while( selection == selection );
}
Changes I made are in red