samedi 25 juin 2016

Scanning char and Combining Switch statement with If statement? switch(time) {case 'F' etc

The code that I've wrote:

#include<stdio.h>
int main()
{
    int yos;
    double salary;
    char time;

    printf("Please enter your employee status, 'P' for Fulltime and 'P' for Parttime: \n");
    scanf_s("%c", &time);
    printf("Please enter your year of service: \n");
    scanf_s("%d", &yos);
    printf("Please enter your current salary: \n");
    scanf_s("%lf", &salary);

    switch (time)
    {
    case 'F':
    case 'f':
        if (yos >= 5)
        {
            salary = (salary*5.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        else if (yos < 5 )
        {
            salary = (salary*4.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);   
        }
        break;

    case 'P':
    case 'p':
        if (yos >= 5)
        {
            salary = (salary*3.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        else if (yos < 5 )
        {
            salary = (salary*2.5 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);       
        }
        break;
    default:
        printf("Please put the details correctly\n");
    }

    return(0);
}

for some reason, when i run the program, I got this output:

Please enter your employee status, 'P' for Fulltime and 'P' for Parttime:
F
Please enter your year of service:
6
Please enter your current salary:
200
Please put the details correctly
Press any key to continue

does this problem occur because it cant scan the char? i even tried spacing the %c. i also dont think that putting %s or %[^\n] will be of any use since its only involve 1 character. please somebody help me?

Ive also tried different code which involve only if statement such as:

#include<stdio.h>
int main()
{
    int yos;
    double salary;
    char time;

    printf("Please enter your employee status, 'P' for Fulltime and 'P' for Parttime: \n");
    scanf_s("%c", &time);
    printf("Please enter your year of service: \n");
    scanf_s("%d", &yos);
    printf("Please enter your current salary: \n");
    scanf_s("%lf", &salary);

    if (char time == 'F' && yos >= 5)
    {
        salary = (salary*5.0 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }

    else if (char time == 'F' && yos < 5)
    {
        salary = (salary*4.0 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }

    if (char time == 'P' && yos >= 5)
    {
        salary = (salary*3.0 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }

    else if (char time == 'P' && yos < 5)
    {
        salary == (salary*2.5 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }
    return(0);
}

But, this one is giving error c2143 missing ',' before '==' at line 15, 21, 27...

there is also this: Warning 11 warning C4553: '==' : operator has no effect; did you intend '='?

your help is greatly appreciated'

Aucun commentaire:

Enregistrer un commentaire