0

I wrote this code:

shuffledteamnames[8][80]; // global
winningteamnames[8][80]; // global
int main()
{
    if (team1 > team2)
    {
        cout << shuffledteamnames[index1] << " beat the " << shuffledteamnames[index2] << " " << team1 << "-" << team2 << " in a game 1." << endl;
        winningteamnames[WINTEAMcounter] = shuffledteamnames[index1];
    }
    else if (team1 < team2) // index1 = 0, index2 = 1, WINTEAMcounter = 0
    {
        cout << shuffledteamnames[index2] << " beat the " << shuffledteamnames[index1] << " " << team1 << "-" << team2 << " in a game 1." << endl;
        winningteamnames[WINTEAMcounter] = shuffledteamnames[index2];
    }
}

The output of shuffledteamnames is something like this:

Trojans
Bruins
Bears
Trees
Ducks
Beavers
Huskies
Cougars

I am trying to create a competition bracket where I take the winners of each round and place them into char array winningteamnames. I understand that these are 2D char arrays so I need to input data into both parameters, but I'm just not sure how to do that. Please let me know if I was vague at any point and I really appreciate all the help.

4
  • 2
    Are you missing the char type in your declarations of the team name arrays? Why don't you use std:string Commented Feb 15, 2014 at 1:42
  • we arent allowed to use strings or vectors Commented Feb 15, 2014 at 1:43
  • en.cppreference.com/w/cpp/string/byte/strcpy Commented Feb 15, 2014 at 1:45
  • would it matter if it was a 2d array though? Commented Feb 15, 2014 at 1:46

1 Answer 1

1

Use strncpy():

strncpy( winningteamnames[WINTEAMcounter]
       , shuffledteamnames[index1]
       , sizeof winningteamnames[WINTEAMcounter]);
Sign up to request clarification or add additional context in comments.

2 Comments

okay, using strncpy gives me an error footballA2.cpp:290:70: error: too few arguments to function ‘char* strncpy(char*, const char*, size_t)’ strncpy(winningteamnames[WINTEAMcounter], shuffledteamnames[index1]); ^ In file included from footballA2.cpp:4:0: /usr/include/string.h:131:14: note: declared here extern char *strncpy (char *__restrict __dest, ^ and when i use strcpy it only stores the value when WINTEAMcounter is 1, 2, or 3, but not 0. Is there any other information that would help debug this?
It should work with any value of WINTEAMcounter from 0 to 7. You haven't shown enough of your program to explain why there's any difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.