2016-12-05 4 views
-2

Aus irgendeinem Grund bekomme ich Fehler C2448 von Microsoft Visual Studios. Ich versuche eine Funktion anzurufen, will aber nicht für mich arbeiten. Sieht jemand was falsch ist? Ich versuche, 3 Funktionen während der while-Anweisung mit einem if innerhalb von es jedoch ich dachte, das war, wie eine Funktion aufrufen, aber es funktioniert nicht für mich. Jede Eingabe wird geschätzt.Problem beim Aufruf einer Funktion C++

#include <string> 
#include <iostream> 

using namespace std; 

//int check (cbal && scharge); 
//int deposit (cbal && scharge); 
//int endt (cbal && scharge && loopend); 

int main() 
{ 
float cbal; 
float scharge; 
float bal; 
char selection; 
int loopend; 
loopend = 1; 
cout << "Transactions will take the form of a letter followed by a dollar amount. " << 
    "Valid letters are “C” for a check, “D” for a deposit, and “E” for the ending " << 
    "transaction(use zero on this transaction). Press <Enter> after each line of input!" << endl; 
cout << "Please enter inital balance: "; 
cin >> bal; 
bal = cbal; 
while(loopend == 1) 
{ 
    cout << "Please enter a transaction" << endl; 
    cin >> selection; 
    if (selection == 'C' || selection == 'c') 
    { 
     int check (float cbal, float scharge); 
    } 
    else if (selection == 'D' || selection == 'd') 
    { 
     int deposit (float cbal, float scharge); 
    } 
    else if (selection == 'E' || selection == 'e') 
    { 
     int endt (float cbal,float scharge, int loopend); 
    } 
    else 
    { 
     cout << "Please enter a valid transaction."; 
    } 
} 
return 0; 
} 

int check (float cbal, float scharge) 
{ 
int transaction; 
bool flag; 
scharge = scharge + .15; 
cout << "What is the check amount?" << endl; 
cin >> transaction; 
cbal = cbal - transaction; 
cout << "Transaction ammount: " << transaction << endl 
    << "Current Balance: " << cbal << endl 
    << "Service Charge Check: $0.15" << endl; 
if (cbal < 500 && flag == false) 
{ 
    scharge = scharge + 5; 
    flag = true; 
    cout << "Service Charge Below $500: $5.00"; 
} 
cout << "Total Service Charges: " << scharge; 
return (cbal); 
return (scharge); 
} 

int deposit(float cbal, float scharge) 
{ 
int transaction; 
scharge = scharge + .10; 
cout << "What is the deposit amount?" << endl; 
cin >> transaction; 
cbal = cbal + transaction; 
cout << "Deposit amount: " << transaction << endl 
    << "Current Balance: " << cbal << endl 
    << "Service Charge Deposit: $0.10" << endl 
    << "Total Service Charges: " << scharge; 
return (cbal); 
return (scharge); 
} 

int endt (float cbal, float scharge, int loopend) 
{ 
int transaction; 
cout << "Enter transaction amount: "; 
cin >> transaction; 
if (transaction == 0) 
{ 
    cout << "Transaction: End" << endl 
     << "Current Balance: " << cbal << endl 
     << "Total Service Charges: " << scharge << endl; 
    cbal = cbal - scharge; 
    cout << "Final Balance: " << cbal; 
    loopend = 2; 
} 
else 
    cout << "Error: 0 was not the transaction amount"; 
return (cbal); 
return (scharge); 
return (loopend); 
} 
+7

Was denkst du '&&' tut? Haben Sie [ein gutes C++ Buch] (http://stackoverflow.com/q/388242/253056), auf das Sie sich leicht beziehen können? –

+0

&& ist der logische UND-Operator. Ich bin mir nicht sicher, was Sie tun wollen, aber es gehört sicherlich nicht dorthin, wo Sie es verwenden. – OldProgrammer

+1

Wie andere aufzeigen, macht 'int endt (cbal &&ladung && loopend)' keinen Sinn. Was denkst du, was das bedeutet? –

Antwort

0

So, da Sie mir gesagt, genau das, was Ihre Funktionen tun sollten, ich bin die richtige Form des Codes veröffentlichen:

#include <string> 
#include <iostream> 

using namespace std; 

//at first you have to at least declare the functions, you can also define them here or at the end of the code (as I did) 

void check(float&, float&); 
void deposit(float&, float&); 
void endt(float&, float&, int&); 

int main() 
{ 
    float cbal; 
    float scharge; 
    float bal; 
    char selection; 
    int loopend = 1; //You can declare a variable with a value 
    cout << "Transactions will take the form of a letter followed by a dollar amount. " << 
     "Valid letters are “C” for a check, “D” for a deposit, and “E” for the ending " << 
     "transaction(use zero on this transaction). Press <Enter> after each line of input!" << endl; 
    cout << "Please enter inital balance: "; 
    cin >> bal; 
    cbal = bal; //Probably you wanted to assign the bal value to cbal, not bal = cbal 
    while (loopend == 1) 
    { 
     cout << "Please enter a transaction" << endl; 
     cin >> selection; 
     if (selection == 'C' || selection == 'c') 
     { 
      check(cbal, scharge); 
     } 
     else if (selection == 'D' || selection == 'd') 
     { 
      deposit(cbal, scharge); 
     } 
     else if (selection == 'E' || selection == 'e') 
     { 
      endt(cbal, scharge, loopend); 
     } 
     else 
     { 
      cout << "Please enter a valid transaction."; 
     } 
    } 
    return 0; 
} 

void check(float& cbal, float& scharge) 
{ 
    int transaction; 
    bool flag = false; //probably you wanted that to be false at the beggining(?) 
    scharge += .15F; //shorter form of scharge = scharge + .15; 
    cout << "What is the check amount?" << endl; 
    cin >> transaction; 
    cbal -= transaction; //same shorter form 
    cout << "Transaction ammount: " << transaction << endl 
     << "Current Balance: " << cbal << endl 
     << "Service Charge Check: $0.15" << endl; 
    if (cbal < 500 && flag == false) 
    { 
     scharge = scharge + 5; 
     flag = true; 
     cout << "Service Charge Below $500: $5.00"; 
    } 
    cout << "Total Service Charges: " << scharge; 
} 

void deposit(float& cbal, float& scharge) 
{ 
    int transaction; 
    scharge += .10F; 
    cout << "What is the deposit amount?" << endl; 
    cin >> transaction; 
    cbal += transaction; 
    cout << "Deposit amount: " << transaction << endl 
     << "Current Balance: " << cbal << endl 
     << "Service Charge Deposit: $0.10" << endl 
     << "Total Service Charges: " << scharge; 
} 

void endt(float& cbal, float& scharge, int& loopend) 
{ 
    int transaction; 
    cout << "Enter transaction amount: "; 
    cin >> transaction; 
    if (transaction == 0) 
    { 
     cout << "Transaction: End" << endl 
      << "Current Balance: " << cbal << endl 
      << "Total Service Charges: " << scharge << endl; 
     cbal -= scharge; 
     cout << "Final Balance: " << cbal; 
     loopend = 2; 
    } 
    else 
     cout << "Error: 0 was not the transaction amount"; 
} 

Sie können auch globale Variablen verwenden:

#include <string> 
#include <iostream> 

using namespace std; 

void check(); 
void deposit(); 
void endt(); 

float cbal, scharge, bal; 
int loopend = 1; 

int main() 
{ 
    char selection; 
    cout << "Transactions will take the form of a letter followed by a dollar amount. " << 
     "Valid letters are “C” for a check, “D” for a deposit, and “E” for the ending " << 
     "transaction(use zero on this transaction). Press <Enter> after each line of input!" << endl; 
    cout << "Please enter inital balance: "; 
    cin >> bal; 
    cbal = bal; //Probably you wanted to assign the bal value to cbal, not bal = cbal 
    while (loopend == 1) 
    { 
     cout << "Please enter a transaction" << endl; 
     cin >> selection; 
     if (selection == 'C' || selection == 'c') 
     { 
      check(); 
     } 
     else if (selection == 'D' || selection == 'd') 
     { 
      deposit(); 
     } 
     else if (selection == 'E' || selection == 'e') 
     { 
      endt(); 
     } 
     else 
     { 
      cout << "Please enter a valid transaction."; 
     } 
    } 
    return 0; 
} 

void check() 
{ 
    int transaction; 
    bool flag = false; //probably you wanted that to be false at the beggining(?) 
    scharge += .15F; //shorter form of scharge = scharge + .15; 
    cout << "What is the check amount?" << endl; 
    cin >> transaction; 
    cbal -= transaction; //same shorter form 
    cout << "Transaction ammount: " << transaction << endl 
     << "Current Balance: " << cbal << endl 
     << "Service Charge Check: $0.15" << endl; 
    if (cbal < 500 && flag == false) 
    { 
     scharge = scharge + 5; 
     flag = true; 
     cout << "Service Charge Below $500: $5.00"; 
    } 
    cout << "Total Service Charges: " << scharge; 
} 

void deposit() 
{ 
    int transaction; 
    scharge += .10F; 
    cout << "What is the deposit amount?" << endl; 
    cin >> transaction; 
    cbal += transaction; 
    cout << "Deposit amount: " << transaction << endl 
     << "Current Balance: " << cbal << endl 
     << "Service Charge Deposit: $0.10" << endl 
     << "Total Service Charges: " << scharge; 
} 

void endt() 
{ 
    int transaction; 
    cout << "Enter transaction amount: "; 
    cin >> transaction; 
    if (transaction == 0) 
    { 
     cout << "Transaction: End" << endl 
      << "Current Balance: " << cbal << endl 
      << "Total Service Charges: " << scharge << endl; 
     cbal -= scharge; 
     cout << "Final Balance: " << cbal; 
     loopend = 2; 
    } 
    else 
     cout << "Error: 0 was not the transaction amount"; 
} 

Ich hoffe, dass das Ihr Problem löst.

+0

zu bekommen, aber ich wollte nur die if-Anweisung einmal passieren, wenn die Bedingungen korrekt waren, aber ich denke, dass ich das herausgefunden – Nickolas

1

Wenn Sie eine Funktion mit zwei Parametern deklarieren wollen, sollten Sie ein, anstelle von & & Sie in Ihrer Funktion Erklärungen geschrieben haben. In Ihrem Fall sollte es so aussehen:

int deposit(float cbal, float scharge) 
{ 
    //some code 
} 

Sie haben jedoch erklärt CBAL und scharge als globale Variablen, so dass Ihre Funktionen auch nichts als Parameter nehmen, können Sie schreiben nur:

int deposit() 
{ 
    //some code 
} 

Darüber hinaus gibt Ihre Funktion nichts zurück, möchten Sie sie wirklich int machen, nicht void?

EDIT: Ich denke, ich weiß, was Sie tun möchten. Wenn Sie Ihre Variablen in der Hauptschleife als lokal deklarieren und sie dann an die void-Funktion übergeben, werden Kopien Ihrer Variablen erstellt und ihre Werte ändern sich nur in dieser Funktion, in der Hauptschleife ändern sie sich nicht. Dann möchten Sie möglicherweise die Referenzen an sie anstelle dieser Variablen übergeben, damit sich ihr Wert auch außerhalb der void-Funktion ändert. Dann müssen Sie es wie definieren:

void deposit (float& cbal, float& scharge) 
{ 
    //some code 
} 

Aber dann, wenn Sie die Funktion aufrufen, übergeben Sie die Parameter normal:

deposit(cbal, scharge); 
+0

hinzugefügt die Rückkehr zu den Funktionen und änderte sie auf die Art, wie Sie es hatten, aber immer noch bekomme die Fehler – Nickolas

+0

Wenn Sie eine Funktion aufrufen, sollten Sie nicht wieder die Typen der Variablen schreiben. Löschen Sie "float" und versuchen Sie es erneut. – Executor1909

+0

Sie können auch nicht zwei Werte von einer Funktion zurückgeben, sondern nur die erste, die Sie geschrieben haben. – Executor1909

Verwandte Themen