#My Code Keeps Printing Memory Addresses and Not Values

12 messages · Page 1 of 1 (latest)

clear timber
#

I'm having issues with the outputs of my code. I need to print the given values, but my programs prints the memory address instead.

`#include <iostream>
using namespace std;

double* read_data(int& size)
{
double* data = new double[10];
cout << "Please enter 10 values for your array: ";
for (int i = 0; i < 10; i++)
{
cin >> data[i];
size++;
}
cout << "\n";
cout << "The values you entered are: ";
for (int i = 0; i < 10; i++)
{
if (i < size - 1)
{
cout << data[i] << " | ";
}
else
{
cout << data[i] << "\n";
}
}
double* bigger_array = new double[16];
for (int i = 0; i < size; i++)
{
bigger_array[i] = data[i];
}
delete[] data;
cout << "\n";
cout << "Please enter 6 more values for your array: ";
for (int i = 0; i < 6; i++)
{
cin >> bigger_array[i];
size++;
}
cout << "\n";
cout << "The other values you entered are: ";
for (int i = 10; i < size; i++)
{
if (i < size - 1)
{
cout << bigger_array[i] << " | ";
}
else
{
cout << bigger_array[i] << "\n";
}
}
return bigger_array;
}
int main()
{
int size = 0;
double* data = read_data(size);
cout << "The size of your array is: " << size;
cout << "\n\n";
cout << "The final values of your array are: ";
cout << "\n";
for (int i = 0; i < size; i++)
{
if (i < size - 1)
{
cout << data[i] << " | ";
}
else
{
cout << data[i] << "\n";
}
}
delete[] data;

system("pause");
return 0;

}`

normal sageBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

clear timber
#

Test case for 10 values: 1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5

Test case for 6: 1.5 4.5 9.5 16.5 7.5 11.5

split warren
normal sageBOT
#

```cpp
int main() {
return 0;
}
```

split warren
#

like that

verbal relic
#

;compile vcpp_v19_latest_x64 | 1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5 1.5 4.5 9.5 16.5 7.5 11.5

#include <iostream>
using namespace std;

double* read_data(int& size)
{
    double* data = new double[10];
    cout << "Please enter 10 values for your array: ";
    for (int i = 0; i < 10; i++)
    {
        cin >> data[i];
        size++;
    }
    cout << "\n";
    cout << "The values you entered are: ";
    for (int i = 0; i < 10; i++)
    {
        if (i < size - 1)
        {
            cout << data[i] << " | ";
        }
        else
        {
            cout << data[i] << "\n";
        }
    }
    double* bigger_array = new double[16];
    for (int i = 0; i < size; i++)
    {
        bigger_array[i] = data[i];
    }
    delete[] data;
    cout << "\n";
    cout << "Please enter 6 more values for your array: ";
    for (int i = 10; i < 16; i++)
    {
        cin >> bigger_array[i];
        size++;
    }
    cout << "\n";
    cout << "The other values you entered are: ";
    for (int i = 10; i < size; i++)
    {
        if (i < size - 1)
        {
            cout << bigger_array[i] << " | ";
        }
        else
        {
            cout << bigger_array[i] << "\n";
        }
    }
    return bigger_array;
}
int main()
{
    int size = 0;
    double* data = read_data(size);
    cout << "The size of your array is: " << size;
    cout << "\n\n";
    cout << "The final values of your array are: ";
    cout << "\n";
    for (int i = 0; i < size; i++)
    {
        if (i < size - 1)
        {
            cout << data[i] << " | ";
        }
        else
        {
            cout << data[i] << "\n";
        }
    }
    delete[] data;

    //system("pause");
    return 0;
}
lethal depotBOT
#
Program Output
Please enter 10 values for your array: 
The values you entered are: 1.3 | 4 | 5.2 | 16.3 | 9.99 | 7.21 | 4.5 | 7.43 | 11.21 | 12.5

Please enter 6 more values for your array: 
The other values you entered are: 1.5 | 4.5 | 9.5 | 16.5 | 7.5 | 11.5
The 
verbal relic
#

🤔 what

#

ok well it looks fine to me testing elsewhere (the compiler bot seems to be bugging out for whatever reason)

#

i don't see any issue, and no memory address printing

normal sageBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.