Need Help Converting Strings from an Array into a char array (1-D).


Name     | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |Total
---------------------------------------------------------
Jenny    |   |   |   |   |   |   |   |   |   |    |         
         |   |   |   |   |   |   |   |   |   |    |         
---------------------------------------------------------
John     |   |   |   |   |   |   |   |   |   |    |     
         |   |   |   |   |   |   |   |   |   |    |      
---------------------------------------------------------


So, I have to make a simulated game of bowling that takes the number of players and their names and creates the scoreboard above. The "bowls" are generated in the program using rand() so I am pretty good with that aspect. I created a string array[max of 5 players] that used an int input of how many players will be playing so it only asks for the names of those playing even though it has a size of 5 strings. I have created the top two lines without a problem using two 1-D char arrays:

char line_one[79]= {'N', 'a','m','e',' ',' // ... etc.
char line_dashed[79];
for(int i=0;i<78;i++) {
line_dashed[i]='-';
}

cout << line_one << endl;
cout << line_dashed << endl;

which outputs:


Name | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |Total
--------------------------------------------------------


I need help with converting the names from the string array into a char array so that the scoreboard can print their names since I don't know how many chars their names will contain because the program asks for their names at the beginning of the program and every user will have a different name. I will also need to input the "amount of pins knocked down" into these arrays as the user bowls (by hitting enter or something) if that makes a difference. I don't think 2-D arrays are needed in this assignment since 1-D arrays are the focus of this assignment. Please help me out with this one if you can, thank you in advance!
Last edited on
what exactly do you have.

is it
string sar[5];
or
char car[5][20];

or something else. ?
I have string player_names[] that was created in a separate function that contains the player's names up to the number of players that are playing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 const size_t MAX_PLAYERS = 5 ; // upper limit
    string player_names[MAX_PLAYERS] ;
    int round = 0;
    size_t num_players ; // actual number of players
    cout << "how many will be playing (1-" << MAX_PLAYERS << ")? " ;
    cin >> num_players ;

    if( num_players == 0 ) num_players = 1 ; // at lest one player
    else if( num_players > MAX_PLAYERS ) num_players = MAX_PLAYERS ; // at most MAX_PLAYERS

    cin.ignore( 10000, '\n' ) ; // throw away the new line remaining in the input buffer
    for( size_t i = 0 ; i < num_players ; ++i )
    {
        cout << "name of player #" << i+1 << ": " ;
        getline( std::cin, player_names[i] ) ; // a name may have more than one word
    }

    cout << "list of players:\n" ;
    for( size_t i = 0 ; i < num_players ; ++i )
        cout << i+1 << ". " << player_names[i] << '\n' ;


I need to pass it into a function that prints the score sheet like above but I need to create a 1D array for each of the names so that they have their own line of scoring such as "Jenny" and "John" in the example scoresheet at the very top of my post. I need to extract the name and place it under the name category but the '|' chars that split the frames won't be aligned unless the amount of letters in the name are the same for each one. The score sheet is just a series of arrays that are stacked on top of each other.

Isn't this essentially a duplicate of the question you're asking in another thread?

http://www.cplusplus.com/forum/general/210375/

Please don't spam multiple threads for the same question. It wastes people's time and effort.
Why use char arrays at all?
Instead of
1
2
3
4
5
char line_one[79]= {'N', 'a','m','e',' ',' // ... etc.
char line_dashed[79];
for(int i=0;i<78;i++) {
line_dashed[i]='-';
} 

You could have
1
2
string line_one = "Name "; // ... etc.
string line_dashed(78,'-');

and so on.

When it come to outputting names such as "Jenny" or "John" with a fixed width, you might use something like
cout << left << setw(9) << player_names[i] << right;
Last edited on
closed account (48T7M4Gy)
Exactly as @chervil says. <iomanip> is designed for this job. C-style strings are totally unnecessary too.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>

int main()
{
    std::string name[] = {"Brown", "Greenstone", "Apple", "Arbitrary", "Smith"};
    
    std::cout << std::setw(12) << std::left << "Name"  << '|' << '\n';
    
    for(int i = 0; i < 5; i++)
        std::cout << std::setw(12) << std::left << name[i] << '|' << '\n';
    return 0;
}


Name        |
Brown       |
Greenstone  |
Apple       |
Arbitrary   |
Smith       |
Program ended with exit code: 0


Last edited on
You could use strcpy with name.c_str() as the second input.
http://www.cplusplus.com/reference/cstring/strcpy/

However, I think it's unlikely that your teacher wants you to include the string library at all if the topic is still char arrays. You might want to check with them...
closed account (48T7M4Gy)
You might want to check with them...

Could be a good move as the exercise might be to get experience at cumbersome vs swish (relatively) ways of doing things.
Topic archived. No new replies allowed.