tyty5949
tyty5949

Reputation: 1530

C++ / 'string' was not declared in this scope/ error

I am making a program that will track golf stats so i need to make players. I made a function called

getPlayer(int playerNum);

and inside of it i have all of this code

switch(playerNum)
case 1: return(player1);break;
case 2: return(player2);break;
case 3: return(player3);break;
case 4: return(player4);break;
case 5: return(player5);break;
case 6: return(player6);break;
case 7: return(player7);break;
case 8: return(player8);break;
case 9: return(player9);break;
case 10: return(player10);break;

and in my playermanager.h

    #ifndef PLAYERMANAGER_H
    #define PLAYERMANAGER_H
    #include <string>

    class playerManager
    {
    public:
        playerManager();
        std::string getPlayer(int playerNum);
    private:
        std::string player1;
        std::string player2;
        std::string player3;
        std::string player4;
        std::string player5;
        std::string player6;
        std::string player7;
        std::string player8;
        std::string player9;
        std::string player10;
    };

    #endif // PLAYERMANAGER_H

Now when i run this i get the error

    'player1' was not declared in this scope

And i also get the error

    break statement not within loop or switch 

and it goes on and on from player 1 to player 10. I have intelized the the string by calling a file reader function. I am really new to c++ and i am probably doing something just stupidly wrong so if anyone could help please. Thanks in advance!

Upvotes: 0

Views: 9885

Answers (4)

Miguel P
Miguel P

Reputation: 1292

Instead of using a switch for different players, try making a dynamic allocated array for the players, so you can always add, or make a pointer, and then keeping an int in the background to tell how many players are present, or whatever.

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83577

You have shown the following code in your getPlayer() function:

switch(playerNum) 
case 1: return(player1);break; 
case 2: return(player2);break; 
case 3: return(player3);break; 
case 4: return(player4);break; 
case 5: return(player5);break; 
case 6: return(player6);break; 
case 7: return(player7);break; 
case 8: return(player8);break; 
case 9: return(player9);break; 
case 10: return(player10);break; 

This code is not inside a function. I assume you simply have not posted few lines that show which function it is in. Please do this so that we can help you further.

Also, since you are using a list of players, you should consider learning how to use an array or a container from the STL (such as std::vector).

Upvotes: 0

crashmstr
crashmstr

Reputation: 28583

Could be a few different things, but it might be that you want this:

std::string playerManager::getPlayer(int playerNum) //<- here you need the class name
{
    switch(playerNum)
    { 
    //^- and here you don't show  the { and } in your example, and this could be the
    // reason for the "break statement not within loop or switch" error

        case 1: return(player1);break;
        case 2: return(player2);break;
        case 3: return(player3);break;
        case 4: return(player4);break;
        case 5: return(player5);break;
        case 6: return(player6);break;
        case 7: return(player7);break;
        case 8: return(player8);break;
        case 9: return(player9);break;
        case 10: return(player10);break;
    }
}

That said, using an array (or vector) might be a better solution for this.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258648

Leaving out the codereview stuff -

did you by any chance implement

std::string getPlayer(int playerNum);

instead of

std::string playerManager::getPlayer(int playerNum);

?

Also - the switch:

switch(playerNum)
{
case 1: return(player1);break;
case 2: return(player2);break;
case 3: return(player3);break;
case 4: return(player4);break;
case 5: return(player5);break;
case 6: return(player6);break;
case 7: return(player7);break;
case 8: return(player8);break;
case 9: return(player9);break;
case 10: return(player10);break;
}

That aside - awful code. Imagine I'm your customer and tell you I want 11 players instead of 10. What then?

Upvotes: 4

Related Questions