Need help with simple string question

Hi, I'm a new programmer trying to write a baseball code for my brother. I'm trying to get it so that when you enter the name of the player, their batting average for the 2014 season shows up. Sounds simple, right? For some reason, the string isn't working. I'm sure it's a simple solution that I just don't understand.


This is my code thus far, and I plan to have all the names of the players for the SF Giants in the code eventually:

#include <string>
using namespace std;

int main ()
{
int avgvalue;
string name;
string "Buster Posey", "Brandon Belt", "Joe Panik", "Brandon Crawford", "Pablo Sandoval", "Mike Morse", "Angel Pagan", "Hunter Pence";


cout << "Please enter the name of an SF ballplayer\n" << "who played during the 2014 season." << endl;
getline (cin, name);

if (name == "Buster Posey")
avgvalue = .311;

else if (name == "Brandon Belt")
avgvalue = .243;

else if (name == "Joe Panik")
avgvalue = .305;

else if (name == "Brandon Crawford")
avgvalue = .246;

else if (name == "Pablo Sandoval")
avgvalue = .279;

else if (name == "Mike Morse")
avgvalue = .279;

else if (name == "Angel Pagan")
avgvalue = .300;

else if (name == "Hunter Pence")
avgvalue = .277;

else
cout << "This name is not recognized. Please rerun the program and try again." << endl;

cout << name <<"'s batting average in the 2014 season\n" << "was " << avgvalue << "." << endl;

return 0;
}

Thank you for the help!
Change the type of avgvalue to double
1
2
// int avgvalue;
double avgvalue ;


throw away this line; we don't need it:
// string "Buster Posey", "Brandon Belt", "Joe Panik", "Brandon Crawford", "Pablo Sandoval", "Mike Morse", "Angel Pagan", "Hunter Pence";

add #include <iostream> at the top

and the program should compile and run.
It works, thanks!!
Now, try 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
#include <iostream>
#include <string>

int main( int argc, char* argv[] )
{
    const int NPLAYERS = 8 ;
    const std::string names[NPLAYERS]  = { "Buster Posey",   "Brandon Belt", "Joe Panik",   "Brandon Crawford",
                                           "Pablo Sandoval", "Mike Morse",   "Angel Pagan", "Hunter Pence" } ;

    const double scores[NPLAYERS] =      {  0.311,            0.243,          0.305,         0.246,
                                            0.279,            0.279,          0.300,         0.277 } ;


    std::cout << "Please enter the name of an SF ballplayer\n who played during the 2014 season.\n" ;
    std::string player_name ;
    std::getline( std::cin, player_name );

    for( int i = 0 ; i < NPLAYERS ; ++i )
    {
        if( names[i] == player_name )
        {
            std::cout << player_name << "'s batting average in the 2014 season was " << scores[i] << ".\n" ;
            return 0 ;
        }
    }

    std::cout << "This name is not recognized. Please rerun the program and try again.\n" ;
}
Oh, yeah, using an array would be much faster. :) I didn't know how to do that. Thanks!
Topic archived. No new replies allowed.