Function calling functions

How do I return true into the other functionside,and how can I call functions within functions.

These are the instructions that I'm working with:
Write a function classify() which will receive three parameters (at this point, they must be valid values). This function will call the two functions described below, as follows.

Using the function ratesonescore(), classify() will rate each of the three scores, one at a time. There will be three calls to this function, one for each score.

Then classify() will use the function findtotalscore() to find the total of the three scores.

Then classify() will use the function ratethegroup() to rate the three scores and the total together. Finally, classify() will return to the main program.

3. Write a function rateonescore() which will receive one parameter representing a valid SAT score. The function will determine which of the following three categories the score is in: less than 500, 500 or above, or 800 (perfect score). Note this function gets one parameter.

The function will print a message indicating which of these three categories occurred.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include <iostream>
using namespace std;

bool isitavalidgroup(int []);

int main()
{


int array[3];

cout <<"Please enter 3 SAT scores"<<endl;

for(int i=0; i<3; i++)
    {

    cin>>array[i];
    }

if(isitavalidgroup(array))
    cout<<"Group is valid /n";
else
        cout<<"Group is not valid /n";



return 0;

}

bool isitavalidgroup(int array[])
    {
    for(int i=0;i<3;i++)
       {
           if(array[i]<200 || array[i]>800 || array[i]%10!=0)
           {
               return false;
           }
       }

       return true;



        }
you can call a function just as you would in main:

bool foo(int I)
{
return true; //returns the value to the caller, which isn't always main.
}

void bar(double d)
{
if(foo(13))
d = 10;
//or
bool b = foo(2);

}

int main()
{
bar(3.14);
}


This should get you going. I ignored your problem, which needs the techniques but has additional complications that are not relevant to the specific question. See if you can do it now...
Last edited on
Don't forget to declare the last function used first or else the function that calls the final function wont recognize it and will give you an error.
I Don't fully understand. Especially the part were if (foo (13)) or bool b=foo (2)
Last edited on
if (foo(13))

This calls the "foo" function with the parameter 13. In this stupid example, the parameter is not even used, so the value of it does not matter, 13 was a random choice. The result of this function is a Boolean, which evaluates to true in this simple function. The true is returned...
so you get
if( true)

which evaluates to "true, do the statements owned by the if condition.

-----------

bool b = foo(2);

this is similar.
foo(2) evaluates to true, this becomes

bool b = true;

creating a new Boolean variable and storing the function's results into it.

and you can avoid the declare order issue with prototypes, but its outside this topic a bit. Generally, for anything that isn't quick hack throwaway code, you want headers for your functions:

bool foo(int); //prototype, goes in .h file or at top of a one-file program.

closed account (48T7M4Gy)
How do I return true into the other functionside,and how can I call functions within functions.


@OP

I think it is about time you started doing some reading and study instead of just surfing around trying to get people to do your homework.

Most people here are familiar with the continual weasel questions such as yours which are merely incremental questions to build up the answer you are supposed to provide yourself.

If you aren't clear then go and see your teacher.
Thank you so much everyone!, especially kemort! If you don't want to help thats cool. However I don't get a chance to run to school when ever I like, I'm trying to juggle a full-time job while attending school. So if I end up learning how to do something through the web by watching peoples examples, I don't see what the problem is with struggling to make it happen. No one is being forced to help me. However thanks for volunteering to shit on people, that must make you feel really powerful. I appreciate your productive post, I hope it boosted your self-esteem!

Everyone else I truly appreciate you help, and I've learned so much!
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/212354/

http://www.cplusplus.com/forum/beginner/212281/

http://www.cplusplus.com/forum/beginner/212371/
Last edited on
Hi, esokoletsky.
Are you sure you have reported your requirements the right way? The one about “ratethegroup()” doesn’t make much sense, I think, but also the others are not totally clear.

Please, remember in this forum is explicitly forbidden to ask for help about homework (don't ask me why!), apart for hints:
http://www.cplusplus.com/forum/beginner/1/
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.


So, in the future I think you could to start your posts by saying you are not a student, but a self-learner, and there’s nobody to guide you into C++, which is known not to be an entry level programming language.

This are my two pennies about what I was able to understand from your requirements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// How do I return true into the other functionside, and how can I call
// functions within functions.
// These are the instructions that I'm working with:
// Write a function classify() which will receive three parameters
// (at this point, they must be valid values).
// This function will call the two functions described below, as follows.
// Using the function ratesonescore(), classify() will rate each of the
// three scores, one at a time.
// There will be three calls to this function, one for each score.
// Then classify() will use the function findtotalscore() to find the total
// of the three scores.
// Then classify() will use the function ratethegroup() to rate the three
// scores and the total together. Finally, classify() will return
// to the main program.
// 3. Write a function rateonescore() which will receive one parameter
// representing a valid SAT score. The function will determine which
// of the following three categories the score is in: less than 500,
// 500 or above, or 800 (perfect score). Note this function gets one parameter.
// The function will print a message indicating which of these
// three categories occurred.
#include <iostream>

// "...Write a function classify() which will receive three parameters..."
//    --> classify(int one, int two, int three)
// "...classify() will return to the main program..."
void classify(int one, int two, int three);

// "...will use the function ratethegroup() to rate the three
// scores and the total together..."
void ratethegroup(int one, int two, int three, int total);

// "...Write a function rateonescore() which will receive one parameter
// representing a valid SAT score..."
void rateonescore(int satvalue);

// "...will use the function findtotalscore() to find the total
// of the three scores..."
int findtotalscore(int one, int two, int three);
bool isitavalidgroup(int group[]);

int main()
{
    int sat_scores[3] {};
    for(int i{0}; i<3; i++) {
        std::cout << "Please, insert a SAT score in the range 200-800: ";
        std::cin >> sat_scores[i];
    }
    if(isitavalidgroup(sat_scores)) {
        classify(sat_scores[0], sat_scores[1], sat_scores[2]);
    } else {
        std::cout << "Sorry, not a valid group...";
    }

    return 0;
}

bool isitavalidgroup(int group[])
{
    for(int i{0}; i<3; i++) {
        if(group[i]<200 || 800<group[i] || group[i]%10) {
            return false;
        }
    }

    return true;
}

void classify(int one, int two, int three)
{
    // "...Using the function ratesonescore(), classify() will rate each of the
    // three scores, one at a time..."
    std::cout << "The first score belongs to the category ";
    rateonescore(one);
    std::cout << "The second score belongs to the category ";
    rateonescore(two);
    std::cout << "The third score belongs to the category ";
    rateonescore(three);

    int total = findtotalscore(one, two, three);

    // Sorry, can't help from this point on
    ratethegroup(one, two, three, total);
}

// "...The function will determine which of the following three categories
// the score is in: less than 500, 500 or above, or 800 (perfect score)...
// The function will print a message indicating which of these
// three categories occurred..."
void rateonescore(int satvalue)
{
    if(satvalue < 500) {
        std::cout << "less than five hundreds - not so good.\n";
    } else if (satvalue >= 500 && satvalue < 800) {
        std::cout << "more than four hundreds ninety-nine - good.\n";
    } else if (satvalue == 800) {
        std::cout << "eight hundreds - perfect match!\n";
    }
}

int findtotalscore(int one, int two, int three)
{
    return one + two + three;
}

// "...will use the function ratethegroup() to rate the three
// scores and the total together..."
void ratethegroup(int one, int two, int three, int total)
{
    // Can't understand what the requirement asks to do!
}

Thank you so much everyone! I was able to finish the entire assignment because of all the help. I needed some examples, and afterwards I understood the rules and structure.

P.S. kemort, get a life. You present yourself as a sad & petty human being.

Any how, this is my final result :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <fstream>
using namespace std;

bool isitavalidgroup(int []);

void clasify(ofstream &, int, int, int );

void rateonescore(ofstream &, int);

int findtotalscore(int, int, int);

void ratethegroup(ofstream &, int, int, int, int);
ofstream fout("assignment4.txt");
int main()
{


    int array[3];
    int valid = 0;
    int invalid = 0;




    while(1){


        cout <<"Please enter 3 SAT scores or enter -1 to quit."<<endl;
        cin >> array[0];
        if(array[0] == -1){
            break;
        }
        for(int i=1; i<3; i++)
        {
            cin>>array[i];
        }
        for(int i = 0; i < 3; i++){
            fout << array[i] << " " ;
        }
        fout << endl;

        if(isitavalidgroup(array)){
            fout<<"Group is valid " << endl;
            valid++;
            clasify(fout, array[0], array[1], array[2]);
        }
        else{
            invalid++;
            fout<<"Group is not valid " << endl;
        }
        fout << endl;
    }
    fout<<"There are "<<invalid<<" invalid groups" <<endl;

    fout<<"There are "<<valid<<" valid groups" <<endl;
    fout.close();
    return 0;

}

bool isitavalidgroup(int array[]){
    for(int i=0;i<3;i++)
       {
           if(array[i]<200 && array[i]>800 || array[i]%10!=0)
           {
               return false;
           }



       }

       return true;

}

void clasify(ofstream& fout, int score1, int score2, int score3){
    rateonescore(fout, score1);
    rateonescore(fout, score2);
    rateonescore(fout, score3);
    int total = findtotalscore(score1, score2, score3);
    ratethegroup(fout, total, score1, score2, score3);
}

void rateonescore(ofstream& fout, int n) {

    if(n < 500){
        fout<<"less than 500" << endl;
    }else if(n >= 500 && n != 800){
        fout<<"500 and above"<<endl;
    }
    else{
        fout<<"Perfect score"<<endl;
    }

}

int findtotalscore(int score1, int score2, int score3){
    return (score1 + score2 + score3);
}

void ratethegroup(ofstream& fout, int totalscore, int score1, int score2, int score3){
    int count = 0;
    if(score1 >= 700){
        count++;
    }
    if(score2 >= 700){
        count++;
    }
    if(score3 >= 700){
        count++;
    }

    if(totalscore>=2100){
        if(count == 3){
            fout << "Out-standing!" << endl;
        }else if(count == 2){
            fout << "Very good! " << endl;
        }
        else if(count == 1){
            fout << "lop - sided" << endl;
        }
    }else {
        if(score1 < 500 && score2 < 500 && score3 < 500){
            fout << "weak" << endl;
        }else{
            fout << "Erratic" << endl;
        }
    }
}
Topic archived. No new replies allowed.