gpt4 book ai didi

c++ - 不理解通过函数传递的 C++ 参数

转载 作者:行者123 更新时间:2023-11-28 05:42:34 25 4
gpt4 key购买 nike

我是 C++ 的新手,我想了解如何通过多个函数传递变量。我知道您可以使用全局变量或参数传递。

在我的代码中,我在主函数之后定义了两个函数。我已经用原型(prototype)设置了它。我在 CodeBlocks 中收到的错误是 .error: 'studentFees' was not declared in this scope。这是有道理的,因为我没有在 123124 行中实现任何内容。但我不太确定如何。我也对正确实现原型(prototype)感觉不太好。

有人能帮我解决这个问题吗?

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

void undergradBill(
double finalBill = 0;
double studentTuition,
double studentFees,
double studentID,
string studentLevel,
string studentBio,
string studentRes,
string studentLife,
string studentCredit
);
void gradBill(
double finalBill = 0;
double studentTuition,
double studentFees,
double studentID,
string studentLevel,
string studentBio,
string studentRes,
string studentLife,
string studentCredit
);

int main()
{
double finalBill = 0;
double studentTuition = 0;
double studentFees = 0;
double studentID = 0;
string studentLevel = "";
string studentBio = "";
string studentRes = "";
string studentLife = "";
string studentCredit = "";

cout << "Welcome College Student!" << endl;
cout << "This program is designed to assist you in calculating your college tuition per semester." << endl;
cout << endl;
cout << "Please provide the following information:" << endl;
cout << " -Student ID" << endl;
cout << " -Graduate/Undergraduate" << endl;
cout << " -Residency" << endl;
cout << " -Major" << endl;
cout << " -Full Time/Part Time" << endl;
cout << " -Credits taken this semester" << endl;
cout << endl;
system("PAUSE");

system("CLS");
cout << "Please enter your student ID." << endl;
cout << "Student ID: ";
cin >> studentID;
while(cin.fail()) {
cout << "Error: please enter a valid entry." << endl;
cin.clear();
cin.ignore(256,'\n');
cout << "Student ID :";
cin >> studentID;
}

cout << endl;
cout << "Are you a graduate or undergraduate student?" << endl;
cout << "(G/U) :";
cin.get();
getline(cin, studentLevel);
while(studentLevel != "g" && studentLevel != "G" && studentLevel != "u" && studentLevel != "U") {
cout << "Error: please enter a valid entry." << endl;
cout << "(G/U) :";
getline(cin, studentLevel);
}
if(studentLevel == "g" || studentLevel == "G") {
cout << endl;
cout << "Are you apart of the biology program?" << endl;
cout << "(Y/N) :";
getline(cin, studentBio);
while(studentBio != "y" && studentBio != "Y" && studentBio != "n" && studentBio != "N") {
cout << "Error: please enter a valid entry." << endl;
cout << "(Y/N) :";
getline(cin, studentBio);
}
}

cout << endl;
cout << "Are you a resident or New York State?" << endl;
cout << "(Y/N) :";
getline(cin, studentRes);
while(studentRes != "y" && studentRes != "Y" && studentRes != "n" && studentRes != "N") {
cout << "Error: please enter a valid entry" << endl;
cout << "(Y/N) :";
getline(cin, studentRes);
}

cout << endl;
cout << "Are you a full time student or a part time student?" << endl;
cout << "(F/P) :";
getline(cin, studentLife);
while(studentLife != "f" && studentLife != "F" && studentLife != "p" && studentLife != "P") {
cout << "Error: please enter a valid entry." << endl;
cout << "(F/P) :";
getline(cin, studentLife);
}
if (studentLife == "p" || studentLife == "P") {
cout << endl;
cout << "How many credit hours are you taking this semester?" << endl;
cout << "Credit Hours :";
cin >> studentCredit;
while(cin.fail()) {
cout << "Error: please enter a valid entry." << endl;
cin.clear();
cin.ignore(256,'\n');
cout << "Credit Hours :";
cin >> studentCredit;
}
}

if(studentLevel == "u" || studentLevel == "U" || studentLife == "p" || studentLife == "P") {undergradBill();}
else {gradBill();}

system("CLS");
finalBill = studentTuition + studentFees;
cout << "Student Account: " << studentID << endl;
cout << "Billing Total: " << finalBill << endl;
}

void undergradBill() {
if(studentLife == "f" || studentLife == "F") {
if(studentRes == "y" && studentRes == "Y") {
studentTuition = 3085.00;
studentFees = 588.50;
}
else {
studentTuition = 7910.00;
studentFees = 588.50;
}
}
else {
if(studentRes == "y" && studentRes == "Y") {
studentTuition = 257.00;
studentFees = studentCredit * 48.95;
}
else {
studentTuition = 659.00;
studentFees = studentCredit * 48.95;
}
}
}

void gradBill() {
if(studentBio == "y" || studentBio == "Y") {
if(studentLife == "f" || studentLife == "F") {
if(studentRes == "y" && studentRes == "Y") {
studentTuition = 5185.00;
studentFees = 342.14 + 900.00;
}
else {
studentTuition = 10095.00;
studentFees = 342.14 + 900.00;
}
}
else {
if(studentRes == "y" && studentRes == "Y") {
studentTuition = studentCredit * 432.00;
studentFees = (studentCredit * 28.37) + 900.00;
}
else {
studentTuition = studentCredit * 841.00;
studentFees = (studentCredit * 28.37) + 900.00;
}
}
}
else {
if(studentLife == "f" || studentLife == "F") {
if(studentRes == "y" && studentRes == "Y") {
studentTuition = 5185.00;
studentFees = 342.14;
}
else {
studentTuition = 10095.00;
studentFees = 342.14;
}
}
else {
if(studentRes == "y" && studentRes == "Y") {
studentTuition = studentCredit * 432.00;
studentFees = studentCredit * 28.37;
}
else {
studentTuition = studentCredit * 841.00;
studentFees = studentCredit * 28.37;
}
}
}
}

我知道这个问题很多...感谢任何可以帮助我了解如何更好地完成此操作的人!

最佳答案

问题是在 C++ 中你有函数重载,这意味着你可以有两个具有相同名称但不同签名(基本上是参数)的函数。

例如,如果你有一个函数

void undergradBill(double finalBill);

与函数不同

void undergradBill();

您已经声明(我可能会非法添加)带参数的函数声明。然后,您定义了采用 参数的函数。由于重载,这在 C++ 中有效。

所以你的问题有很多原因:首先你有非法的函数声明,这会给你带来错误。因此,编译器没有您要调用的函数的声明。由于声明和定义不匹配的问题,您将无法调用这些函数,因为您调用的函数实际上并不存在。

当然,由于您定义的函数没有参数,使用“参数”会给您带来错误,因为函数定义中不存在这些变量。

关于c++ - 不理解通过函数传递的 C++ 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835107/

25 4 0
文章推荐: javascript - AngularJS ui-grid editableCellTemplate : 'ui-grid/dropdownEditor'
文章推荐: html - 在 Reaction 创建的网格中对齐文本
文章推荐: c++ - 使用父类中的变量
文章推荐: html - 如何仅使用 CSS 设置