-4

How do I access another functions specific variables in another function?

void gradesInput() {
    int s1, s2, s3;
    cout << "Please enter your test scores, in order from the first to last.\n";
    cin >> s1 >> s2 >> s3;
    if (s1 < 0 || s1 > 100) {
        cout << "Your first test score is not a valid number, please re-enter: ";
        cin >> s1;
    }
    else if (s2 < 0 || s2 > 100) {
        cout << "Your second test score is not a valid number, please re-enter: ";
        cin >> s2;
    }
    else if (s3 < 0 || s3 > 100) {
        cout << "Your third test score is not a valid number, please re-enter: ";
        cin >> s3;
    }
}

double average3Scores(int s1, int s2, int s3) {
    gradesInput();
    double avg = (int)(((s1 + s2 + s3) / 3.0) + 0.5); //rounded average
    return avg;
}

In Average3Scores, I dont want to have to manually input s1, s2 ,s3 in the parameters, if you know what I mean? I know this has something to do with pointers, is that correct?

I've edited my program to use reference variables, can you tell me if I am using it correctly?

#include <iostream>
using namespace std;

void gradesInput(int &test1, int &test2, int &test3) {
    cout << "You will be prompted to enter your three test scores" << endl;
    cout << "Please make sure your number is between 0 and 100." << endl;
    cout << "Please enter your first test score: ";
    cin >> test1;
    cout << "\nPlease enter your second test score: ";
    cin >> test2;
    cout << "\nPlease enter your third test score: ";
    cin >> test3;
}

double average3Scores(int test1, int test2, int test3) {
    double avg1 = ((test1 + test2 + test3) / 3.0); // calculating the average without rounding
    avg1 = (int)(avg1 + 0.5); //this rounds the test average
    return avg1;
}

double average2Scores(int test2, int test3) {
    double avg2 = ((test2 + test3) / 2.0);
    avg2 = (int)(avg2 + 0.5);
    return avg2;
}

char toLetterGrade(double avg1, double avg2, int test3) {
    if (avg1 >= 90)
        return  'A';
    else if (avg1 >= 70)
        if (test3 >= 90)
            return 'A';
        else
            return 'B';
    else if (avg1 >= 50)
        if (avg2 >= 70)
            return 'C';
        else
            return 'D';
    else
        return 'F';
}

void displayGrade(double avg, char lGrade) {
    cout << "Your average is: " << avg << endl;
    cout << "Your grade is: " << lGrade << endl;
}

int main() {
    int test1, test2, test3;
    double avg1, avg2;
    char lGrade;
    gradesInput(test1, test2, test3);
    average3Scores(test1, test2, test3);
    average2Scores(test2, test3);
    toLetterGrade(avg1, avg2, test3);
    displayGrade(avg1, lGrade);
    system("pause");
    return 0;
}
user703016
  • 37,307
  • 8
  • 87
  • 112
Panthy
  • 1,605
  • 4
  • 19
  • 27
  • You don't. You pass the parameters by address (or reference), declaring them as formal pointers (or references) in the parameter list of the called function, and dereferencing them within to write to the caller's data variables. – WhozCraig Apr 05 '16 at 05:43
  • The variables are in different scope, therefore you must pass them in the method. **BUT** you can put them in class (and than the variables will be share with all class members) – AsfK Apr 05 '16 at 05:44
  • @WhozCraig I haven't learned formal pointers and stuff yet, could I have a simple example so that I may implement it to my code? – Panthy Apr 05 '16 at 05:45
  • 3
    @Panthy the question [shown **here**](https://stackoverflow.com/questions/114180/pointer-vs-reference?rq=1) shows both methods. The subject of the question covers when it is appropriate to use which, and reasons for why. It is probably worth a read. – WhozCraig Apr 05 '16 at 05:49
  • In your edited code you never set `lGrade` in `main` to any value. Your call to `toLetterGrade` should look like this: `lGrade = toLetterGrade(avg1, avg2, test3);`. This is all I see at the moment. – muXXmit2X Apr 05 '16 at 06:16
  • 1
    Well people are downvoting this so just close this damn thing, i guess i'm not even allowed to ask a genuine question on this forum anymore. I'll figure it out with my professor tomorrow instead of trying to get help from the useful people here. Thanks to the people who tried to help me out. To the people who downvoted, go **** yourself for discouraging someone who just wants to learn. bye. – Panthy Apr 05 '16 at 06:20
  • @Panthy Don't let the bullies bother you. This site is full of people with Dunning Kruger and narcissism, that want revenge for their bad childhood. – doug65536 Apr 05 '16 at 06:51

3 Answers3

1

It is advisable to use references, as per the link mentioned by WhozCraig in the comments - References vs pointers. It is usually considered a bad practise to use pointers if you don't intend to play with pointer arithmetic. Here is your modified code:

#include <iostream>
using namespace std;

void gradesInput(int &s1, int &s2, int &s3) {
    cout << "Please enter your test scores, in order from the first to last.\n";
    cin >> s1 >> s2 >> s3;
    if (s1 < 0 || s1 > 100) {
        cout << "Your first test score is not a valid number, please re-enter: ";
        cin >> s1;
    }
    else if (s2 < 0 || s2 > 100) {
        cout << "Your second test score is not a valid number, please re-enter: ";
        cin >> s2;
    }
    else if (s3 < 0 || s3 > 100) {
        cout << "Your third test score is not a valid number, please re-enter: ";
        cin >> s3;
    }
    else cout<<s1<<" "<<s2<<" "<<s3;
}

double average3Scores() {
    int s1, s2, s3;
    gradesInput(s1,s2,s3);
    double avg = (int)(((s1 + s2 + s3) / 3.0) + 0.5); //rounded average

    return avg;
}

int main() {
    average3Scores();
    return 0;
}

Edit: I have added the main() method and one cout statement in gradesInput() just to enable you to directly run the program. Please let me know if you want me to remove those.

1

You can't access variables which are in another scope. However you can pass variables by reference either by using their addresses or directly by using C++ references.

Using Pointers

This type of call is called "call-by-reference" wich allows you to pass variables from one scope to another but making the changes of a variable visible to the first scope:

// declare gradesInput to take pointers as parameters
void gradesInput(int* s1, int* s2, int* s3) 
{
   // access the chunk of memory the pointer points to by dereferencing it
   *s1 = 1; // "set the variable s1 pointes to to 1"
   *s2 = 2;
   *s3 = 3;
}

average3Scores should then look similar to this:

double average3Scores() 
{
    int s1, s2, s3;
    gradesInput(&s1, &s2, &s3); // turn a simple interger variable into a pointer to an int by using the address-off-operator &
    double avg = (int)(((s1 + s2 + s3) / 3.0) + 0.5); //rounded average
    return avg;
}

Using References

Since this whole pointer thing can be a little bit overwhelming for beginners there is a much more easier solution: The C++ Reference. They allow you a real call-by-reference and you don't have to do quite as much as with pointers:

// instead of declaring s1, s2 and s3 as pointers simply declare them as a reference
void gradesInput(int& s1, int& s2, int& s3) 
{
   s1 = 1; // use them like you're used to
   s2 = 2;
   s3 = 3;
}

And average3Scores will then look like this:

double average3Scores() 
{
    int s1, s2, s3;
    gradesInput(s1, s2, s3); // pass them like a "normal" variable, they will then "magically" be changed ;)
    double avg = (int)(((s1 + s2 + s3) / 3.0) + 0.5); //rounded average
    return avg;
}
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
  • can you check out my new code? I tried to do it my self without looking too much, but not able to make it work properly – Panthy Apr 05 '16 at 06:11
0

Sorry for my post last night. I've worked on it and got a little help from my professor so I wanted to share my answer with you guys.

I am asking for a little constructive criticism, especially on things that could have been done in a better/more efficient way.

#include <iostream>
using namespace std;

void gradesInput(int& s1, int& s2, int& s3) {
    cout << "Please enter your test scores, in order from the first to last.\n";
    cin >> s1 >> s2 >> s3;
    if (s1 < 0 || s1 > 100) {
        cout << "Your first test score is not a valid number, please re-enter: ";
        cin >> s1;
    }
    else if (s2 < 0 || s2 > 100) {
        cout << "Your second test score is not a valid number, please re-enter: ";
        cin >> s2;
    }
    else if (s3 < 0 || s3 > 100) {
        cout << "Your third test score is not a valid number, please re-enter: ";
        cin >> s3;
    }
}


double average3Scores(int s1, int s2, int s3) {
    double avg = (int)(((s1 + s2 + s3) / 3.0) + 0.5); //rounded average
    return avg;
}

double average2Scores(int s2, int s3) {
    double avg = (int)(((s2 + s3) / 2.0) + 0.5);
    return avg;
}

char toLetterGrade(double avg, double avg2, int s3){
    if (avg >= 90)
        return 'A';
    else if (avg >= 70) {
        if (s3 >= 90)
            return 'A';
        else
            return 'B';
    }
    else if (avg >= 50) {
        if (avg2 >= 70)
            return 'C';
        else
            return 'D';
    }
    else
        return 'F';
}

void displayGrade(double avg, char lGrade) {
    cout << "Average of 3 test scores is : " << avg;
    cout << endl << "Your grade is : " << lGrade << endl;
}


int main() {
    int s1, s2, s3;

    gradesInput(s1, s2, s3); //works
    double avg1 = average3Scores(s1, s2, s3); //works
    double avg2 = average2Scores(s2, s3); //works
    displayGrade(avg1, toLetterGrade(avg1, avg2, s3));

    system("pause");
    return 0;

}

I think I got the hang of reference parameters, but still not sure If I used them correctly. Feel free to point me in the right direction!

Panthy
  • 1,605
  • 4
  • 19
  • 27