0

私はC++が初めてで、複数の関数を使って変数を渡す方法を理解しようとしています。グローバル変数や引数の受け渡しを使用できることを理解しています。関数を介して渡されるC++引数を理解していません

私のコードでは、main関数の後に2つの関数を定義しました。私はプロトタイプでそれを設定しました。 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

prototypeからgradBill()およびundergradBill()の実装にパラメータを追加します。 – dgsomerton

+0

これらの関数の宣言はC++では有効ではないので、[初心者の本から始める](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)が必要です。あなたにエラーを与える。 –

+0

デフォルトの引数を持つパラメータは、パラメータリストの末尾に連続したグループを形成する必要があります。したがって、finalBillにデフォルトのパラメータを渡したい場合は、これを最後のパラメータとして渡すか、次のパラメータのデフォルト値を指定する必要があります。 – Stefano

答えて

1

問題は、C++で、あなたが同じ名前で異なる署名(基本的には、引数)を持つ2つの機能を持つことができることを意味の過負荷を機能を持っているということです。例えば

あなたは機能が異なる機能

void undergradBill(double finalBill); 

void undergradBill(); 

を持っている場合は、関数の宣言は、引数を取る(不法Iが追加される場合があります)と宣言しています。それでは、には、引数を取る関数を定義しています。これは、オーバーロードのためにC++で有効です。

あなたの問題は多くの理由があります:まず、エラーをもたらす不正な関数宣言があります。そのため、コンパイラには呼び出す関数の宣言がありません。また、宣言と定義が一致しないため、呼び出す関数が実際には存在しないため、関数を呼び出すことはできません。

もちろん、引数なしで関数を定義するので、 "arguments"を使用すると、それらの変数が関数定義内に存在しないためエラーが発生します。

関連する問題