0

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;
        }
   }
}
4
  • 1
    Tip: Don't use #define in C++, use const with an appropriate type and value. Additionally reserving i is really inconvenient if not downright rude, that's expected to be free for use as an iterator variable. This would be better expressed as const int max_number = 10. Commented Feb 9, 2021 at 4:42
  • Tip: Use std::vector<int> and push_back. Don't use C arrays if you can help it. Commented Feb 9, 2021 at 4:45
  • (1) Use a temporary variable to read the value, and store it only if it is positive. (2) Use an if statement, instead of while, and a break. (3) Think more about the while condition. Commented Feb 9, 2021 at 5:00
  • sorry about reserving i. Won't happen again!! Commented Feb 9, 2021 at 5:09

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

2 Comments

Hi thanks for the reply, honestly I love using vectors but we haven't gone over them in class yet. Is there any way I could do this without using vectors?
Unfortunately this answer using arrays is that wrong that it would be better deleted. Defining an VLA in C++ does not even compile. And returning an C-Sytle array? That is defined on the stack and ceases to exist afterwards.Wow. @PatrickLe. Can you please delete your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.