| laiwilliam (5) | |
|
I am trying to display num + num + num = sum for example: Enter size: 3 first number 1 second number 2 third number 3 The sum of: 1 + 2 + 3 = 6. <- trying to do that. so far my code is: #include <iostream> using namespace std; void main() { int size; cout << "Enter amount of size: "; cin >> size; int count = 0; int sum = 0; int x; while(count < size){ count+=1; cout << + "Enter number: "; cin >> x; sum+= x; } cout << "The sum is = " << sum; } | |
|
Last edited on
|
|
| chwsks (304) | |||
|
here is an an example using an array. What is your question? your code is not using an array. Note this example is C++11.
| |||
|
|
|||
| greenleaf800073 (84) | |||
You could just do this;
| |||
|
|
|||
| laiwilliam (5) | |
| im just trying to make it show num + num + num..etc = sum at the last line. Maybe I don't need to use an array then, what other way i could set it up to show the listed integers? | |
|
|
|
| chwsks (304) | |
|
@greenleaf800073 That is a really bad example. You are using a variable length array. If it works--it works because you are lucky. | |
|
|
|
| Aaron Vienneau (7) | |||
I feel like I'm oversimplifying this, which I probably am...But the request is simple, right?
| |||
|
Last edited on
|
|||
| chwsks (304) | |||
|
@laiwilliam read about vectors they are a great method for storing values. here is an example using a vector
| |||
|
Last edited on
|
|||
| laiwilliam (5) | |
|
i need it to be infinite that is why i have size. | |
|
|
|
| chwsks (304) | |
|
vectors are dynamic arrays -- you need to read about them; this site has a great reference. | |
|
|
|
| laiwilliam (5) | |
|
is there any way to list the numbers with the code I have? Not changing my whole code, just modify? | |
|
|
|
| chwsks (304) | |
|
you have to store the values that are given in a container. A vector is a dynamic array i.e. it can change its size to accommodation the number a variables you give it, using push_back (see example above). there are multiple ways to print out those variables using a loop. yes, you can use your code--but you are going to have to store those variables in an array--a dynamic array is probably your best choice. | |
|
Last edited on
|
|
| Aaron Vienneau (7) | |
|
Ah, I see. Yeah, so in order for it to be an indefinite size -- you would have to use some sort of "changing" or "dynamic" (if you will) data structure. Because, something has to store all the numbers that have been input, so you can output them at the end of the program: num + num + num + num = sum 1) Ask user to input a number 2) User inputs number 3) Store that number in a [dynamic] array. 4) Ask user for another number 5) User inputs number 6) Store that number in a [dynamic] array. 7) Ask user for another number 8) User Inputs number 9) Store that number in a [dynamic] array. . . . 10) Output each element of the array seperated by a " + " (Loop) 11) Sum all the elements of the array (Loop) 12) output the sum | |
|
|
|
| laiwilliam (5) | |
|
how do you program the loop? | |
|
|
|
| chwsks (304) | |||
simple example:
| |||
|
|
|||
| greenleaf800073 (84) | |
|
I like using arrays. It makes sense to me more. | |
|
|
|