Function issues

I seem to be having a lot of trouble with functions, as far as I can tell no matter how I rewrite them I get issues saying stuff hasn't been declared in the right scope. the following code isn't finished but as far as I can tell I shouldn't be getting some of the errors I'm getting when I try to compile and run it.

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
 #include <iostream>
#include <string>
using namespace std;

void orderGames(int endvalue);
void optionOne(Game *games);
void optionTwo();
void optionThree();
void optionFour();

enum Genre {FPS, Strategy, Roleplay};
struct Game
{
    string title;
    Genre genre;
    int ranks;
    float timed;

};
const int ASIZE=5;

int main()
{
    Game games[5];
    games[0].title="League of Legends";
    games[1].title="Team Fortress 2";
    games[2].title="Ultima 7: The Black Gate";
    games[3].title="Halo";
    games[4].title="Command and conquer: Red Alert";
    games[0].genre=Strategy;
    games[1].genre=FPS;
    games[2].genre=Roleplay;
    games[3].genre=FPS;
    games[4].genre=Strategy;
    games[0].ranks=3;
    games[1].ranks=5;
    games[2].ranks=1;
    games[3].ranks=2;
    games[4].ranks=4;
    games[0].timed=9.50;
    games[1].timed=16.75;
    games[2].timed=4.25;
    games[3].timed=0.75;
    games[4].timed=6.75;
    int endvalue;

    while (endvalue != -1){
    orderGames(endvalue);
    }

    return 0;
}

void orderGames(int endvalue)
{
    int option;
    cout << "Enter 1 if you'd like to print all games sorted in decreasing order by rank, " << endl;
    cout << "Enter 2 if you'd like to print the information for a specific game, " << endl;
    cout << "Enter 3 if you'd like to print the total time spent for all games, " << endl;
    cout << "Enter 4 if you'd like to print the average rank for all games" << endl;
    cout << "Enter -1 if you'd like to end the program" << endl;
    cin >> option;
    if(option=1)
    {
        optionOne(&games);
    }
        else if(option=2)
        {
            optionTwo();
        }
            else if(option=3)
            {
                optionThree();
            }
                else if(option=4)
                {
                    optionFour();
                }
                    else if(option=-1)
                    {
                        endvalue=-1;
                    }




}

void optionOne(Game* games)
{
    int ranker;
    cout << "You picked option 1, these are the results in the form of Title, Genre, Rank, and time in days " << endl;
    for(ranker=4;ranker>-1;ranker--)
    {
       cout << strcat(games.title[ranker], games.genre[ranker], games.ranks[ranker], games.timed[ranker]);

    }
}

void optionTwo()
{
    string term;
    int countone;
    cout << "Which game would you like information on? " << endl;
    getline(cin, term);
    term find(const )

}


I would appreciate help, if you like I can try to post the assignment specifics if needed.
Hello ArtoriosVII,

To start with yo could use a few more blank lines to break up the code.

Line 20 you define a variable and then never use it. Line 24 should look like Game games[ASIZE];.

Line 45 was hard to find because all the lines run together and it did not stand out. The other problem is that it is uninitialized and just contains garbage. On my computer this value is usually "-858993460". This may make your while condition true, but not a number that you want.

In the function "orderGames" you pass "endvalue" be value. So if you make a change to this copy in the function it is lost when the function ends. You may want void orderGames(int& endvalue). Now if you make a change to this variable it bill be seen in "main"

Also on line 65 you are trying to pass the address of "games" to the function, but "games" was defined in "main" and "main" lost scope when the function was called, so the function can not see this variable. You need to pass the array to the function if you want to use it. So your function definition and prototype should look like this: void orderGames(Game games[], int& endvalue). Unless you have to there is no need to make "games" a pointer even though it will degrade to a pointer. Not that you will see any difference in the function. You can still use games[?].variableName to access the struct.

So on line 65 you can just pass "games" and not the address Then change line 89 to match the function definition of "orderGames".

In the function "optionOne" why are you using a C function on a "std::string"? If you want to add two "std::string"s together just use "+". To use what you have you would have to change the "std::string" to a C string and then back when you are finished. Also "strcat" takes a destination and source as parameters and you are trying to use three sources. Using the comma operator you are likely to only use the last variable.

On line 106 I have no idea what you are trying to do there, but it does not work. What is it you are trying to find and from where?

Hope that helps,

Andy
Line 6: You use Game before it is actually defined (line 12 onward).

Either move the class definition of Game before line 6. Or use forward declaration:

void optionOne(class Game *games); // Note class for forwarding
Topic archived. No new replies allowed.