2016-05-10 9 views
-1
#include<iostream> 
#include<conio.h> 
using namespace std; 
int main(){ 

    int salary; 
    float deduction,netpayable; 
    switch(salary/10000){ 
     cout<<"enter salary amount :"; 
     cin>>salary; 
     case 0:  
     deduction=0; 
     netpayable = salary; 
     cout<<"netpayable salary is salary-deduction ="<<netpayable; 
     break; 

     case 1:  
     deduction=1000; 
     netpayable = salary-deduction; 
     cout<<"netpayable salary is salary-deduction ="<<netpayable; 
     break; 

     default:  
     deduction=salary*(7/100); 
     netpayable = salary-deduction; 
     cout<<"netpayable salary is salary-deduction ="<<netpayable; 
     break; 
    } 
    system("pause"); 
} 

as image is shown no errors have been detected but its not giving the required outeinfaches Programm C++ Anweisung wechselt die Mitarbeitergehälter

i beschäftigt haben zu finden, und ich mag ein einfaches Programm machen, in dem ich switch-Anweisung bin mit dem Gehalt der verschiedenen Mitarbeiter abzuziehen die über 10.000 Rs so weiter ... aber der Compiler hat keinen Fehler gezeigt, aber das Programm läuft nicht und gibt eine Ausgabe wie im Bild gezeigt, ich bin etwas verwirrt darin.

+2

die Warnmeldung Sie sehen werden sollten .. http://melpon.org/wandbox/permlink/mr5zv3DchszVMB3T – xaxxon

+0

was netpayable ist = Lohn-Abzug; Was ist Lohnabzug? – shami

+0

@shami, das muss "Gehalt" sein * minus * "Abzug" – CinCout

Antwort

3

Sie haben switch unter salary hinzugefügt, ohne der Variablen einen Wert zuzuweisen. Dies führt dazu, dass salary einen Müllwert aufweist.

Einfach diese Zeilen außerhalb switch:

cout<<"enter salary amount :"; 
cin>>salary; 

// now start the switch statement here: 
switch(...) 
{ 
    .... 
} 

Auf diese Weise werden Sie zuerst den Benutzer auffordert, die salary, einzugeben und später gewünschten Operationen auf, es zu tun.

+0

vielen Dank, wie enttäuschend ich bin. –

+0

@RaheelAlmis warum bist du enttäuscht? Hast du versucht, was ich vorgeschlagen habe? – CinCout

+1

Ja, es funktioniert Bruder Ich habe alle Fehler korrigiert. thx –

2

Ich sehe 3 Fehler in Ihrem Code. Ich habe deinen Code korrigiert und Kommentare geschrieben, um sie hervorzuheben.

Bitte siehe unten:

#include<iostream> 
#include<conio.h> 
using namespace std; 

int main(){ 

    // 1) Declare all variables of same type to avoid implicit casting errors. 
    // In this case we need float or double types. 
    float salary; 
    float deduction; 
    float netpayable; 

    // 2) This block must be out of switch instruction! 
    cout<<"enter salary amount :"; 
    cin>>salary; 

    // 1.1) The switch will do the expected job 
    // only if it works on a int variable, so I put an explicit cast 
    // to (int). 
    switch((int)(salary/10000)){ 

    case 0:  
     deduction=0; 
     netpayable = salary; 
     cout<<"netpayable salary is salary-deduction ="<<netpayable; 
     break; 

    case 1:  
     deduction=1000; 
     netpayable = salary-deduction; 
     cout<<"netpayable salary is salary-deduction ="<<netpayable; 
     break; 

    default: 
     // 3) (7/100) = 0 because compiler interprets it as integer. 
     // Use (7./100.) instead. 
     deduction=salary*(7./100.); 
     netpayable = salary-deduction; 
     cout<<"netpayable salary is salary-deduction ="<<netpayable; 
     break; 
    } 

    system("pause"); 
}