A simple program however doubts on it

Hello mates. i would like to see your recommendations and your quotes.

i don't know if i understand the set out of the problem, can you verify it? please

how can i optimize this code?
what is wrong into the code?
what can it be improved?

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
35
36
37
38
39
40
41
42
43
  //exercise 5.5; control instructions 2
//Luis Fernando 
//18/09/2018

/*Write a program that uses an instruction for to add up a sequence of integers. Suppose that the first integer read specifies the number of values that miss for introducing. Your program must read only a value for every instruction
Of entry. A typical sequence of entry might be
5 100 200 300 400 500
Where 5 indicates that they are going to add up 5 subsequent values.
*/

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main(int argc, char** argv)
{
	int number;
	int numberSum;
	int storeTotalSum = 0;
	
	cout << "Join the numbers to add up " ;
	cin >> number;
	
	
	
	for(int counter = 0; counter < number; counter++)
	{
		
		cout << "add a number to add up: ";
		cin >> numberSum;
		
		storeTotalSum += numberSum;
		
	}
	
	cout << "it´s going to add up " << number << " integers." << endl;
	cout << "the sum is: " << storeTotalSum << endl;
	
	
	return 0;
}
Last edited on
This looks like the standard pattern for this kind of homework. The only thing I would not do is continue to ask for input (line 31); the user already knows the number of integers he wishes to input, but it doesn't really matter either way.

Also, be careful when you are talking to the user:

1
2
3
  cout << "How many numbers do you wish to add? ";
--or--
  cout << "¿Cuántos números deseas sumar? ";

Likewise, tell the user what to do:

1
2
3
  cout << "Enter a number: ";
--or--
  cout << "Ingrese un número: ";

Don't tell things that happened out of chronological order. At the end of the program the numbers have already been added:

1
2
3
  cout << number << " integers were added.\n";
--or--
  cout << "Se sumaron " << number << " integeres.\n";


Hope this helps.
muchas gracias compañero. thanks mate
Topic archived. No new replies allowed.