I am making a mock financial aid account for a project and I wanted to know why my program is not working. Can anyone help me out?? After creating my class, I am trying to access my getName(student_name) function in my main(). It says: "error C2065: 'student_name' : undeclared identifier". How can it be undeclared when I did #include "Financial_Aid_.h" in my main_program.cpp file?? Can someone answer this for me please? Thank You guys and gals for looking at my post. Here is my code.

This is my Financial_Aid_.h header file

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <ctime>
#include <conio.h>
#include <string>

using namespace std;

class Financial_Aid_Data
{
private:
    string name;
    float student_id, scholarship_amt; 
    int awards;

public:

    Financial_Aid_Data() /* constructor */
    {
        name = "unknown";
        student_id = 123456;
        scholarship_amt = 0;
        awards = 0;
    }

    string getName(string student_name)
    {
        cout << "What is your name? ";
        getline(cin, student_name);
        cout << endl << endl;
        name = student_name;
        return name;
    }

    float getIdNumber(float student_id_num)
    {
        cout << "What is your student id number? ";
        cin >> student_id_num;
        student_id = student_id_num;
        cout << endl << endl;
        return student_id;
    }

    int getAwardAmount(int amt_of_awards)
    {
        cout << "How many awards do you have? ";
        cin >> amt_of_awards;
        awards = amt_of_awards;
        cout << endl << endl;
        return awards;
    }

    float getScholarship_amount(int awards, float my_scholarship)
    {
        vector<float>num_of_awards;

        for(int i = 0; i < awards; i++)
        {
            cout << "How much is this scholarship worth? ";
            cin >> my_scholarship;
            num_of_awards.push_back(my_scholarship);
            cout << endl << endl;
        }

        for(float x = 0; x <= num_of_awards.size(); x++)
        {
            scholarship_amt += x;
        }

        cout << "Your amount is: " << scholarship_amt << endl;
        return scholarship_amt;
    }

    void Display()
    {
        cout << "Your name is: " << name << endl << endl;
        cout << "Your id is: " << student_id << endl << endl;
        cout << "Your scholarship amount is: $" << scholarship_amt << " from " << awards << " awards." << endl;
    }
};

And this is in my main_program.cpp

#include "Financial_Aid_.h"

void Continue() 
{ 
    char c;
    cout << "Press any key to continue... "; c = _getch();
}

int main()
{
    Financial_Aid_Data Student_Data;

    Student_Data.getName(student_name);
    Continue();
    return 0;
}

Recommended Answers

All 16 Replies

Why are you passing the variable (note: variable) student_name into the .getName method? Shouldn't the parameter list be void?

And, because .get methods generally return the internal value stored in the class, shouldn't your method be called .inputName()? Just a thought.

What waltp said:

string getName()
    {
        cout << "What is your name? ";
        getline(cin, name);
        return name;
    }
commented: Do you ALWAYS have to reinterpret what's said before? Can't the post stand on it's own? -3

I think he got confused between the Get method vs Set method...

also that specific error arises because you are trying to send to the function a variable in the main function but that variable is not declared or created in the main.cpp

Walt your posts can stand on their own but for a complete noob he wont understand a word, I know you are getting frustrated and trying to get the people to brush up on the basics, but I dont believe they will know where to brush up on if you dont give a reference,

that said, giving them code that explains everything also isnt the way to do it

commented: Yuo need to keep your *assumptions* about peoples mental states out of your posts. You on the edge of a Rules Infraction. -3

Well, I am taking Data structures next week and I am brushing up on my C++ programming. I remembered that classes end with a ; and that to call a class you use ClassName whatever; . Then to access the info, i do whatever.method(); inside my main..similar to a structure. With me creating the class in a seperate header and including it in my main_program.cpp by doing #include "Financial_Aid_.h" I should have no problem getting the variables inside of my class. However, I can't access them. I am going to go to TaeKwonDo practice in a little bit so I will try and create a new method of code on here when I get back. Thank You Guys for looking at my code again.

To answer walt's question, I am returning the student's name inside my getName function/module so my student can have a name. You know? Is there a better way of doing this?

So return it, don't pass it in.

You are inputting the value directly into the class variable student_name so you don't need to pass anything into the method. By passing it in, I believe you have created a local variable and bypassed the class variable.

Here is my new uploaded code. The commented out part will not work. Can anyone help me out please?

This is my student_financial_aid.h headerfile

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <conio.h>

using namespace std;

class Student_Financial_Data
{
public:
    Student_Financial_Data();
    string getName();
    float getId_Number();
    int getAwardAmount();
    float getScholarship_Money(int getAwardAmount());
    void Display();

private:
    string name;
    float id, scholarship_amount;
    int award_amount;
};

Student_Financial_Data::Student_Financial_Data()
{
    name = "unknown";
    id = 123456;
    scholarship_amount = 0;
    award_amount = 0;
}

string Student_Financial_Data::getName()
{
    cout << "What is your name? ";
    getline(cin, name); cout << endl;
    return name;
}

float Student_Financial_Data::getId_Number()
{
    cout << "What is your student id number? ";
    cin >> id;
    cout << endl;

    return id;
}

int Student_Financial_Data::getAwardAmount()
{
    cout << "How many awards do you have? ";
    cin >> award_amount;
    cout << endl;

    return award_amount;
}

float Student_Financial_Data::getScholarship_Money(int getAwardAmount())
{
    vector<float> scholarship_holder;
    float scholarship_money;

    for(int i = 0; i < award_amount; i++)
    {
        cout << "How much is this award worth? ";
        cin >> scholarship_money;
        cout << endl;
        scholarship_holder.push_back(scholarship_money);
    }

    for(int i = 0; i < scholarship_holder.size(); i++)
    {
        scholarship_amount += scholarship_holder.at(i);
    }

    cout << "You have: $" << scholarship_amount << " in your account. " << endl << endl;

    return scholarship_amount;
}

void Student_Financial_Data::Display()
{
    cout << "Your Name: " << name << endl << endl;
    cout << "Your ID Number: " << id << endl << endl;
    cout << "Your Award Amount: " << award_amount << " gives you $" << scholarship_amount << " in scholarship grants." << endl << endl;
}

And this is my main_program.cpp

#include "student_financial_aid.h"

void Continue()
{
    char ch; cout << "Press any key to continue... "; ch = _getch();
}

int main()
{
    Student_Financial_Data student_Data;

    Student_Financial_Data(); // initializer for the class

    student_Data.getName();
    student_Data.getId_Number();
    student_Data.getAwardAmount();
    // student_Data.getScholarship_Money(getAwardAmount());  <========== why doesn't this work???
    student_Data.Display();
    Continue();
    return 0;
}

The commented out part in my main_program.cpp will not work. =/ Can anyone please help me out???

Please explain what you think this definition means on line 59?
float Student_Financial_Data::getScholarship_Money(int getAwardAmount())

What is the parameter being passed in?

// student_Data.getScholarship_Money(getAwardAmount()); <========== why doesn't this work???

Define "doesn't work"... Compiler error? Sementation fault? Makes coffee bitter?

Always explain your problem, don't make us guess what is happening...

I believe that my getScholarship_Money function allows me to use the award_amount from my getAwardAmount's results and implements it into my getScholarship_Money function so I can calculate how much money the student recieves from the sum of the x amount of awards he or she recieves. That is what I think my function does.

Doesn't work means that my function is not working the way I want it to. I have a question to ask you WaltP, can I use award_amount in my getScholarship_Money function after I get the result of award_amount? (As long as I call getAwardAmount() before the getScholarship_Money function?

Doesn't work means that my function is not working the way I want it to.

Then, with that detailed information, the answer is "then you did something wrong and have to figure out what it is". If you took your car to get fixed and gave them that explanation, what do you think they'd say?

I have a question to ask you WaltP, can I use award_amount in my getScholarship_Money function after I get the result of award_amount? (As long as I call getAwardAmount() before the getScholarship_Money function?

Now that is a question we can answer. Yes.

You can pass function2() into function1() as a parameter, but in function1() it's just a variable name. Not a function.

So if I delcared the how many awards did you recieve and returned it, on the next line, I can just use award_amount in my function and it should give me my correct result? (in theory of course)

Also, with the whole function2() into function1(), can I do: return award_amount, scholarship_amount; inside one function?

My question at this moment, two+ hours later is: "did you
1) wait two hours for an answer and do nothing?"
2) try your idea to see if it worked so you'd know if it works?"

If you're calling student_Data.getAwardAmount(); you should store the return value in a variable as such and pass the variable to : getScholarship_Money()

int number_of_awards = student_Data.getAwardAmount();
student_Data.getScholarship_Money(number_of_rewards);

But first change this getScholarship_Money(int getAwardAmount()) to getScholarship_Money(int awards)

To your other question, you cannot return two or more values, you are only allowed to return 1. However, returning more than 1 value is achieved through pointers which you should be familiar with if you're already working with classes.

I completely forgot about that dx9_programmer. Wow, I feel stupid. Also, for WaltP's answer: I saw somewhere that you can return 2 things, so I was just confirming. Thank You for having patience with me Walt. I really do appreciate it. And Thank You dx9 programmer for reminding me of this. I really do appreciate it also. Thank You all for your help. =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.