-2

I want to save some numbers in a two-dimensional array until the user enters a zero that's the sign for the end of the process. How can I make this?
user enters something like this.

4586

6546

31358

0

NEW
  • 3
  • 3
  • 1
    And if you don't have a good book yet, then [here's a list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). Get a couple of them and start reading from the beginning. – Some programmer dude Feb 08 '19 at 07:40
  • 1
    Use vectors, much easier than arrays. But really you need to find a good book and start reading. – john Feb 08 '19 at 08:16

1 Answers1

0

I don't understand that you mention 2 dimensional array. Use vectors.

#include <iostream>
#include <vector>

// Returns true if s is a number else false
bool isNumber(std::string s) {
    for (int i = 0; i < s.length(); i++)
        if (isdigit(s[i]) == false)
            return false;

    return true;
}

int main(int argc, const char * argv[]) {
    std::string input;
    std::vector<int> myvector;
    int num;
    while (1) {
        std::cout << "Enter a number: ";
        std::getline(std::cin, input);

        if (isNumber(input) == false) {
            std::cout << "Only a number is allowed!'" << std::endl;
            continue;
        }

        num = std::stoi(input);
        if (num == 0) {
            break;   
        }

        myvector.push_back (num);


        std::cout << "Appended '" << input << "'. Vector is now:" << std::endl;
        for (int i = int(myvector.size()) - 1; i >= 0; i--) {
            std::cout << myvector[i]  << std::endl;
        }
    }

    return 0;
}