出于某种原因,我从微软视觉工作室收到错误C2448。我试图调用一个函数,但它不想为我工作。有谁看到有什么问题?我试图在while语句中调用3个函数,并在其中使用if但是我认为这是如何调用函数但是它对我不起作用。任何输入都表示赞赏。
#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);
}
答案 0 :(得分:1)
如果你想要声明一个带有两个参数的函数,你应该使用a而不是&amp;&amp;你已经写了你的函数声明。在你的情况下,它应该是这样的:
int deposit(float cbal, float scharge)
{
//some code
}
但是,您已将cbal和scharge声明为全局变量,因此您的函数甚至不需要将任何内容作为参数,您只能编写:
int deposit()
{
//some code
}
另外,你的函数不会返回任何内容,你真的想让它们成为int,而不是void吗?
编辑: 我想我知道你想做什么。如果在主循环中将变量声明为局部变量然后将它们传递给void函数,则会创建变量的副本,它们的值将仅在该函数中更改,在主循环中它们不会更改。然后,您可能希望将引用传递给它们而不是那些变量,以使其值也在void函数之外更改。然后你必须将其定义为:
void deposit (float& cbal, float& scharge)
{
//some code
}
但是如果你调用这个函数,你可以正常传递参数:
deposit(cbal, scharge);
答案 1 :(得分: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";
}
我希望能解决你的问题。