User inputs numbers into an array

I'm working on an assignment. A user inputs a bunch of numbers. "The number of integers input by the user is not known in advance, except that it will not exceed 100. Numbers are input through standard input, either from keyboard or file re-direct. The program should read numbers until a non-digit or end-of-file is encountered or 100 numbers have been read."

I figured I would make an array to hold the numbers and the user would fill it. But I don't know how to cancel out of it with 'x'. I want the user to input 'x' when they're done entering their numbers. I also have to calculate mean and median of the numbers, but I'm just trying to figure out how to get the numbers in the array in the first place. (I took an Intro to C++ course in fall 2013 and haven't done anything with it since, I'm sure I learned how to do something this simple.)


1
2
3
4
5
6
7
 int numbers[]={0};
	cout << "Enter integers('x' to stop";
	for(int i = 0; i < 100 || numbers[i] == x; x++)
	{
		cin >> numbers[i];
		
	}


int numbers[]={0}; Contains 1 element. Your 100 elements loop is disaster waiting to happen.


I want the user to input 'x' when they're done entering their numbers. Either get input into string, check it and convert to int if it is not x, or use the fact that attempt to read charactor into int sets stream failed state and loop on input;
1
2
3
4
5
6
7
8
const size_t max_size = 100;
int numbers[max_size] {};
std::cout << "Enter integers('x' to stop)\n";
size_t size = 0;
while(std::cin >> numbers[size++] && size < max_size)
    ; //Empty loop body
std::cin.clear(); //Clear failed state
std::cin.ignore(); //Discard first symbol in stream (our X) 
Thanks for the response. I was following a tutorial on youtube and roughly accomplished what I was trying to do. But when I just want to print the array on the screen, its also prints the leftover 0s. I used an int numOfElements to try to keep it from happening. Here's what I mean.

Type #s 'x' to stop: 2 3 4 5 x
2
3
4
5
0
0
0
0...and so on to 100 zeros.

It should just be
2
3
4
5




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

const int SIZE = 100;

main()
{
	int array[SIZE];
	int i = 0;
	int numOfElements = 0;
	int input = 0;
	
	cout << "Type #s. 'x' to stop: ";
	cin >> input;
	
	while((input != 'x') && (i < SIZE))
	{	numOfElements++;
		array[i] = input;
		i++;
		cin >> input;
		
	}
	for(int j = 0; j < numOfElements; j++)
	{
		cout << array[j] << endl;
	}

return 0;
}
It is not going to do what you think.

input != 'x' This is utterly wrong. You cannot store a letter in int. Your condition is actually parsed as input != 120 (Try it. Entering 120 should stop the loop and give you correct results).

What happens when you enter 'x' is that stream sees letter when it expects number and enters failed state. It sets variable which it was going to write to to 0 and stops without extracting offending letter. You increment numOfElements and try to read again. As stream in failed state it is not even attempting to read instead returning instantly. You still increment numOfElements and try again. And again until numOfElements hits SIZE.
Ah I see. 120 made it work... So the only way to do it that way ('x' to stop) is by either converting strings to int or by using cin.clear and cin.ignore? The reason I ask is we haven't gone over those ways yet in class.
Last edited on
If you will not ask user for any more input further in your program, then you can just drop clearing state and ignoring character. You need those only if you want to use stream again.
Otherwise yes, those are two reasonable ways to do this.
I tried it this way and it seems to work except it adds a 0 when I just try to print the numbers on screen. I'm sure its something wrong with my for loop.

I enter 2 3 4.
It prints.
2
3
4
0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;

const size_t max_size = 100;


main()
{
	int numbers[max_size] {};
	cout << "Enter integers('x' to stop)\n";
	size_t size = 0;
	while ((cin >> numbers[size++]) && (size < max_size));
	for(int i = 0; i < size; i ++)
	{
		cout << numbers[i] << endl;
	}
return 0;
}
Last edited on
I'm sure its something wrong with my for loop.
No, it is just size being one higher than it should, as it counts terminating 'x' as part of array. Either decrament it after loop or (better) move size increment into body of the loop:

1
2
while (size < max_size && cin >> numbers[size]) //Note switched order. It is important to not overrun array
    ++size; //Increment size if everything is okay and we did actually read number 

Thank you! You've been very helpful. One more question. I was messing with it and moved "cin >> numbers[size]" from the while condition and put it in the body like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const size_t max_size = 100;


main()
{
	int numbers[max_size] {};
	cout << "Enter integers('x' to stop)\n";
	size_t size = 0;
	while (size < max_size)
	{
		cin >> numbers[size];
		size++;
	}
	
	for(int i = 0; i < size; i ++)
	{
		cout << numbers[i] << endl;
	}
return 0;
}


When I ran the program it did
2
3
5
0
0
0
0
0......

Why didn't it do that before when cin >> numbers[size] was in the while condition?
Because it is not in condition anymore.
Why following not work:
1
2
3
if() {
    x < 4;
}


When input was in condition, we checked status of stream after each read and acted accordingly. If you want to move input from condition, you will need to check stream state manually:
1
2
3
4
5
6
7
while (size < max_size)
{
	cin >> numbers[size];
	if ( !cin )
		break;
	++size;
}

Topic archived. No new replies allowed.