#First character isn't being displayed. (Ptrs and arrays)

22 messages · Page 1 of 1 (latest)

gilded nimbus
#
#include <iostream>

int length(char *str) {
    int length = 0;
    while (*(str + length) != 0) {
        length++;
    }
    return length;
}


void input(char **array, int rows, int columns) {
    for (int i = 0; i < rows; i++) {
        *(array + i) = new char[columns];
        char* data = new char[columns];
        std::cout << "Enter input for the {" << i << "} row\n";
        std::cin.ignore();
        std::cin.getline(*(array+i), columns);
    }
}

void output(char **array, int rows, int columns) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            std::cout << *(*(array + i) + j) << " ";
        }
        std::cout << std::endl;
    }
}

int main() {
    int rows = 0;
    std::cout << "How many rows does the input contain?";
    std::cin >> rows;
    int columns = 0;
    std::cout << "How many columns does the input contain?";
    std::cin >> columns;

    char **array = new char *[rows];
    input(array, rows, columns);
    output(array, rows, columns);
}```
full otterBOT
#

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 use !howto ask.

ashen anvil
#

you would do well by replacing this with std::string ngl

coral surge
#

data is being useless there.

ashen anvil
#

but most likely there is garbage memory in there somewhere

coral surge
#

You issued std::cin.ignore(), which skipped the first character.

#

You need to learn about the leftover newline in the input buffer, when it's there and when it isn't.

gilded nimbus
ashen anvil
gilded nimbus
#

I'll get back to that once my input is correct.

gilded nimbus
#

after getline?

coral surge
#

It should be between an input that creates a leftover newline and the getline call.

#

One such input is an integer input.

#

After you have taken the first row, it's between two getline calls and you must not issue ignore.

gilded nimbus
#

Thanks

#

for the hint

#

so I'll be adding cin.ignore() after the column input.

#

Not in the loop

#

!solved

full otterBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity