Need help

Question is : Consider the class declaration and main() function below. There are two errors in
the main() function. Name the errors and explain how to fix them, providing all the
code required to correct the errors.

I edited the code to look like this
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
#include <iostream>

using namespace std;

class Game
{
    public:
        Game();
        string getName();
        int getLevel();
        double getScore();

    private:
        string Name;
        string Champion;
        int Level;
        double Score;
};
int main()
{
    Game sodoku, tetris[12];
    string getChampion();
    {
        return Champion;
    }
    double nScore = sodoku.Champion;
    double nScore = sodoku.getChampion();
    return 0;
    cout << "The first tetris player is "
    << tetris.getName() << endl;
}


I only edited the following : *string getChampion(); {return Champion;}
and *double nScore = sodoku.getChampion();
Last edited on
Champion is private. its the first nScore that needs to be cut out, not the second.
This has the smell of a troll......
its not a troll seeplus , and I know that Champion is Private and u can't access it due to it not being public, do i take out the first nScore?


this is the code they gave us:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Game
{
public:
Game();
string getName();
int getLevel();
double getScore();
private:
string Name;
string Champion;
int Level
double Score;
};
int main()
{
Game sodoku, tetris[12];
.........(additional code)
double nScore = sodoku.Champion;
.........(additional code)
return 0;
cout << "The first tetris player is "
<< tetris.getName() << endl;
}
yes. you have to cut that one, not the getchamp() function one.

- the forum is getting a lot of first time posters that turn out to be spam. Your post does look like one of them, don't take it personally :)

this program is small enough that you can fix it and test your fix in a compiler faster than you can get answers here. But if you do not understand what is illegal, or need a better explain, ask!
Last edited on
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
#include <iostream>

using namespace std;

class Game
{
    public:
                    Game();
            string getName();
            int getLevel();
            double getScore();
            string getChampion();
    private:
            string Name;
            string Champion;
            int Level;
            double Score;
};
int main()
{
        Game sodoku, tetris[12];
        string getChampion();
        {
            return Champion;
        }

        double nScore = sodoku.getChampion();

        return 0;
        cout << "The first tetris player is "
        << tetris.getName() << endl;
}



Im getting 3 errors now

1 : 24 - Champion was not declared in this scope
2 : 27 - cannot covert 'std::__cxxll:string' etc etc "to 'double' in initialisation
3 : 31 - request for member 'getName' in 'tetris' , which is of non-class type 'Game [12]


can anyone help me to fix it?

and ty for the responds so far and sorry for making it look like spam jonnin :(
Hi! :)

This compiles OK:

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
#include <iostream>
#include <string>

using namespace std;

class Game {
public:
	Game() {}
	string getName() const { return Name; }
	int getLevel() const { return Level; }
	double getScore() const { return Score; }
	string getChampion() const { return Champion; }

private:
	string Name;
	string Champion;
	int Level {};
	double Score {};
};

int main()
{
	Game sodoku, tetris[12];
	const auto nScore {sodoku.getChampion()};

	cout << "The first tetris player is " << tetris[0].getName() << '\n';
}


In your code, L22-25 are supposed to be the implementation of the class function getChampion(). Functions cannot be defined within functions (cannot nest).

L31. tetris is an array so to reference the first element, you need tetris[0] as aray index starts at 0.

L29. The retuen is in the wrong place. It should be after L31. Statements after a return statement to the end of the block are not executed.

L27. getChampion() returns a type std::string. So nscore should be of type std::string
Last edited on
There are two errors in the main() function. Name the errors and explain how to fix them

This task is more about understanding syntax than about writing code.

IF the original code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    Game sodoku, tetris[12];
    string getChampion();
    {
        return Champion;
    }
    double nScore = sodoku.Champion;
    double nScore = sodoku.getChampion();
    return 0;
    cout << "The first tetris player is "
    << tetris.getName() << endl;
}

AND there are two errors
THEN ... I see only one way to count to two:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    Game sodoku, tetris[12];

    // #1 block of nonsense
    string getChampion();
    {
        return Champion;
    }
    double nScore = sodoku.Champion;
    double nScore = sodoku.getChampion();
    return 0;

    cout << "The first tetris player is "
      << tetris[0].getName() << endl; // #2 array element access
}

However,
- The logic of the remaining code is still a mystery
- "nonsense" is not a proper description for the multiple syntax errors in that block
Topic archived. No new replies allowed.