简体   繁体   中英

How to end a user input array

So this is for a lab assignment and I already have it working, but one thing is bothering me. The assignment involves creating a 1-dimensional array and then manipulating it. I am supposed to allow a max of 100 inputs but the user does not have to use all 100. Right now, I am using a while statement to either break or allow another input to be entered. To break the statement, you have to enter a negative number (this is what I don't like and want to change). What other options are there to end the user input, once they are done entering their numbers? Is it possible to end the loop once you hit enter with nothing typed?

I have searched stackoverflow for the last 3 days and found some compelling stuff but could never get it to work.

Note, I get the void function is redundant here but that's besides the point (unless it actually affects my ability to achieve what I want).

Also, thanks in advance.

here is my code so far (my while statement is in the main)... be kind I'm a newbie to coding.

#include <iostream>

using namespace std;

void reverseElements(int array[], int size)
{
    int tmp;
    int j;
    int i = size;

    j = i - 1;
    i = 0;

    while (i < j)
    {
        tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
        i++;
        j--;
    }

    cout << "I will now reverse the elements of the array." << endl;
    cout << endl;

    for (i = 0; i < size; i++)    
    {
        cout << array[i] << " " << endl;
    }
}

int main()
{
    const int NUM_ELEMENTS = 100;
    int iArr[NUM_ELEMENTS];
    int i;
    int myInput;

    cout << "Enter your numbers, then enter a negative number to finish" << endl;
    cout << endl;

    for (i = 0; i < NUM_ELEMENTS; i++)  //loop to obtain input
    {
        cin >> myInput;

        if (myInput < 0)   //checks for negative number to end loop
        {
            break;
        }

        else   //continues to allow input
        {
            iArr[i] = myInput;
        }
    }

    cout << endl;

    reverseElements(iArr, i);

    return 0;
}

Probably the easiest solution: let your user choose how many numbers to write before actually writing them.

int readNumbersCount()
{
  int const numbersMin = 1;
  int const numbersMax = 100;
  int numbersCount = -1;
  while (numbersCount < numbersMin || numbersCount > numbersMax)
  {
    std::cout <<
      "How many numbers are you going to enter? Choose from " <<
      numbersMin << " to " << numbersMax << ":\n";
    std::cin >> numbersCount;
  }
  return numbersCount;
}

int main()
{
  int const numbersCount = readNumbersCount();
  for (int i = 0; i < numbersCount; ++i)
  {
    // read the numbers etc.
  }
  return 0;
}

I wrote readNumbersCount() as a separate function to extract numbersMin and other "one-use" identifiers from main() and to make main() 's numbersCount const .

I have edited the main function a little bit. Here the user is asked how many elements he wants to enter. and doing the memory allocation dynamically so as to save space

int main()
{   int n=101;
    while(n>100){
    cout<<"How many numbers do you want to enter";
        cin>>n;
    }
    int *ptr=new(nothrow)int[n]; 
    for (int i=0;i<n;i++){
        cout << "Enter your number" << endl;
        cin>>ptr[i];
    }

cout << endl;

reverseElements(ptr, n);

return 0;
}

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