2016-06-25 16 views
-5

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 '='? 
+0

"P" für Vollzeit und "P" für Teilzeit ".... was? –

+0

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

+1

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

Antwort

-1

scanf_s ist Microsoft-spezifische verwenden Sie, dass Sie es verwenden müssen? Wenn ich mit scanf und dem Compiler versuche, scheint die erste Version des Programms zu arbeiten, und die Standardanweisung wird nicht gedruckt, wenn das Programm ausgeführt wird.

#include<stdio.h> 

int main() { 
    int yos; 
    double salary; 
    char time; 

    printf("Please enter your employee status, 'F' for Fulltime and 'P' for Parttime: \n"); 
    scanf("%c", &time); 
    printf("Please enter your year of service: \n"); 
    scanf("%d", &yos); 
    printf("Please enter your current salary: \n"); 
    scanf("%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); 
} 

-Test

Please enter your employee status, 'F' for Fulltime and 'P' for Parttime: 
F 
Please enter your year of service: 
6 
Please enter your current salary: 
200 

Your new salary is 210.00 
+0

ive änderte die Einstellung von Pre-Prozessor und versuchte sogar #define _CRT_SECURE_NO_WARNINGS und sogar beide. Kann wirklich die scanf aus Sicherheitsgründen verwenden. Übrigens danke für die Hilfe zu allen Antworten –

0

Ich entschied mich scanf statt scanf_s zu verwenden, ziehe ich es auch Aufzählungen und ganzen Zahlen in switch-Anweisungen verwenden. Die folgende Bearbeitung Ihres Codes funktionierte für mich;

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

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

    int status; 
    if(time=='F'||time=='f') 
     status = 1; 
    if(time=='P'||time=='p') 
     status = 2; 
    switch (status) 
    { 
    case 1: 
     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 2: 
     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); 
} 
0

Bei Verwendung des %c Formatspezifizierer, müssen Sie die Anzahl der Zeichen einen zusätzlichen Parameter geben, die Angabe zu lesen. Von der MSDN page for scanf_s:

Gegensatz scanf und wscanf, scanf_s und wscanf_s erfordern die Puffergröße für alle Eingangsparameter des Typs C, C, S, S, oder String Steuersätze angegeben werden, die eingeschlossen sind in [ ]. Die Puffergröße in Zeichen wird als zusätzlicher Parameter übergeben, der unmittelbar auf den Zeiger des Puffers oder der Variablen folgt.

...

Im Fall von Zeichen, ein einzelnes Zeichen kann wie folgt gelesen werden:

char c; 

scanf_s("%c", &c, 1); 

Weil Sie in der erforderlichen Anzahl von Parametern nicht passieren, Sie rufen undefined behavior.So

, wenn Sie in time lesen, tun Sie es wie folgt aus:

scanf_s("%c", &time, 1); 

Auch in Ihrem zweiten Beispiel, das ist ungültige Syntax:

if (char time == 'F' && yos >= 5) 

hier der char Stichwort Befreien Sie sich.

+0

Warum der Downvote? – dbush