Thread: i need alil help with my switch question

  1. #16
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    never expect anyone to write a program for you in this forum, but since i see you've at least been trying i'll help you out.

    Code:
    #include <stdio.h> 
    
    int main() 
    {
      int swt=0, num1=0, num2=0, num3=0, num4=0, num5=0;
      float awn1=0, awn2=0, awn3=0, awn4=0, awn5=0, total;
     
      for(;;)  //infinite for statemtn basically a loop which never ends
      { 
        printf ("\nEnter the product number(-1 to end): "); 
        scanf ("%d", &swt );
        if(swt==-1)
          break;
      
        switch ( swt )
        { 
          case 1: 
            printf ("Enter the amount for No.1: "); 
            scanf ("%d",&num1 ); 
            awn1 = num1 * 2.98;
            break; 
    
          case 2: 
            printf ("Enter the amount for No.2: "); 
            scanf ("%d", &num2 ); 
            awn2 = num2 * 4.50; 
            break; 
    
          case 3: 
            printf ("Enter the amount for No.3: "); 
            scanf ("%d", &num3 ); 
            awn3 = num3 * 9.98; 
            break; 
    
          case 4: 
            printf ("Enter the amount for No.4: "); 
            scanf ("%d", &num4 ); 
            awn4 = num4 * 4.49; 
            break; 
          
          case 5: 
            printf ("Enter the amount for No.5: "); 
            scanf ("%d", &num5 ); 
            awn5 = num5 * 6.87; 
            break; 
    
          default: 
            printf ("Incorrect number\n"); 
            printf ("Enter your product number:"); 
        } 
      } 
    
      total = awn1+awn2+awn3+awn4+awn5; 
      printf ("\nThe total for you products is: %.2f",total); //total is float!
                    //%.2f prints the floating point total with 2 decimal places
      getch(); 
      return 0; 
    }
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A cut down version, showing the switch statement.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
    	int  num1;
    	clrscr();
    
    	printf("Enter choice, 1 or 2: (x to exit) ");
    	while (scanf("%d", &num1) == 1)
    	{
    		switch (num1)
    		{
    			case 1:	
    				printf("Doing stuff for 1\n");
    				break;
    			case 2:	
    				printf("Doing stuff for 2\n");
    				break;
    			default:
    				printf("Doing stuff for default (ie Not 1 or 2)\n");
    				break;
    		}
    		printf("Enter choice, 1 or 2: (x to exit) ");
    	}
    
    	printf ("All done, press any key\n");
    	getch();
    	return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    lol i beat you by a fraction of a sec
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. easy question about switch
    By zdzislavv in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2008, 09:20 AM
  3. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM