I'm new to c so my current code might be completely wrong. I'm trying to get two inputs into two arrays I've declared two arrays of max size 10 and I want to use the console input and add it to the end of each array.
#include <iostream>
int main() {
char playerName[10];
char playerScore[10];
char name, score;
cout << "Enter the player name:";
cin >> name;
cout << "Enter the player score"
cin >> score;
for (i=0; i<10, i )
{
// add name at the end of playerName
// add score at the end of playerScore
}
return 0;
}
CodePudding user response:
First off, use string
not char
if you want multiple characters. You also need to initialize i
first.
Then you can add inputs at the end of your array like this;
int main() {
string playerName[10];
int playerScore[10];
for (int i=0; i<10; i )
{
cout << "Enter the player name: ";
cin >> playerName[i];
cout << "Enter the player score: ";
cin >> playerScore[i];
}
}
CodePudding user response:
1- you must use using namespace std;
after #include <iostream>
2- you must use ;
at the end of cout << "Enter the player score"
3- you must specify the type of i when you use for loop and use ;
between each part -> for (int i = 0; i < 10; i )
and for fill the character array use -> cin >> playerName;
and cin >> playerScore;
#include <iostream>
using namespace std;
int main() {
char playerName[10];
char playerScore[10];
char name, score;
cout << "Enter the player name: ";
cin >> playerName;
cout << "Enter the player score: ";
cin >> playerScore;
cout << playerName <<'\n';
cout << playerScore << endl;
return 0;
}
But it is better to use a string
CodePudding user response:
If you want to use character arrays, they should be 2d arrays, such as char playername [10][10]
.
Maximum name of 10 characters.
#include <iostream>
using namespace std;
int main() {
char playerName[10][10];
int playerScore[10];
for (int i=0; i<10; i )
{
cout << "Enter the "<<i 1<<"th "<< "player's name:";
cin >> playerName[i];
cout << "Enter the " <<i 1<<"th "<< "player's score:";
cin >>playerScore[i];
cout << "_________________________________________\n";
}
return 0;
}