Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    272
    Thanks
    8
    Thanked 1 Time in 1 Post

    C Programming Switch Help

    I'm Getting the following error and I don't know whats wrong with my code?
    Attached Thumbnails Attached Thumbnails C Programming Switch Help-untitled.jpg  

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    For your purposes, C only supports integer values for labels.

    You may want to actually resolve those characters to their ASCII integer representation, then substitute those in.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    what do you intend to do in in this program?

    in switch statements you need to use integer values... not characters...

    example:
    Code:
    #include<stdio.h>
    int main()
    {
    		int input;
    
    		printf("please choose a number (1, 2, 3) :");
    		scanf("%d", &input);
    
    		switch (input)
    		{
    			case 1:
    				printf("You chose number 1.");
    				break;
    			case 2:
    				printf("You selected number 2.");
    				break;
    			case 3:
    				printf("You like number 3?");
    				break;
    			default:
    				printf("You entered an invalid number!");
    		}
    		fflush(stdin);
    		getchar();
    		return 0;
    }

  • #4
    Regular Coder
    Join Date
    Oct 2010
    Posts
    272
    Thanks
    8
    Thanked 1 Time in 1 Post
    I'm buiilding a payroll program. Can anyone tell me why it is crashing after I enter hourly rate?
    And also If I were to add more than one employee I probably couldn't use same variable like in case 2:
    Any suggestions for better approach or am I doing it right?

    Code:
    
    int main(){
        
        int input;
        char lastName[30];
        char firstName[30];
        double rate;
        int hours;
        
        printf("Payroll Program Menu\n\nAdd New Employee(type '1')\n\nPrint the Paycheck(type '2')\n\nExit Program(type '3')\n");
        scanf ("%d", &input);
        switch (input){
               case 1:
               printf ("Add New Employee\n");
               printf ("\nPlease Enter First Name\n");
               scanf("%s", firstName);
               printf ("\nPlease Enter Last Name\n");
               scanf("%s", lastName);
               printf ("\nPlease Enter Hourly Rate\n");
               scanf("%lf", rate);
                 printf ("\nPlease Enter Hours Worked\n");
               scanf("%d", hours);
               
               break;
               case 2:
               printf ("Please enter employee's last name\n");
               scanf ("&s", lastName);
               break;
       case 3: 
           printf ("Goodbye\n");
           break;
          }
        
        
      
        
        system ("pause");
        }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •