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?

5
  • You're printing the array reference, not the array contents, in the second. In the first you're printing out whatever happens to be in memory after the last array element (zero indexed). Commented May 12, 2018 at 14:53
  • 2
    index 3 is out of bounds for an array of size 3. Commented May 12, 2018 at 14:54
  • Oh I see. So to print the contents of the array, I would have to use a for loop and print each individual element inside? Commented May 12, 2018 at 14:55
  • @Dracep Yep, afraid so. Commented May 12, 2018 at 15:00
  • The loop is by far the easiest of your options to write and read. I wouldn't try anything else unless you need to grub for nanoseconds. Commented May 12, 2018 at 15:03

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think you use the getline for keeping the console window open... please read stackoverflow.com/questions/454681/…
Thanks so much, this cleared up a lot of the confusion I had about arrays!
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.

4 Comments

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 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 cplusplus.com/doc/tutorial
@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 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!

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.