0

I am trying to print out the 2 employees which one member contains a multidimensional array.

    struct Employees{
    string name;
    double salary;
    char skill[5][20];
 };
void main(){
Employees Namn[3] = {
    { "Dawn", 120000, { "C#", "C++" } },
    { "John", 13456, { "Java", "C++" } }
};
for (int i = 0; i < 3; i++){

    cout << Namn[i].name << "\t";
    cout << Namn[i].salary;
    cout << "\t";
    for (int j = 0; j < 5; j++){
        for (int k = 0; k < 20; k++){
            cout << Namn[i].skill[j][k];
            }
        }
        cout << endl;
    }
}
  • 2
    `I am trying to print out the 2 employees` And yet you're printing out 3 `Employees` in your code. – user657267 Nov 07 '14 at 01:37
  • Why do you allocate space for 3 employees but only provide data for 2? Turn the warning levels up to maximum on your compiler / IDE. You should have received a warning for this issue. – Thomas Matthews Nov 07 '14 at 01:53
  • In your initialization of the array, your are missing some skills. You should provide the missing skills (such as empty strings) and don't rely on the compiler to provide default values. – Thomas Matthews Nov 07 '14 at 01:56
  • **Big Question**: Why do you use `std::string` for the name but use `char` for the skills? I suggest you use an array of `std::string` for the names and drop the `char` type. – Thomas Matthews Nov 07 '14 at 01:57
  • @ThomasMatthews: Never saw the compiler complain about *too few initializers* in that situation yet. – Deduplicator Nov 07 '14 at 02:02
  • 1
    @ThomasMatthews: There's nothing wrong with relying on the compiler to provide default values. Unspecified members are initialized to zero. For `char` arrays, that means all elements are set to `'\0'`, which means they contain empty strings. (The loop that displays the values should probably be checking for that, though.) – Keith Thompson Nov 07 '14 at 02:02
  • `void main()` is incorrect; it should be `int main()`. Did your compiler not complain about that? – Keith Thompson Nov 07 '14 at 02:02
  • @KeithThompson: After being in maintenance for decades, and working on safety critical systems, I find that being explicit reduces a lot of defects. – Thomas Matthews Nov 07 '14 at 02:05
  • @user657267 I am sorry but i left that empty so that it can get default values. – hugry clide Nov 07 '14 at 02:26
  • @ThomasMatthews: If you give a number, you are being explicit that you want the rest zeroed out. Otherwise, just let the compiler figure out how many you wrote. Trust it not to mis-count. If you want to double-check some constant, use `static_assert`. – Deduplicator Nov 07 '14 at 02:27
  • @ThomasMatthews I am using string for the name because i dont really care abt that. I just wanted to use char so that i can learn how to play with the multidimensional array. That is the only thing that is important here. – hugry clide Nov 07 '14 at 02:29
  • @KeithThompson void main() can be used if you dont want a return value.. – hugry clide Nov 07 '14 at 02:30
  • @hugryclide: Not legally. The C++ standard explicitly requires `main` to return `int`. If you don't want a return value, you don't need a `return` statement; reaching the closing `}` does an implicit `return 0;`. Again, did your compiler really not complain about that? – Keith Thompson Nov 07 '14 at 02:32
  • @KeithThompson It didn't complain aboutt that, and I know it is not standard, but my instructor likes using that, so i use it aswell. – hugry clide Nov 07 '14 at 02:37
  • @all Thanks for your help, I guess the issue was with the default values. – hugry clide Nov 07 '14 at 02:41
  • Your instructor is giving you bad advice. – Keith Thompson Nov 07 '14 at 04:19

1 Answers1

-1

The -9.255 output is caused by the default values. I just needed to add { "0", 0.0, { "0", "0" } }. Thanks for your help guys.

  • 1
    In debug mode, memory is usually filled with hex cccccccccccccccc and hex cccccccccccccccc when interpreted as a double is -9.2559631349317831 x 10^061 – rcgldr Nov 07 '14 at 03:12
  • 1
    When you initialize a struct using `{ }`, any unlisted members must be value-initialized (which means initialized to `0.0` for a double); so if this is really the answer then your compiler is extremely broken – M.M Nov 07 '14 at 04:05
  • To know why it initializes uninitialized values to 0xCC read [this question](http://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new) – phuclv Nov 07 '14 at 06:15