Question:
c++ beginner error- char array?
Nick
2015-04-19 13:59:50 UTC
here is code

#include

using namespace std;

int main()
{

char selectPlayer[6] {'Zach Parise', 'Jason Pomminville', 'Thomas Vanek', 'Mikko Koivu', 'Nino Niederreiter', '\0'};
void intro();
intro ();
void who();
who ();
cin >> selectPlayer;
void what(char);
what (selectPlayer);

}
void intro ()
{
cout << "Hello, welcome to the Minnesota Wild Stat Generator!" << endl;
cout << "You can learn stats about 5 top players during the " << endl;
cout << "2014-2015 regular season!" << endl;
}
void who ()
{

cout << "Who would you like to learn about?" << endl;
cout << "Press 0 for Zach Parise" << endl;
cout << "Press 1 for Jason Pomminville" << endl;
cout << "Press 2 for Thomas Vanek" << endl;
cout << "Press 3 for Mikko Koivu" << endl;
cout << "Press 4 for Nino Niederreiter" << endl;
}
void what (char selectPlayer)
{
cout << "What statistic would you like to learn about " + selectPlayer;
}



and here are the errors:
http://gyazo.com/6a6b36488a257bb55dfa72f81eb18ccd
please help! thanks :D

oh and line 14 is:
void what(char);

line 15:
what (selectPlayer);

thanks
Three answers:
husoski
2015-04-19 14:57:45 UTC
You can't put whole strings into a char variable, constant or element of a char[] array. It's like trying to put a gallon into a half-pint bottle. You can't fit several chars into the space that was reserved for just one.



There is a non-portable use of a very small number of characters is a char constant, but that's not really of type char anyway. 'a' is actually an int constant, and 'ab' represents an int that has an 'a' and a 'b' in it somewhere. But, the compiler is not required to allow that, the limit of how many characters can be used and what order they are packed in is not specified, the result can't be stored in a char variable, and there's not portable way to print out the int value as characters.



To get an array of constant variable-length strings, use an array of pointers and initialize with a list of C-string constants:



const char *playerNames[] = { "Zach Parise", "Jason Pomminville", "Thomas Vanek",

"Mikko Koivu". "Nino Niederreiter" };



There's no reason for a '\0' at the end, since this isn't a string. If you want a null pointer at the end of that array, you can add a 0 (not '0' or '\0' or "0") to the end, or used the NULL constant defined in .



const char *playerNames[] = { "Zach Parise", "Jason Pomminville", "Thomas Vanek",

"Mikko Koivu". "Nino Niederreiter", NULL };



-------

Also note that the name is NOT selectPlayer. You can't use the same variable name for both the array of names and for the input selection value from the user. They are not the same thing.



The selectPlayer input should be an int, by the way, since you want to use it as an array index. There's not problem with using values 1,2,3,... for the player numbers, but you need to either subtract 1 from that value when you index the array, since array indices are zero-based in C++. Another option is to simply add a dummy entry to the front of the array for the non-existent "player zero", so you can use your 1-based numbers without subtracting.



Finally, you might want to put cin>> call for that player number in the who() function and return the result as an int. The call in main would then look like:



int who();

int selectPlayer = who();
?
2015-04-19 14:08:40 UTC
attempted declaration of function within function

passing a pointer to a char array when the function declaration has char as argument
?
2015-04-19 14:39:09 UTC
i fixed some things



//char is for a single character you were using strings

//you should not declare functions in another function..

//Use globalspace

#include

#include



using namespace std;

void intro(void);

void who( string*);

void what(string *,int);

int main()

{

string selectPlayer[6]= {"Zach Parise", "Jason Pomminville", "Thomas Vanek" , "Mikko Koivu", "Nino Niederreiter" };

int p;;

who (selectPlayer);

//cin >> p;

p=1;

what (selectPlayer,p);

return 0;



}

void intro () {

cout << "Hello, welcome to the Minnesota Wild Stat Generator!" << endl;

cout << "You can learn stats about 5 top players during the " << endl;

cout << "2014-2015 regular season!" << endl;

}

void who (string * selectplyer){

//no need to type in the names again just use them from the array...

cout << "Who would you like to learn about?" << endl;

cout << "Press 0 for "<< selectplyer[0]<< endl;

cout << "Press 1 for "<< selectplyer[1] << endl;

cout << "Press 2 for "<< selectplyer[2] << endl;

cout << "Press 3 for "<< selectplyer[3]<< endl;

cout << "Press 4 for "<< selectplyer[4] << endl;

}

void what (string * selectPlayer,int playerChoice)

{

cout << "What statistic would you like to learn about " << selectPlayer[playerChoice];

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...