Home › Forums › C Programming › Help with switch › Re: Re: Help with switch
November 3, 2008 at 7:26 pm
#3471
GWILouisaxwzkla
Participant
This seems to work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | <br /> <br /> #include <br /> #include <br /> <br /> int main(void)<br /> {<br /> int choice;<br /> int number;<br /> float floatingPointNumber;<br /> char a;<br /> char b;<br /> char c;<br /> <br /> <br /> printf ( "please enter a choice nn" );<br /> printf ( "A-print the square of the number n" );<br /> printf ( "B- print the square root of the numbern" );<br /> printf ( "C- print the cube of the number. n" );<br /> printf ( "Q- Quit n" );<br /> choice = getchar ();<br /> getchar (); //get newline char<br /> <br /> while ( choice != 'Q' && choice != 'q' )<br /> { /*loop until user types the End of File */<br /> <br /> switch ( choice )<br /> {<br /> <br /> case 'A':<br /> case 'a':<br /> printf ( "nEnter a number: n" );<br /> scanf ( "%d", & number );<br /> getchar (); //get newline char<br /> number = number * number;<br /> printf ( "Your answer is: %i n", number );<br /> break;<br /> <br /> case 'B':<br /> case 'b':<br /> printf ( "nEnter a number: n" );<br /> scanf( "%f", & floatingPointNumber );<br /> getchar (); //get newline char<br /> floatingPointNumber = sqrt ( floatingPointNumber );<br /> printf ( "Your answer is: %f n" , floatingPointNumber );<br /> break;<br /> <br /> case 'C':<br /> case 'c':<br /> printf ( "nEnter a number: n" );<br /> scanf ( "%d", & number );<br /> getchar (); //get newline char<br /> number = number * number * number;<br /> printf ( "Your answer is: %i n", number );<br /> break;<br /> case 'Q':<br /> case 'q':<br /> printf ( "goodbye .... n " );<br /> break;<br /> <br /> default:<br /> printf ( "n <hr class="bbcode_rule" /> " );<br /> printf ( "nIncorrect choice entered. ");<br /> printf ( "n <hr class="bbcode_rule" /> nn" );<br /> printf ( "enter a choice: " );<br /> break;<br /> };/*end switch function*/<br /> printf ( "nn" );<br /> printf ( "please enter a choice nn" );<br /> printf ( "A-print the square of the number n" );<br /> printf ( "B- print the square root of the numbern" );<br /> printf ( "C- print the cube of the number. nn" );<br /> printf ( "Q- Quit n" );<br /> choice = getchar ();<br /> getchar (); //get newline char<br /> <br /> }/*end while function*/<br /> <br /> return 0;<br /> }<br /> <br /> <br /> |