简体   繁体   中英

validation for array user input c++

I've been trying to find a way to get user input from keyboard for an array. The user can enter up to 100 elements. If at any point the user wishes to stop, they enter 0 and the array will not take anymore inputs. When I run the code below and enter 0 it still goes through the while loop. I don't know what I am doing wrong. Looking for help with this problem!!

#include <iostream>
#include <iomanip>

using namespace std;

#define i 10

void getElements(int array[]);

int main()
{  
    int array[i];
    getElements(array);

    cout << array;
    return 0;
}
void getElements(int array[])
{  
   cout << "Please enter up to " << i 
        << " positive nonzero integer elements. Enter 0 to stop input.\n" << endl;
   for(int count = 0; count < i; count++)
   {  
        cout << "array[" << count << "]: ";
        cin >> array[count];
        while((!array[count]) || (array[count] < 0))
        {   
            cin.clear();
            cin.ignore(10000, '\n');
            cout << "Sorry, invalid input. Was expecting a positive integer." 
                 << " Please try again." << endl;
            cout << "array[" << count << "]: ";
            cin >> array[count];
        }
        if (array[count] == 0)
        {
            // code to stop input from keyboard.
            cout << "stop input" << endl;
        }
   }
}

From what i understood, you want a way to stop when user enter 0. to do so you can use the break keywords when you are inside a loop.

if (array[count] == 0)
    {
        cout << "stop input" << endl;
        break;
    }

break keyword have a different meaning depending on the situation you are using it for.

When it comes to loop. it tells that you want to stop iterating the loop "you want to break out of the loop"(if it make sence).

I really recommend avoiding Arrays when doing this. I believe the problem lies in that you didn't initialize the size of the array. Here's the code using std:vector<int>

vector<int> getElements(int maxCount)
{  
  vector<int> result = new vector<int>();
  cout << "Please enter up to " << maxCount << " positive nonzero integer elements. Enter 0 to stop input.\n" << endl;

  int i = 0;
  while(i < maxCount)
  {  
    int userInput = "";

    cout << "array[" << count << "]: "; 
    cin >> userInput; 

    if (userInput < 0)
    {
      cout << "Sorry, invalid input. Was expecting a positive integer. Please try again." << endl;
    }
    else if (userInput == 0)
    {
      cout << "stop input" << endl; 
      break; // Break out of the while loop
    }
    else
    {
      result.push_back(userInput);
      i++;
    }
  }

  return result;
}

Here's the Code using Arrays

int[] getElements(int maxCount)
{  
  int[] result[maxCount];
  cout << "Please enter up to " << maxCount << " positive nonzero integer elements. Enter 0 to stop input.\n" << endl;

  int i = 0;
  while(i < maxCount)
  {  
    int userInput = "";

    cout << "array[" << count << "]: "; 
    cin >> userInput; 

    if (userInput < 0)
    {
      cout << "Sorry, invalid input. Was expecting a positive integer. Please try again." << endl;
    }
    else if (userInput == 0)
    {
      cout << "stop input" << endl; 
      break; // Break out of the while loop
    }
    else
    {
      result[i] = userInput;
      i++;
    }
  }

  return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM