declaring unknown amount of variables

I help to understand how to declare variables for an unknown amount of inputs and displaying "Enter an integer" for each of the unknown amounts of variables. Is there a way to do this?
"How many integers would you like to sum?" x

"Enter an integer:"

I haven't figure out how to accomplish this without knowing how many the user will input. For example what if they want to sum 50 or 100 different integers. Declaring individual variables for each isn't reasonable.


Re-use the same variable!
Specifically, use two variables. One to keep track of the number of integers.
A second one to keep track of the sum of all integers entered by the user so far.
There is a more than an insignificant difference between the number of variables and the amount of variables just as there is a more than insignificant difference in the number of variables and the amount of variables.

Those differences will have a substantial impact on how you understand the problem and proceed to writing a successful solution.
For example what if they want to sum 50 or 100 different integers.


You use a loop with a terminating condition. This could be a sentinel value (ie -999), or a specified number (eg enter 10 numbers) or other condition (eg enter <CR> to end).

This example sums entered numbers until just a CR is entered. Note no error checking.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <cstdlib>

int main() {
	int num {};
	int sum {};

	for (std::string line; (std::cout << "Enter number <CR> to end: ") && std::getline(std::cin, line) && !line.empty(); sum += std::strtol(line.c_str(), nullptr, 10));

	std::cout << "Sum is: " << sum;
}

declaring unknown amount of variables

Entering and summing a user specified number of numbers is not creating a user specified number of variables.

Creating a user specified number of variables could easily be handled with a C++ container. A std::vector would be a good choice. The size is flexible.
http://www.cplusplus.com/reference/vector/vector/

As others have pointed out what you actually ask is how to keep track of entering a varying number of inputs. Three variables would work. One for the number of inputs. The second to get each input from the user in a loop; lather, rinse and repeat. The third to hold the sum of all the individual inputs.

There are two basic strategies for how to get the input from the user.

1. ask the user how many integers they want to enter and loop input for that number. Rather inflexible IMO. What if the user entered an incorrect amount?

2. In a loop ask the user to enter an integer, specifying a terminating condition (-9999 is a valid and reasonable terminating number). Add the input to the summing variable, increment a count variable by one to keep track of each input.

Exactly how you write the code is something you should tackle, after all this is your assignment. We can offer help with code you've written, we won't do the assignment for you.
 
for (std::string line; (std::cout << "Enter number <CR> to end: ") && std::getline(std::cin, line) && !line.empty(); sum += std::strtol(line.c_str(), nullptr, 10));
wtf?
Last edited on
Let's keep it simple:

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

int main()
{
   int N;
   double sum = 0.0, number;
   cout << "How many numbers do you want to sum? ";   cin >> N;
   while ( N-- )
   {
      cout << "Enter a number: ";   cin >> number;
      sum += number;
   }
   cout << "Total = " << sum << '\n';
}
I help to understand how to declare variables for an unknown amount of inputs and displaying "Enter an integer" for each of the unknown amounts of variables. Is there a way to do this?

There are essentially three aspects.

1. When you have many values of same type it is more convenient to have an array than to declare distict variable for each value.

2. When the amount is not known during writing/compiling the code, then one has to dynamically allocate memory when the program runs.

The std::vector is an array that allocates memory dynamically.

3. The input data is not always the data that you need to store. In your case you only need the sum, not the raw input values.
Last edited on
Registered users can post here. Sign in or register to post.