Home Forums C Programming Help with switch Re: Re: Help with switch

#3471
GWILouisaxwzkla
Participant

This seems to work:



#include
#include

int main(void)
{
int choice;
int number;
float floatingPointNumber;
char a;
char b;
char c;


printf ( "please enter a choice nn" );
printf ( "A-print the square of the number n" );
printf ( "B- print the square root of the numbern" );
printf ( "C- print the cube of the number. n" );
printf ( "Q- Quit n" );
choice = getchar ();
getchar (); //get newline char

while ( choice != 'Q' && choice != 'q' )
{ /*loop until user types the End of File */

switch ( choice )
{

case 'A':
case 'a':
printf ( "nEnter a number: n" );
scanf ( "%d", & number );
getchar (); //get newline char
number = number * number;
printf ( "Your answer is: %i n", number );
break;

case 'B':
case 'b':
printf ( "nEnter a number: n" );
scanf( "%f", & floatingPointNumber );
getchar (); //get newline char
floatingPointNumber = sqrt ( floatingPointNumber );
printf ( "Your answer is: %f n" , floatingPointNumber );
break;

case 'C':
case 'c':
printf ( "nEnter a number: n" );
scanf ( "%d", & number );
getchar (); //get newline char
number = number * number * number;
printf ( "Your answer is: %i n", number );
break;
case 'Q':
case 'q':
printf ( "goodbye .... n " );
break;

default:
printf ( "n
" );
printf ( "nIncorrect choice entered. ");
printf ( "n
nn" );
printf ( "enter a choice: " );
break;
};/*end switch function*/
printf ( "nn" );
printf ( "please enter a choice nn" );
printf ( "A-print the square of the number n" );
printf ( "B- print the square root of the numbern" );
printf ( "C- print the cube of the number. nn" );
printf ( "Q- Quit n" );
choice = getchar ();
getchar (); //get newline char

}/*end while function*/

return 0;
}