故障调用函数C
Trouble calling a function c++
由于某种原因,我从Microsoft Visual Studios那里获得了错误C2448。我正在尝试调用一个功能,但它不想为我工作。有人看到怎么了吗?我试图在while语句中调用3个函数,但是我认为这是如何调用函数的方法,但是它对我不起作用。任何输入都将不胜感激。
#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);
}
如果要声明具有两个参数的函数,则应使用A,而不是&amp;&amp;您已经写了您的功能声明。在您的情况下,应该看起来像这样:
int deposit(float cbal, float scharge)
{
//some code
}
但是,您已将CBAL和SCHARGE声明为全局变量,因此您的功能甚至不必将任何内容作为参数,您只能编写:
int deposit()
{
//some code
}
此外,您的功能不返回任何内容,您真的想让它们int而不是无效?
编辑:我想我知道你想做什么。如果将变量声明为主循环中的本地变量,然后将它们传递到void函数,则将创建变量的副本,其值仅在该函数中会更改,在主循环中它们不会更改。然后,您可能需要将引用传递给它们,而不是将这些变量传递给它们,以使它们的价值更改也在void函数之外。然后,您必须将其定义为:
void deposit (float& cbal, float& scharge)
{
//some code
}
但是,如果您调用函数,则正常通过参数:
deposit(cbal, scharge);
因此,由于您确切地告诉我您的功能应该做什么,因此我要发布正确的代码形式:
#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";
}
我希望能解决您的问题。
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销