0

I am new to C++ and I am trying to understand how to pass variables through multiple functions. I understand you can use global variables or argument passing.

In my code, I have defined two functions, after the main function. I have set it up with prototypes. The error I am receiving in CodeBlocks is .error: 'studentFees' was not declared in this scope. This makes sense, as I have not implemented anything in lines 123 and 124. But I am not quite sure how. I am also not feeling to great on implementing the prototypes properly either.

Can someone help me out on the proper way of doing this?

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

I know this is a lot to ask... Thanks you to anyone who can help me understand how to do this a little better!

Gavin Youker
  • 330
  • 2
  • 6
  • 21
  • add the parameters from the prototypes to the implementations of gradBill() and undergradBill(). – dgsomerton Apr 25 '16 at 08:13
  • You need to [start with a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) because those function declarations are not valid in C++ and should give you errors. – Some programmer dude Apr 25 '16 at 08:13
  • Parameters with default arguments have to form a contiguous group at the end of the parameter list. So if you want to give a default param to finalBill this should be passed as last parameter or you should provide a default value for the following parameters too. – Stefano Apr 25 '16 at 08:27

1 Answers1

1

The problem is that in C++ you have function overloading, which means you can have two function with the same name but different signature (basically arguments).

For example if you have a function

void undergradBill(double finalBill);

that is different from a function

void undergradBill();

You have declared (illegally I might add) function declarations taking arguments. Then you have defined functions taking no arguments. This is valid in C++ due to the overloading.

So your problem is because of many reasons: First you have illegal function declarations which will give you errors. Because of that the compiler doesn't have the declarations of the functions you want to call. And due to your problem with declaration and definition not matching you will not be able to call the functions anyway as the functions you call doesn't actually exist.

And of course, since you define the functions without arguments, using the "arguments" will give you errors as those variables don't exist inside the function definitions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621