|
-
March 28th, 2011, 08:26 PM
#1
Having trouble passing array to function.
I am getting all kinds of errors when passing my array to this function. The function is suppose to have the user enter a name and a score and store them in 2 array seperate arrays, one for the names, one for the scores. I believe I have to use pointers but have no idea on how to use them. Here is the code:
#include <iostream>
int InputData(int, char, int);
using namespace std;
void main()
{
char playerName[100][20];
int score[100];
int numPlayers = 0;
int averageScore = 0;
InputData(numPlayers, playerName, score);
}
int InputData(int numPlayers, char playerName[100][20], int score[100])
{
while (numPlayers <= 100)
{
cout << "Enter Player Name (Q to quit): ";
cin.getline(playerName, 100, ‘\n’);
if ((playerName[numPlayers] = 'Q') || (playerName[numPlayers] = 'q'))
return 0;
cout << "Enter score for " << playerName[numPlayers] <<": ";
cin >> score[numPlayers];
numPlayers++;
}
}
-
March 28th, 2011, 09:01 PM
#2
Re: Having trouble passing array to function.
Well, what errors are you getting? When you check if the user entered 'q', you need to use ==, not =. = is assignment, not equals. == is equals. Also, this wasn't much of an issue since the code was short, but it is best to surround any pasted code with [cod e] and [/cod e] (without spaces), so indentation is kept.
EDIT: Whoa! Why is the max player name size 20, but your getline has a maximum of 100? Shouldn't it be 20?
Last edited by henryswanson; March 28th, 2011 at 09:24 PM.
-
March 28th, 2011, 11:06 PM
#3
Re: Having trouble passing array to function.
Ok I made some changes, here is the code now:
Code:
#include <iostream>
int InputData(int &, char, int);
using namespace std;
int main()
{
char playerName[100];
int score[100];
int numPlayers = 0;
InputData(numPlayers, playerName, score);
return 0;
}
int InputData(int &numPlayers, char playerName[], int score[])
{
while (numPlayers <= 100)
{
cout << "Enter Player Name (Q to quit): ";
cin.getline(playerName, 100, ‘\n’);
if ((playerName[numPlayers] == 'Q') || (playerName[numPlayers] == 'q'))
return 0;
cout << "Enter score for " << playerName[numPlayers] <<": ";
cin.getline(score, 100, ‘\n’);
numPlayers++;
}
}
-
March 29th, 2011, 03:27 AM
#4
Re: Having trouble passing array to function.
I really find it difficult also to pass array into function. I get confused on how to do it.
basic math test
-
March 29th, 2011, 11:10 AM
#5
Re: Having trouble passing array to function.
f ((playerName[numPlayers] = 'Q') || (playerName[numPlayers] = 'q'))
here you put playerName[nuplayers to Q and q its source of error as i think it does not cheak not bt assign it to q
Crash_Override has given you proper answer.
-
March 29th, 2011, 04:35 PM
#6
Re: Having trouble passing array to function.
Are you trying to have multiple player names and multiple scores? One name and many scores? Also, you have ( ` ) around your \n, where you need ( ' ).
Last edited by henryswanson; March 29th, 2011 at 04:42 PM.
-
March 29th, 2011, 05:17 PM
#7
Re: Having trouble passing array to function.
Alright, I've rewritten a few parts of it, and it is working (at least on my computer). I'll put in comments to show why I changed it.
Code:
#include <iostream>
using namespace std;
void InputData(int&, char[][20], int[]); //you are passing arrays of char and int, not just char and int. also, it doesn't return anything useful, so i made it void.
int main()
{
char playerName[100][20]; //each name is a char[20], so 100 names is char[100][20]
int score[100];
int numPlayers = 0;
int averageScore = 0;
InputData(numPlayers, playerName, score);
}
void InputData(int& numPlayers, char playerName[][20], int score[])
{
while (numPlayers <= 100)
{
cout << "Enter Player Name (Q to quit): ";
cin.getline(playerName[numPlayers], 20, '\n'); //the 100 was changed to 20 because the size allocated for each name was 20 chars. also, you use the apostrophe, not back tick, for characters
if ((playerName[numPlayers][0] == 'Q') || (playerName[numPlayers][0] == 'q')) //if the first character is q
{
if(playerName[numPlayers][1] == '\0') return; //if the string ends after one character (otherwise, quentin would make it quit)
}
cout << "Enter score for " << playerName[numPlayers] << ": ";
cin >> score[numPlayers]; //getline works for char arrays, not ints
cin.ignore(1); // >> leaves a newline in the stream, so you have to skip that
numPlayers++;
}
}
However, if you put in more than 20 character names, it starts flipping out. I'm not very familiar with cin, but i think you need to ignore the rest of the name. Not sure how to do that though. :\
EDIT: Figured it out. Include <limits>. Make a function void discardExtraChars(), and call it after cin.getline. The function should be this:
Code:
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //the numeric limits thing is the maximum size of the stream. You could put in any large number, but this is good practice.
}
Last edited by henryswanson; March 29th, 2011 at 05:46 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|