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

int num[3]{ 3, 5, 6, };
cout << num[3] << endl;

string y;
getline(cin, y);
return 0;
}

gives an output of -858993460

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

int num[]{ 3, 5, 6, };
cout << num << endl;

string y;
getline(cin, y);
return 0;
}

gives an output of 004FFC48

But I would like to have my output be 356. Why am I receiving different outputs in the two code examples stated above?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Dracep
  • 388
  • 2
  • 13

2 Answers2

2

read your code and answer me, does y have any relation with the num array?
Of course not, is just another variable.

Another error in int num[]{ 3, 5, 6, };
_________________________________^__
remove the , you are just saying that your array will have 4 elements and you are not saying wich number will be in the last space, so the compiler just puts garbage in there and then you print the num variable space since arrays are like pointers but no the same.
(suggestion, remove the comma and remember the computer makes the instructions in ascendent order from line 1 to N)

if your want to make the output 356 you need to convert the int datatype to char because string is a set of chars. So make your own stringify function

#include <iostream>
#include <string> // is ambiguous because iostream already have string
using namespace std;
// where is the main function?
int num[]{ 3, 5, 6, };
cout << num << endl;// this should be in a for statement at the end of the program because you output the proccesed values

string y; // container of chars
getline(cin, y); //why do you need this?
return 0;
}

Fixed:

#include <iostream>
using namespace std;

int main() {
    //just for printing the numbers
    int num[]{3, 5, 6};

    for (int i = 0; i < 3; i++)
        cout << num[i] << endl;

    return 0;
}
Egon Stetmann.
  • 461
  • 6
  • 14
  • 1
    I think you use the getline for keeping the console window open... please read https://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c – Egon Stetmann. May 12 '18 at 15:28
  • Thanks so much, this cleared up a lot of the confusion I had about arrays! – Dracep May 12 '18 at 15:51
1

First, as a basic recommendation, read about the topic of pointer arithmetics for educational reasons.

Now, let's give you some code you can work with.

A raw array is somewhat unwieldy to use. There are situations in which you want to do that, but not in this one. What you want to do here is to use STL containers, such as std::vector. This one behaves like an array but also knows it's size and does some other nice things for you. If you use C++11 or higher, you can initialize those with a list, just like you did with the array:

std::vector<int> numbers = { 3, 5, 6 };

The next thing, there is no native printing for arrays or similiar types. A good thing to do here would to write a function that does that:

void print(const vector<int>& vec)
    for(size_t i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << " ";
    }
    cout << endl;
}

As you see, this works pretty well because vector has the method size(). We don't have to use a 3 in our code (a "magic number") and we have written a piece that can be reused in the future (our function).

Not the actual answer to your question, but I thought that this is something that helps you in the future. In general, take a good look at the things that the STL provides, those are your basic tools.

Aziuth
  • 3,652
  • 3
  • 18
  • 36
  • Thanks so much! I recently started C++ and your post gave me insight on how to write vectors more effectively for the future. Do you have any specific reads or video tutorials for C++? – Dracep May 12 '18 at 16:09
  • 1
    @Dracep first learn how to use the basics of C++, STL in general is for more skilled complex things, the reason STL provides more friendly functions is just because the C++ programmer know what that functions do and saves time when developing, please read http://www.cplusplus.com/doc/tutorial/ – Egon Stetmann. May 12 '18 at 22:49
  • 1
    @Dracep Most learning is done by doing. Do you know about CodeReview, another site within StackExchange? I think the best way is to write some stuff, posting it on CodeReview, get it ripped apart and learn by that. In answer to Egons post, I disagree on the STL being something to learn later. One should obviously learn the basics and the tutorial he posted is good, but I'd start especially with STL containers from the start. But remember to go back and learn how raw pointers and raw arrays work, what the heap is and about `new` and `delete`, for example. – Aziuth May 13 '18 at 09:28
  • @Aziuth Again, thanks for the insight on which topics are the most essential to learn first. I'd say that I need to look into STL's more through more tutorials. I will check out CodeReview as well! – Dracep May 13 '18 at 17:17