Home Forums C Programming Help with switch

Viewing 1 reply thread
  • Author
    Posts
    • #2150
      RandolpNolen
      Participant

      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)



      #include
      #include

      int main(void)
      {
      int choice;
      int number;
      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. nn");


      while((choice = getchar () ) !=EOF){ /*loop until user types the End of File */

      switch(choice){

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

      case 'B':
      case 'b':
      printf("nEnter a number: n");
      scanf("%d", &number);
      sqrt(number);
      printf("Your answer is: n", &number);

      break;

      case 'C':
      case 'c':
      printf("nEnter a number: n");
      scanf("%d", &number);
      number=number*number*number;

      printf("Your answer is: n", &number);

      break;

      default:
      printf("n
      ");
      printf("nIncorrect choice entered. ");
      printf("n
      nn");
      printf("enter a choice: ");
      break;
      }/*end switch function*/
      }/*end while function*/

      return 0;

      }

      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?

    • #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;
      }


Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.