### Need help on a simple program

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
closed account (iw0XoG1T)
here is an an example using an array.

Note this example is C++11.
 123456789101112131415161718 #include #include #include int main( int argc, char ** argv) { std::array arry; for( auto& i : arry) { std::cout << "enter a value: " ; std::cin >> i; } std::cout << "here is the sum: " << std::accumulate(arry.begin(), arry.end(), 0); return 0; }
You could just do this;

 1234567891011121314151617181920212223242526272829303132333435363738 #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { int size = 0; int total = 0; int adder; cout << "This is a addition program. Enter array size: "; cin >> size; int array[size]; for(int relay = 0;relay < size; relay++) { cout << "Enter number: "; cin >> adder; array[relay] = adder; total = total + adder; } for(int relay2 = 0; relay2 < size; relay2++) { cout << array[relay2]; if(relay2 + 1 != size) { cout << " + "; } } cout << " = " << total << endl; system("PAUSE"); return 0; }
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?
closed account (iw0XoG1T)
@greenleaf800073

That is a really bad example. You are using a variable length array. If it works--it works because you are lucky.
I feel like I'm oversimplifying this, which I probably am...But the request is simple, right?

 123456789101112131415161718192021 #include using namespace std; int main() { int a = 0; int b = 0; int c = 0; cout << "Please enter your first value: "; cin >> a; cout << "Please enter your second value: "; cin >> b; cout << "Please enter your last value: "; cin >> c; cout << "\n" << a << " + " << b << " + " << c << " = " << ( a + b + c ); cout << endl; return 0; }
Last edited on
closed account (iw0XoG1T)
@laiwilliam

here is an example using a vector

 123456789101112131415161718192021222324 #include #include #include int main( int argc, char ** argv) { std::vector v; std::cout << "enter number of values\n" ; int num(0); int val(0); std::cin >> num ; for ( int i = 0 ; i < num ; ++i) { std::cout << "enter a value: "; std::cin >> val ; v.push_back(val); } for( auto i = v.begin(); i != v.end() - 1; ++ i ) std::cout << *i << '+'; std::cout << v.back() << " = " << std::accumulate(v.begin(), v.end(), 0 ) << std::endl; return 0; }
Last edited on
i need it to be infinite that is why i have size.
closed account (iw0XoG1T)

this site has a great reference.
is there any way to list the numbers with the code I have? Not changing my whole code, just modify?
closed account (iw0XoG1T)
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
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
how do you program the loop?
closed account (iw0XoG1T)
simple example:

 123456789101112131415 #include #include int main( int argc, char ** argv) { std::vector v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); for ( size_t i = 0 ; i < v.size(); ++i) // loop example std::cout << v[i] << " + " ; return 0; }
I like using arrays.

It makes sense to me more.
Topic archived. No new replies allowed.