2016-04-25 66 views
0

我是C++的新手,我想了解如何通过多个函数传递变量。我明白你可以使用全局变量或参数传递。不理解通过函数传递的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; 
      } 
     } 
    } 
} 

我知道这是一个很多问... 感谢您对任何人谁可以帮助我了解如何做到这一点更好一点!

+0

将参数从原型添加到gradBill()和undergradBill()的实现中。 – dgsomerton

+0

因为这些函数声明在C++中是无效的,所以你需要[从一个好的初学者书开始](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)给你错误。 –

+0

具有默认参数的参数必须在参数列表的末尾形成一个连续的组。所以如果你想给finalBill一个默认参数,这应该作为最后一个参数传递,或者你也应该为下面的参数提供一个默认值。 – Stefano

回答

1

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

例如,如果你有一个函数

void undergradBill(double finalBill); 

是从功能

void undergradBill(); 

您已经声明(非法我可以补充)函数声明服用参数不同。然后你已经定义了参数为没有的参数。由于超载,这在C++中是有效的。

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

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