-1

So the requirements for this program is to be able to increment arrays of the same size (size from 5 to 15 indexes) and increment each element in the array by one using for and while loops. The last task is to take values from the first array and put them in reverse order and assign them to the second array.

So everything works as normal, and the program rejects invalid inputs and does not go into an infinite loop. However, the program accepts some inputs that are not wanted.

For example, I would input something like '12 a' or '7 asdfkla;j lasnfg jasklgn asfg' and it would go through. It is interesting too because the code registers only 12 or 7 and completely ignores the rest. I think it is because once it hits a non-integer character, it would stop ignore the rest.

Why is it ignoring the rest of the input? And is there a way to catch this error from going through?

Also, if you see anything that catches your eye, feel free to critique c: I am always looking to improving.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(NULL));
    int x;
    int j = 0;
    bool not_valid = true;

    system("color f");

    cout << "Program will ask for an input for the size of an array.\n"
         << "With the array size defined, program will generate semi-\n"
         << "true random integers from 0 to 8. First array will then\n"
         << "be assigned to the second in reverse (descending) order.\n\n";

    do {
        cout << "Enter array size (0 - 15): ";
        cin >> x;

        if (x >= 5 && x <= 15) {
            not_valid = false;
            cout << "\nArray size: " << x << endl;
        }
        else {
            cout << "Invalid input.\n\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

    } while (not_valid);

    int *arr0;
    int *arr1; 
    arr0 = new int[x];
    arr1 = new int[x];

    for (int i = 0; i < x; i++) {
        arr0[i] = rand() % 9;
    }

    for (int i = 0; i < x; i++) {
        arr1[i] = rand() % 9;
    }

    cout << "\nARRAY 0 (unmodified, for):\n";
    for (int i = 0; i < x; i++) {
        cout << arr0[i] << "\t";
    }

    cout << "\n\nARRAY 0 (modified, for):\n";
    for (int i = 0; i < x; i++) {
        arr0[i]++;
        cout << arr0[i] << "\t";
    }

    cout << "\n\nARRAY 1 (unmodified, while):\n";
    for (int i = 0; i < x; i++) {
        cout << arr1[i] << "\t";
    }

    cout << "\n\nARRAY 1 (modified, while):\n";
    while (j < x) {
        arr1[j]++;
        cout << arr1[j] << "\t";
        j++;
    }

    int second = x - 1;

    for (int i = 0; i < x; i++) {
        arr1[second] = arr0[i];
        second--;
    }

    j = 0;
    cout << "\n\nARRAY 1 (array 0, descending):\n";
    while (j < x) {
        cout << arr1[j] << "\t";
        j++;
    }

    cout << endl << endl;
    system("pause");
    return 0;
}
Korywon
  • 21
  • 1
  • 5
  • Already answered here: http://stackoverflow.com/questions/10828937/how-to-make-cin-take-only-numbers – Apara Jan 26 '17 at 05:24
  • Try using `std::getline` to read the user's input as one big `std::string`, then verify that the `std::string` is a number and convert it with `std::stoi`, and then, assuming the input has made it this far, check the bounds on the number and approve is inbounds. – user4581301 Jan 26 '17 at 05:26
  • In [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) take advantage of the `pos` parameter to ensure `stoi` consumed the entire `string` and that there is no crap like " asdfkla;j lasnfg jasklgn asfg" hanging around after a valid number. – user4581301 Jan 26 '17 at 05:30
  • This is correct: "I think it is because once it hits a non-integer character, it would stop ignore the rest." – Cheers and hth. - Alf Jan 26 '17 at 06:00

1 Answers1

0

Take input in string and then check if it's a number or not.

Example:

#include<iostream>
#include<sstream>
#include <string>
using namespace std;

int main()
{
    string line;
    int n;
    bool flag=true;
    do
    {
        cout << "Input: ";
        getline(cin, line);
        stringstream ss(line);
        if (ss >> n)
        {
            if (ss.eof())
            {
                flag = false;
            }
            else
            {
                cout << "Invalid Input." << endl;
            }
        }
    }while (flag);
    cout << "Yo did it !";
}
Ahsan
  • 412
  • 5
  • 17