2

I'm having trouble storing a char sequence array of a defined length in to an object of a struct. I can make it work without defining a char length or just using a string but it just bothers me why this is happening.

The code:

#include <iostream>
#include <string>
using namespace std;

struct highscore {
char name[50];
int score;
char date[10];
} hstable[9];

void printtable (highscore show_tab) {
cout << show_tab.name << show_tab.score << show_tab.date;
};

void main() {
hstable[0].name = "Kyle ";
hstable[0].score = 100;
hstable[0].date = " 01/03/88 \n";

printtable (hstable[0]);
system("pause");
 return;

};

Error :

error C2440: '=' : cannot convert from 'const char [6]' to 'char [50]' 1> There is no context in which this conversion is possible

error C2440: '=' : cannot convert from 'const char [12]' to 'char [10]'

UzumakiDev
  • 1,286
  • 2
  • 17
  • 39

4 Answers4

5

If you want to do this, you should be using the strcpy (or strncpy) function from the <cstring> header.

strcpy(hstable[0].name, "Kyle ");

But please consider using std::string instead of plain char arrays.

Note: char[10] is too small to store " 01/03/88 \n" as a C string, so you've already fallen in one of the many traps that C strings offer (buffer overflow in this case).

Mat
  • 202,337
  • 40
  • 393
  • 406
4

You cannot assign arrays in C++ (and a string literal is a const char array). You have to copy them element-by-element, and for null-terminated char arrays the way to do that is with strncpy.

A much better, C++-style way to do this would be to make name and date into std::strings, though, which you can assign with the obvious syntax.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I understand I should be using std::string but even then when I define a length I still receive the same error. So basically I want to use std::string name[50]; – UzumakiDev Jul 31 '11 at 14:42
  • 2
    No no no, just `std::string name;` The string manages variable-length char arrays. – Kerrek SB Jul 31 '11 at 14:43
  • ok but what if say i wanted names that were only 3 characters long? – UzumakiDev Jul 31 '11 at 14:50
  • You simply don't worry about any of this and move on to solve your actual problems! :-) (Unless you actually want to *enforce* a maximum length.) – Kerrek SB Jul 31 '11 at 14:51
  • Yes I want to enforce a maximum length how would I go about that? – UzumakiDev Jul 31 '11 at 15:01
  • Sure, do it during the assigmnent: `mystr = yourstring.substr(0, 3);`. But think again whether you really need this restriction. It's possible, but perhaps you don't need it after all. – Kerrek SB Jul 31 '11 at 15:02
  • @Kyle: `if (mystr.length() > 3) mystr.resize(3);` -- Note that if you are using char arrays simply because you can specify the length, you're not actually enforcing anything, you're just allowing buffer overflows in the cases where the array is assigned something larger than it can hold. – Benjamin Lindley Jul 31 '11 at 15:27
2

This is what you program in C++ should more or less look like

#include <iostream>
#include <string>
#include <vector>

struct HighscoreEntry {
    std::string name;
    int score;
    std::string date;

    HighscoreEntry(const std::string& name,
                   int score,
                   const std::string& date)
        : name(name), score(score), date(date)
    { }
};

std::vector<HighscoreEntry> high_scores;

std::ostream& operator<<(std::ostream& s, const HighscoreEntry& hs) {
    return s << hs.name << " " << hs.score << " " << hs.date << "\n";
}

int main(int argc, const char *argv[]) {
    high_scores.push_back(HighscoreEntry("Kyle", 100, "01/03/88"));
    std::cout << high_scores[0];
}

Why? There are so many reasons that an SO answer is not appropriate to contain them all... there are books for that. You should pick a good C++ book and read it from cover to cover to learn C++. Just typing in some code in a compiler hoping to learn it with logic and experimentation is a recipe for a disaster with C++.

It doesn't matter how smart you are... you cannot learn C++ that way. Actually in a sense the smarter you are and the harder it will be (because you will try to use logic to fill gaps but there are places in which C++ is not logical at all - mostly for historical reasons).

C++ can be a very nice language, but approach it from the wrong side and it can become your worst nightmare (well... either your nightmare or the worst nightmare for the users of your C++ software).

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265
0
  • In C++, you should use std::string
  • In C, to copy a string literal to an array of characters, use strcpy(), or, better yet, memcpy().
ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • Because you make sure that the caller knows the length of the string. – ninjalj Jul 31 '11 at 15:19
  • Not sure I understand; IMO `memcpy(dst, src, 1+strlen(src))` has nothing better than `strcpy(dst, src)`... both of them can overflow if `dst` is not large enough and the `memcpy` version is also traversing the string twice. – 6502 Jul 31 '11 at 15:26