# For loops and user input

I'm wanting to take user input and store it into an array.There is a a minimum and maximum number of input but otherwise the user should be able to enter any number of integers in between. Say the max number is 10 but I only want to enter 5 numbers. I used a for loop but it only ends if the maximum number is entered. Is there a way to make it end after the user has entered the desired number of integers? Or should I use a different method? (Also I'm not sure how the code editor works)

Code:
const int MAXSIZE = 10;
int Input[MAXSIZE];

for (int i = 0; i<MAXSIZE ; i++)
{
cout << "Enter a positive integer: ";
cin >> Input[i];

if (Input[i]< 0) // terminate loop if negative number is entered
break;
}
}

Last edited by a moderator:

## Answers and Replies

Related Programming and Computer Science News on Phys.org
phinds
Science Advisor
Gold Member
2019 Award
Why not have the user first enter the number of elements he wants to input and use THAT as the loop count? OR, have the user enter some particular value that flags no more entries, something like -9999

QuantumQuest
Science Advisor
Gold Member
I think that the best is what phinds suggests, otherwise you'll need some if construct inside your for loop like:
C:
if (i < MAXSIZE/2) {
// take user input
}
It is also a very good idea to combine some needed things into your loop conditional like:
C:
while ( i < MAXSIZE && userInput != -9999) {
// do whatever you need here
}

Last edited:
Mark44
Mentor
OR, have the user enter some particular value that flags no more entries, something like -9999
The OP is doing something like this, except that instead of a specific sentinel value like -9999, he is terminating the loop on any negative integer. In the loop, the user is prompted to enter a positive integer, but the prompt should also include a message that a negative value terminates input.

phinds
Science Advisor
Gold Member
2019 Award
The OP is doing something like this, except that instead of a specific sentinel value like -9999, he is terminating the loop on any negative integer. In the loop, the user is prompted to enter a positive integer, but the prompt should also include a message that a negative value terminates input.
Thanks. I just glanced at the code but was focusing on the question so overlooked the obvious.

QuantumQuest
Science Advisor
Gold Member
The OP is doing something like this, except that instead of a specific sentinel value like -9999, he is terminating the loop on any negative integer. In the loop, the user is prompted to enter a positive integer, but the prompt should also include a message that a negative value terminates input.
Actually, OP is doing this but he has to include a message prompting user for negative values terminating the loop. If on some other case, negative values are permitted input, he has to specify something like -1 or -9999 (a matter of usual practice).

Last edited:
Mark44
Mentor
Actually, OP is doing this and he can include a message prompting user for negative values.
That's pretty much what I said.
QuantumQuest said:
Alternatively, some specific negative value like -1 or -9999 would do the same thing in a better way, avoiding loop termination from negative values inadvertently entered.
The prompt should indicate that either a) any negative number terminates the loop, or b) a specific negative number terminates the loop. Since the existing prompt asks for positive numbers, the user should be able to enter 0 to terminate the loop.

As long as the program provides some information to the user how to terminate early, it works for me. In a user interface, you have to make some assumptions about the ability of the person using the program.

QuantumQuest
Science Advisor
Gold Member
Yes, I was already editing my post.

jtbell
Mentor
Instead of using a fixed-size array and having to prevent the user from "walking past the end", you should seriously consider using a vector instead. A C++ vector works a lot like an array, but with extra features. For example, it can grow automatically to accommodate the input, and it can tell you how big it is at any time.

Code:
#include <iostream>
#include <vector>

using namespace std;

int main ()
{
vector<int> Input;  // initially size is 0
while (true)
{
cout << "Enter a positive integer: ";
int num;
cin >> num;
if (num < 0)
break;
else
Input.push_back(num);  // append num to Input and increase the
// size of Input as needed
}

// the size() member function tells you how big the vector is

cout "You entrered:" << endl;
for (int k = 0; k < Input.size(); ++k)
{
cout << Input[k] << endl;
}

return 0;
}

Svein
Science Advisor
Sigh. Both the OP and the answer assumes that the input from the user is reasonable. What if the user inputs the string "four"? What if the input is empty?

It reminds me - back in the nineties I was employed at Tandberg Data. One of the things we produced was keyboards, containing a small microcontroller. The guy who did the development had his code working and proudly announced that to his group leader. In order to test his code, she turned the keyboard upside down and sat on it - the keyboard promptly stopped working. No - it was not finished, since it was not "idiot proof".

jtbell
Mentor
First one learns to crawl, then to walk, then to run; then how to train for a marathon, then for the Ironman...

Mark44
Mentor
First one learns to crawl, then to walk, then to run; then how to train for a marathon, then for the Ironman...
I agree. In the context of this problem, it seems reasonable to me to assume some level of intelligence on the part of the program user. IOW, if the user is prompted to enter a positive integer, it can be assumed that he/she won't type in "four" or the like. For a commercial program, OTOH, it's reasonable to assume that some users will be idiots, and to write the program accordingly.

Silicon Waffle and Jaeusm
Thanks for the responses. I tried implementing some of your suggestions. Since I'm just a beginner programmer I having trouble with this other part of the program where I want to sort the array of numbers. I think I'm on the right track using pointers but the way I've got it set up it only prints the address of the first element however many integers the user inputs. Really I'm just trying to understand pointers and why it keeps outputing the same address. Also I am trying to get the hang of pass by reference. If one of the function parameters is a pointer can I pass the array of user inputs into it and then print that array sorted? This code is a mess but I am trying to learn how everything is working and why this doesn't work.

Code:
void sort(int *x, int j)
{

for (int i = 0; i < j-1; i++)
{
//const int j = 5;
int num[j];
int *pnum;
pnum = num;
int k = i;
while (i > i + 1)
{
if (num[k - 1] > num[k])
{
num[k] = num[k - 1];
// I feel like there needs to be an increment or decrement operator here
}
cout << num <<endl; // prints out the same address j-1 times
}
}
}

I'm sorry that I am doing something wrong with the code editor. How am I suppose to use it properly?
Edit: I figured it out.

Mark44
Mentor
Thanks for the responses. I tried implementing some of your suggestions. Since I'm just a beginner programmer I having trouble with this other part of the program where I want to sort the array of numbers. I think I'm on the right track using pointers but the way I've got it set up it only prints the address of the first element however many integers the user inputs. Really I'm just trying to understand pointers and why it keeps outputing the same address. Also I am trying to get the hang of pass by reference. If one of the function parameters is a pointer can I pass the array of user inputs into it and then print that array sorted? This code is a mess but I am trying to learn how everything is working and why this doesn't work.

Code:
void sort(int *x, int j)
{

for (int i = 0; i < j-1; i++)
{
//const int j = 5;
int num[j];
int *pnum;
pnum = num;
int k = i;
while (i > i + 1)
{
if (num[k - 1] > num[k])
{
num[k] = num[k - 1];
// I feel like there needs to be an increment or decrement operator here
}
cout << num <<endl; // prints out the same address j-1 times
}
}
}
You can't declare num as you did; i.e., as int num[j]; To declare an array, its size must be known at compile time. Passing j as a parameter means that the size of the array isn't known until run time.
Your sort function has a parameter x, which is a pointer, but there is no further mention of x in your code. The name of an array is a kind of pointer (a constant pointer). You could use your parameter x in place of the local array num.

In your last line what you are doing is printing num (an address) each iteration of the loop. Again, the name of an array evaluates to the address of the first element of the loop. num[k] evaluates to the element at index k.

Edit: I just noticed this: while (i > i + 1)
This is never true -- a number can't be larger than 1 more than that number, so the loop body will not execute at all. The output statement you have won't execute for this reason. And besides, your sort() function shouldn't be doing output except possibly for help in debugging it, with the output statements removed once it's working.

There are a number of things wrong with your code. Once you get something working, we can look at some more of the problems.

Last edited:
jtbell
Mentor
You can't declare num as you did; i.e., as int num[j]; To declare an array, its size must be known at compile time. Passing j as a parameter means that the size of the array isn't known until run time.
Here's another advantage to using vectors, in addition to the one I gave in my previous post. You can declare a vector with an initial size that is not known until run time:

Code:
vector<int> num(j);
If you omit the initial size, it defaults to zero as in my previous example.

Note you use parentheses rather than square brackets. This is because the size is actually the argument of a special function called the "constructor."

rcgldr
Homework Helper
You can't declare num as you did; i.e., as int num[j];
ISO C99 compliant compilers allow variable length arrays (VLA), C++ standards changed support for VLAs back to optional (depends on compiler). Microsoft C / C++ compilers don't support it (and never did), but _alloca() could be used for the equivalent:

Code:
int * num = _alloca(j * sizeof(int));
http://en.wikipedia.org/wiki/Variable-length_array

... sort ...
Take a look at the wiki example for bubble sort.

http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation

Note that swap in C is usually done using a temp variable:

Code:
    tmp = A[i-1];
A[i-1] = A[i];
A[i] = tmp;

jtbell
Mentor
C++ standards changed support for VLAs back to optional (depends on compiler).
C++ didn't have variable-length arrays until the C++14 standard. (I'm still not even up to speed with C++11! ) It looks like there are two flavors: runtime-sized arrays which are like C's VLAs; and std::dynarray which is a templated standard library container class like std::vector, std::list, etc.

http://blog.smartbear.com/development/a-glimpse-into-c14/

rcgldr
Homework Helper
C++ didn't have variable-length arrays until the C++14 standard.
GCC may have always had it as an option for C++ (I've never used GCC, but I see referenced a lot). Microsoft C, including Visual Studio versions up to 2015, is C89 or C90, not C99, so it never supported VLAs. Although Visual Studio C++ compilers are more up to date, for example lambda expressions / function were added with VS2010, VLAs continued to not be supported. VS2010 is the last version that can run on Win XP and/or Win XP X64.