...

View Full Version : C Programming Switch Help



andynov123
09-05-2011, 07:12 AM
I'm Getting the following error and I don't know whats wrong with my code?

ShaneC
09-05-2011, 07:58 AM
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.

dlep2x2
09-05-2011, 08:31 AM
what do you intend to do in in this program?

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

example:


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

andynov123
09-05-2011, 06:47 PM
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?





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");
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum