私はC++が初めてで、複数の関数を使って変数を渡す方法を理解しようとしています。グローバル変数や引数の受け渡しを使用できることを理解しています。関数を介して渡されるC++引数を理解していません
私のコードでは、main関数の後に2つの関数を定義しました。私はプロトタイプでそれを設定しました。 CodeBlocksで受け取ったエラーは.error: 'studentFees' was not declared in this scope
です。これは、私が行123
と124
に何も実装していないので意味があります。しかし、私はどのようにはっきりしていません。プロトタイプを適切に実装することについても、私は素晴らしい気分ではありません。
誰かがこれを行う正しい方法で私を助けることができますか?
#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;
}
}
}
}
私はこれが...私は少し良く、これを行う方法を理解するのに役立つことができます誰にも おかげであなたを求めることがたくさんある知っています!
prototypeからgradBill()およびundergradBill()の実装にパラメータを追加します。 – dgsomerton
これらの関数の宣言はC++では有効ではないので、[初心者の本から始める](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)が必要です。あなたにエラーを与える。 –
デフォルトの引数を持つパラメータは、パラメータリストの末尾に連続したグループを形成する必要があります。したがって、finalBillにデフォルトのパラメータを渡したい場合は、これを最後のパラメータとして渡すか、次のパラメータのデフォルト値を指定する必要があります。 – Stefano