简体   繁体   中英

c++ why am I getting junk outputting an array?

Here's the code:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int keyArray[7] = {1,2,3,4,5,6,7};
    int breakPoint;
    int counter;
    for (counter = 0; counter < 7; counter++)
    {
        //      keyArray[counter] = (rand() % 9) + 1; later
        keyArray[counter] = counter;  //testing
    }
    cout << keyArray[0] + "\n";
    cout << keyArray[1] + "\n";
    cout << keyArray[2] + "\n";
    cout << keyArray[3] + "\n";
    cout << keyArray[4] + "\n";
    cout << keyArray[5] + "\n";
    cout << keyArray[6] + "\n";
    cin >> breakPoint;  //so I can see what the hell is going on before it disappears
    return 0;
}

The only reason I gave values to keyArray was that I read in answer to a similar question that you have to initialize an array with data before you use it. But it made no difference. The output is just junk symbols whether you initialize or not.

The compiler is Visual Studio Community 2017. Thanks for any help.

The error is not in your logic but rather in your debugging output. Since the other answers focus on how to fix it, I'll rather explain what happens instead. There seems to be a misunderstanding about the way strings work in C++.

The failure is in this operation:

keyArray[0] + "\\n"

Internally, string literals are arrays of characters, in this case const char[2] , consisting of the newline and a terminating '\\0' null terminator. When you then try to add the integer and this array together, the array will be represented by a pointer to its first element, ie it will decay to const char* in order to be used as the second argument to the plus operator used in your code.

So for the compiler, this line will need operator+(int, const char*) . But the result of that will be const char* , the input pointer offset by the integer, as that is the operation that happens when adding integers to pointers.

So instead of printing the number and then the string, it will try to access a string that does not exist as the pointer now pointer behind the string "\\n" and thus into some arbitrary memory.

Instead of doing

cout << keyArray[0] + "\n"

do:

cout << keyArray[0] << "\n"

or

cout << keyArray[0] << endl

You can't concatanate an integer with a string. That's why you got garbage output

Try this first:

cout << keyArray[0] << "\n";

If you are using compilers that support C++ 11 then try using std::to_string(...) to make a string from an integer before doing the addition:

cout << (std::to_string(keyArray[0]) + "\n");

you cannot concatenate int with string.

change

cout << keyArray[0] + "\n";
cout << keyArray[1] + "\n";
cout << keyArray[2] + "\n";
cout << keyArray[3] + "\n";
cout << keyArray[4] + "\n";
cout << keyArray[5] + "\n";
cout << keyArray[6] + "\n";

to

cout << keyArray[0] << "\n"
<< keyArray[1] << "\n"
<< keyArray[2] << "\n"
<< keyArray[3] << "\n"
<< keyArray[4] << "\n"
<< keyArray[5] << "\n"
<< keyArray[6] << endl;

You need to convert the integers into a string. Using a relatively recent version of C++:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int keyArray[7] = {1,2,3,4,5,6,7};
    int breakPoint;
    int counter;
    for (counter = 0; counter < 7; counter++)
    {
        //      keyArray[counter] = (rand() % 9) + 1; later
        keyArray[counter] = counter;  //testing
    }
    cout << std::to_string(keyArray[0]) + "\n";
    cout << std::to_string(keyArray[1]) + "\n";
    cout << std::to_string(keyArray[2]) + "\n";
    cout << std::to_string(keyArray[3]) + "\n";
    cout << std::to_string(keyArray[4]) + "\n";
    cout << std::to_string(keyArray[5]) + "\n";
    cout << std::to_string(keyArray[6]) + "\n";
    cin >> breakPoint;  //so I can see what the hell is going on before it disappears
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM