2016-12-05 4 views
-2

По какой-то причине я получаю сообщение об ошибке C2448 от визуальных студий Microsoft. Я пытаюсь вызвать функцию, но она не хочет работать для меня. Кто-нибудь видит, что не так? Я пытаюсь вызвать 3 функции во время инструкции while, если внутри нее, но я думал, что это как вызвать функцию, но она не работает для меня. Любой вход оценивается.Проблема с вызовом функции 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

Как вы думаете, '&&' делает? У вас есть [хорошая книга на C++] (http://stackoverflow.com/q/388242/253056), к которой вы можете легко обратиться? –

+0

&& - логический оператор И. Не уверен, что вы пытаетесь сделать, но это, безусловно, не принадлежит тому, где вы его используете. – OldProgrammer

+1

Как указывают другие, 'int endt (cbal && sarge && loopend)' не имеет никакого смысла. Как вы думаете, что это значит? –

ответ

0

Так, так как вы сказали мне, что ваши функции должны делать я разместить правильную форму вашего кода:

#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"; 
} 

Вы также можете использовать глобальные переменные:

#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"; 
} 

Я надеюсь, что решает вашу проблему.

+0

, однако, я только хотел, чтобы оператор if выполнялся один раз, если условия были правильными, однако я думаю, что я понял это – Nickolas

1

Если вы хотите сделать объявить функцию с двумя параметрами, вы должны использовать вместо & & вы написали в своих декларациях функций. В вашем случае это должно выглядеть следующим образом:

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

Однако, вы объявили cbal и scharge как глобальные переменные, так что ваши функции не должны даже взять что-нибудь в качестве параметра, вы можете написать только:

int deposit() 
{ 
    //some code 
} 

Кроме того, ваша функция ничего не возвращает, вы действительно хотите сделать их int, а не недействительными?

EDIT: Я думаю, что знаю, что вы хотите. Если вы объявите свои переменные как локальные в основном цикле, а затем передадите их функции void, будут созданы копии ваших переменных, и их значения будут меняться только в этой функции, в основном цикле они не изменятся. Затем вы можете захотеть передать ссылки на них вместо этих переменных, чтобы изменить их значение также вне функции void. Затем вы должны определить его как:

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

Но тогда, если вы вызываете функцию, вы передаете параметры обычно:

deposit(cbal, scharge); 
+0

добавил возврат функций и изменил их так, как у вас было, но я все равно получаю ошибки – Nickolas

+0

Когда вы вызываете функцию, вам не следует писать типы переменных снова. Удалите «float» и повторите попытку. – Executor1909

+0

Также вы не можете вернуть два значения из функции, он вернет только первый, который вы написали. – Executor1909

Смежные вопросы