Create a program that inputs up to 100 integers

hey guys, i could use your insight here. I got this assignment to do -

"Create a program that inputs up to 100 integers (space separated!) and outputs their sum."

I know i can create a loop to ask for each number and place it in an array, but i was wondering how i can ask for 1 input and do this?

program should look like this -

Enter some numbers: 3 4 17 6
The total is: 30
closed account (48T7M4Gy)
Best insight is to start analyzing the problem and perhaps begin by writing some pseudocode. Once you have something towards developing your solution there are plenty of us to give you a helping hand. The worst insight is to ask us to do it for you. :)
The trick here is to enter the line of numbers as a string, and then use a stringstream to extract them:
1
2
3
4
5
6
7
8
    string line;
    cout << "Enter some numbers: ";
    getline(cin, line);
    stringstream ss(line);

    int num;
    while (ss >> num) {
        ...

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
30
31
32
33
34
//Author - Dav Bam , DATE, Dev-c++ 5.11, MODDIFICAION HISTORY
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <strings.h>
#include <string>
//add other needful include files
using namespace std;
//start coding from here
int main(int nNumberofArgs, char* pszArgs[])
{
	int array[100];
	int accumulator = 0;
	int i;
	cout<<"Enter a set of numbers: \n";
	for(i = 0;i<100;i++)
	{
		cin>>array[i];
		if (array[i] == 0)
		{
			//terminate the set by entering 0
			//except u want to input 100 integers... lol
			break;
		}
	}
	for(int j = 0; j<i; j++)
	{
		accumulator += array[j];
	}
	cout<<"The sum is: "<<accumulator<<endl;
	cin.get();
	return 0;
	
}


will this work?? The only difference is, this approach will list the numbers in a column instead of a row. Gurus in the house pls check this out!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
    int sum = 0;
    int number = 0;

    std::cout << "Enter some numbers, space-separated (Non-number to quit): ";
    for(int i=0; i < 100; i++) // loop goes around maximum 100 times
    {
        cin >> number;
        if( std::cin.fail() ) break; // if non-number entered, break out of loop
        sum += number;
    }

    std::cout << "Sum: " << sum << std::endl;



Enter some numbers, space-separated (Non-number to quit): 3 4 17 6 a
Sum: 30

Process returned 0 (0x0)   execution time : 6.073 s
Press any key to continue.

Last edited on
closed account (48T7M4Gy)
Appears that worst insights prevail.

Well done though. OP now has to pick which one - they all have their merits, advantages and disadvantages, especially for a beginner not across all the technologies involved, but that's a learning opportunity.

For DavBam a way to maintain rows is by the dhayden stringstream approach. The columns are there in yours because enter produces a linefeed.
> I know i can create a loop to ask for each number and place it in an array,

To find the sum of the integers, we do not need to store them in an array - we can update a running sum as the each numbers is read.


> but i was wondering how i can ask for 1 input and do this?

By default, formatted input skips white-space characters.
We can read several white-space separated integers from a single line of input.

Slightly modified version of Arslan7041's snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    constexpr int MAX_SZ = 100 ; // up to 100 integers
    int size = 0 ; // number of integers entered by the user
    long long sum = 0 ; // sum of many integers may be beyond what single int can hold.

    std::cout << "enter some integers separated by white-space (enter a non-number to end input):\n";

    for( int number = 0 ; size < MAX_SZ && std::cin >> number ; ++size ) sum += number ;
    // note: ( std::cin >> number ) would evaluate to false if attempted input fails - 
    // for instance, when a non-numeric, non-whitespace character is encountered

    std::cout << "sum of " << size << " numbers: " << sum << '\n' ;
}
^@Jlborges, Ah yes, I feel like a noob for not doing that: putting the cin statement in the loop header. It would remove the need for the cin.fail() check.

Topic archived. No new replies allowed.