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

时间:2016-04-25 08:09:56

标签: c++ function arguments global-variables parameter-passing

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

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

有人可以帮我解决这个问题的正确方法吗?

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

我知道这要问很多...... 感谢任何能帮助我了解如何做得更好的人!

1 个答案:

答案 0 :(得分:1)

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

例如,如果你有一个功能

void undergradBill(double finalBill);

与功能不同

void undergradBill();

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

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

当然,既然您定义了没有参数的函数,那么使用“arguments”会给出错误,因为函数定义中不存在这些变量。