Der Code, den ich geschrieben habe:Scanning und Combining Switch-Anweisung mit If-Anweisung? Schalter (Zeit)
#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);
}
aus irgendeinem Grund, wenn ich das Programm ausführen, ich diese Ausgabe erhalten:
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
tut dieses Problem auftreten, weil es schräge das Zeichen scannen? Ich habe sogar versucht,% c zu spacen. Ich denke auch nicht, dass das Setzen von% s oder% [^ \ n] von Nutzen ist, da es sich nur um 1 Zeichen handelt. bitte jemand mir helfen?
Ich habe auch verschiedene Code, beinhalten nur, wenn Aussagen wie versucht:
#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);
}
Aber dieses ist was
error c2143 missing ',' before '==' at line 15, 21, 27...
gibt es auch dies:
Warning 11 warning C4553: '==' : operator has no effect; did you intend '='?
"P" für Vollzeit und "P" für Teilzeit ".... was? –
In der zweiten Version Ihres Codes sollte es sonst sein, wenn (time == 'P' && yos <5) 'anstelle von 'sonst wenn (char time ==' P '&& yos <5)'. Beachten Sie das zusätzliche Wort "char". – sameerkn
Sie möchten die Dokumentation zu 'scanf_s()' lesen: https://msdn.microsoft.com/en-us/library/w40768et.aspx ("* Im Gegensatz zu' scanf' [...] scanf_s [...] ] requires [s] die Puffergröße, die angegeben werden soll für ... * " – alk