Home > Blockchain >  I am not able to print data from dynamic array
I am not able to print data from dynamic array

Time:01-29

In this problem, I have to create a dynamic array and get data from the user, If the user keep entering value, a new array should be created and the data of the old array should be added to new array. and the old array should be deleted. I am not able to print the data from the new array. It is only printing garbage values. can you check where the problem is ?

#include<iostream>
using namespace std;
int main() {

    int size = 5;
    int* myarrptr = new int[size];
    int* newarr = 0;
    int count = 0;
    int count2 = 0;
    int i;
    for (i = 0; i <= size; i  ) {
        
        if (i == size) {
            count = *((myarrptr   i) - 1);
             newarr = new int[size*2];
            for (i = 0; i < size; i  ) {
                *(newarr   i) = *(myarrptr i);
            }
            size = size   size;
            delete[] myarrptr;
            myarrptr = newarr;
            *(myarrptr   i) = count;

        }
        cin >> *(myarrptr   i);
        if (*(myarrptr i) == -1) {
            break;
        }
        count2  ;
        
    }
    
    delete[] myarrptr;

    cout << newarr[3];
    

}

Here is the code

I am trying to print the data from the newarr but I am getting garbage values.

CodePudding user response:

This answer is not only for you but for other readers as well.

It looks like you are learning C from an outdated source, look at cppreference for examples. Get a recent C book or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date)

Example of what you want to do in current C

#include <iostream>
#include <vector>

// output of a datatype in C   is best generalized by overloading the output stream operator <<
// this will make it work with all kind of streams like std::cout, and std::ofstream (files)

std::ostream& operator<<(std::ostream& os, const std::vector<int>& values)
{
    bool comma = false;
    os << "[";

    // range based for loop instead of index based
    // this will never run outside the boundaries of your "array"
    for (const int value : values)
    {
        if (comma) os << ", ";
        os << value;
        comma = true;
    }
    os << "]";
    return os;
}

int main(int argv, char** argc)
{
    std::size_t size;
    std::cout << "Enter number of numbers : ";
    std::cin >> size;

    // for dynamically allocatable arrays use std::vector
    // current C   code should almost never need new/delete and raw pointers
    std::vector<int> values(size); // your dynamic memory allocation happens here

    // loop over all values by reference so you can modify them
    for (int& value : values)
    {
        std::cout << "Enter a number : ";
        std::cin >> value;
    }

    std::cout << values;
   
    return 0;
}

CodePudding user response:

There a solution already implemented in the standard template library in the vector header file. I would only use raw arrays in game development or constrained environments or to ethically hack. Look at the code I implemented for you. Also, I implemented a sentinel question to exit or continue because if you use a -1 as sentinel, you will not be able to add -1 as a value to display.

#include <iostream>
#include <vector>

int main() {
    std::cout << "Enter values to display:" << std::endl;

    bool shouldExit = false;
    std::vector<int> values = std::vector<int>(0);

    do {
        std::cout << "Continue? (y/n): ";
        char characterInput = '\0';
        std::cin >> characterInput;

        switch (characterInput) {
            case 'y':
            case 'Y':
                break;
            case 'n':
            case 'N':
                shouldExit = true;
                break;
            default:
                std::cout << "Error: Enter valid input: \'y\' or \'n\' (case insensitive)" << std::endl;
                continue;
        }

        std::cout << "Enter integer value: ";
        int value = 0;
        std::cin >> value;

        values.push_back(value);
    } while (!shouldExit);

    if (values.size() == 0) {
        std::cout << "No values to print" << std::endl;
    }
    else {
        std::cout << "Values: " << std::endl;
        for (unsigned int i = 0; i < values.size(); i  )
            std::cout << "[" << i << "] = " << values[i] << std::endl;
    }
}

If you need the raw array from a vector, you can always use the data() function.

CodePudding user response:

your only printing the 4th value in the array with cout << newarr[3]; you'll need to add a for loop to print all the values

  • Related