Returning from function to certain vector

closed account (EAp4z8AR)
This is only part of my code but I need help returning the values from the function to the specific vectors. How would I phrase it in my main function so that the value returned from home function gets stored in the home vector and the value returned from the away function gets stored in the away vector?

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
  double homeQuarter1(double min, string max);
double awayQuarter1(double min, string max);


int main()
{
    string maximum;
    double minimum=0;
    vector<double> homeScore;
    vector<double> awayScore;
    
    
    homeQuarter1(minimum, maximum);
}

double homeQuarter1(double min, string max)
{
    double homeScore, minimum=0;
    string maximum;
    cout << "1Q Home team score: \nPlease enter a number (min: 0, max: no limit): ";
    cin >> homeScore;
    
    while (homeScore>=min)
    {
        awayQuarter1(minimum, maximum);
        return homeScore;
    }
    
    while (homeScore<min)
    {
        cout << "Invalid input, try agian!\nPlease enter a number (min: 0, max: no limit): ";
        cin >> homeScore;
        while (homeScore>=min)
        {
            awayQuarter1(minimum, maximum);
            return homeScore;
        }
    }
}    

double awayQuarter1(double min, string max)
{
    string maximum;
    double awayScore, minimum=0;
    cout << "1Q Away team score: \nPlease enter a number (min: 0, max: no limit): ";
    cin >> awayScore;
    
    while (awayScore>=min)
    {
        homeQuarter2(minimum, maximum);
        return awayScore;
    }
    
    while (awayScore<min)
    {
        cout << "Invalid input, try agian!\nPlease enter a number (min: 0, max: no limit): ";
        cin >> awayScore;
        while (awayScore>=min)
        {
            homeQuarter1(minimum, maximum);
            return awayScore;
        }
    }
}
It's hard to understand why minimum is a double while maximum is a string.

Also, I doubt it is a good program structure to have quarter1 call quarter2 which calls quarter3, etc. That's probably not really what you want.

As for the vectors, you need to pass them by reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
using namespace std;

void fill(vector<double>& home) {
    for (int i = 0; i < 10; i++)
        home.push_back(i);
}

int main() {
    vector<double> home;
    fill(home);
    for (const auto& score: home)
        cout << score << '\n';
}

Last edited on
closed account (EAp4z8AR)
Okay I got this now but why does it say this when I try to run the code
"request for member ‘push_back’ in ‘home’, which is of non-class type ‘double’ home.push_back(score);"

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
double homeQuarter(double);
double awayQuarter1(double);
vector<double> home;
vector<double> away;

int main()
{
    for (int i=1; i<8; i++)
    {
    homeQuarter(home[i]);
    }
    
    cout << "" << setw(7) << right;
    for (int i=1; i<8; i++)
    {
        cout << i << "Q" <<  setw(7) << right;
    }
    
    cout << "HOME" << setw(7) << right;
    
    for (int i=1; i<8; i++)
    {
        cout << home[i] << setw(7) << right;
    }
    
}

double homeQuarter(double home)
{
    double score;
    for(int i=1; i<8; i++)
    {
        cout << i << "Q Home team score: \nPlease enter a number (min: 0, max: no limit): ";
        cin >> score;
        
        while (score>=0)
        {
            home.push_back(score);
        }
    
        while (score<0)
        {
            cout << "Invalid input, try agian!\nPlease enter a number (min: 0, max: no limit): ";
            cin >> score;
            while (score>=0)
            {
                home.push_back(score);
            }
        }
    }
}
Last edited on
You can't pass a vector-of-doubles as a double. You need to do it like I did it, passing a reference to a vector of doubles. And homeQuarter doesn't return anything, so it should be void.

 
void homeQuarter(vector<double>& home)

Also note that the indices for the first 8 elements of a vector (or array) go from 0 to 7, not 1 to 7 (like your loop does for some reason).
Topic archived. No new replies allowed.