I have made a simple application to demostrate the problem I got in making a much more complicated one (didn't want to post all of it here). I don't know how to add character values longer than one letter to arrays.

#include <iostream>

using namespace std;

int main()
{
    char name;
    int score;
    int scores[11] = {1,2,3,4,5,6,7,8,9,10};
    char names[11][11] =
    {"None", "None", "None", "None", "None", "None", "None", "None", "None", "None"};
    
    int next;
    
    loopmenu:
    cout << "Type 1 to add new record, 2 to see high scores and 3 to exit.";
    cin >> next;
    
    switch(next)
    {
                case 1:
                     goto loopcontinue;
                     break;
                case 2:
                     goto loophighscores;
                     break;
                case 3:
                     return 0;
    }
    
    loopcontinue:
    cout << "Type name:";
    cin >> name;
    
    cout << "Type score:";
    cin >> score;
    
    if(score > scores[9])
    {
             cout << "New best score!";
             names[9][1] = name; // Here's the problem! What should I write here?
    }
    
    // Same code x9....
    
    loophighscores:
    cout << "Highscores: \n";
    for(int a = 0; a < 10; a++)
    {cout << a + 1 << ". " << scores[a] << " points by " << names[a] << "\n";};
    goto loopmenu;
    
    std::cin.get();
    return 0;
}

Thank you!

EDIT: And could someone please tell me any possible alternatives to using goto, as I know that everyone hates it.

Recommended Answers

All 3 Replies

What are you trying to do and what is it doing wrong? It looks like you read one character of input from the reader and store it in "name" char variable, then set the first element of names[9] equal to that character. If you're tying to use whole names, you should read in a string/char array rather than a single character.

Thanks, I did a bit of googling and I think I found the answer.

I would make 'name' a char array (cstring):

//line #7
char name[80];

Now you can actually accept a name from the user:

//Line #32
cout << "Enter ye' name: ";
cin >> name;

Since you are using cstrings (character arrays) you should become very familiar with the <cstring> library. One function you may be interested in is called, strcpy():

#include<cstring>

//Line #41 
//Be careful here with names[11][11], your 'name' must be less than 11 characters (leaving room for a null terminating character)
strcpy(names[9][0], name);

Other suggestions:

  • Always make sure your character arrays are null terminated.
  • Your use of goto is frowned upon by the modern OOP programming community (it leads to a phenomenon called "spaghetti code"). Instead, try making function calls:
switch(next)
{
     case 1:   loopcontinue();
     break;
     case 2:   loophighscores();
     break;
     case 3:   exit(0);
     break;
     default:  display_error();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.