Home › Forums › C Programming › Help with switch
- This topic has 1 reply, 2 voices, and was last updated 16 years, 1 month ago by GWILouisaxwzkla.
Viewing 1 reply thread
- AuthorPosts
- November 3, 2008 at 5:12 pm #2150RandolpNolenParticipant
hey I have problem printing the answer…I have been ask to enter a number and square it or cubing it or finding the square root using a switch function! first it ask you to enter a choice(A-square B-square root C-cube)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869<br /><br />#include <stdio.h><br />#include <math.h><br /><br />int main(void)<br />{<br />int choice;<br />int number;<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. nn");<br /><br /><br />while((choice = getchar () ) !=EOF){ /*loop until user types the End of File */<br /><br />switch(choice){<br /><br />case 'A':<br />case 'a':<br />printf("nEnter a number: n");<br />scanf("%d", &number);<br />number = number * number;<br />printf("Your answer is: n", &number);<br />break;<br /><br />case 'B':<br />case 'b':<br />printf("nEnter a number: n");<br />scanf("%d", &number);<br />sqrt(number);<br />printf("Your answer is: n", &number);<br /><br />break;<br /><br />case 'C':<br />case 'c':<br />printf("nEnter a number: n");<br />scanf("%d", &number);<br />number=number*number*number;<br /><br />printf("Your answer is: n", &number);<br /><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 />}/*end while function*/<br /><br />return 0;<br /><br />}<br /><br />my problem is that when you enter any choice it work but when it asks you to enter a number and when you enter the number it prints “incorrect choiced entered”. Whats the problem?
- November 3, 2008 at 7:26 pm #3471GWILouisaxwzklaParticipant
This seems to work:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687<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 />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.