// progtam to input some integer values and get their sum.
// I want the program to exit and show result once return key is pressed
#include <iostream>
using namespace std;
int main() {
int a[1000]; // Declare an array of 1000 ints
int n = 0; // Number of values in a.
int sum =0;
cout << " Input some positive values (for exit input a character) : " << endl;
while (cin >> a[n]) {
sum = sum + a[n];
n++;
}
cout << "The sum of " << n << " numbers is : " << sum << endl;
Firstly,
"Requesting modification to my program"
It's not hard to type coherently.
Secondly, use code tags!
Thirdly, it isn't hard. If you bother to read the pinned topic in the "Beginners" section; you will find that it is fairly easy to do so.
Finally, don't put your question as comments. It's annoying, isn't funny, and makes it harder to read.
Firstly,
"Requesting modification to my program"
It's not hard to type coherently.
Secondly, use code tags!
Thirdly, it isn't hard. If you bother to read the pinned topic in the "Beginners" section; you will find that it is fairly easy to do so.
Finally, don't put your question as comments. It's annoying, isn't funny, and makes it harder to read.
@chrisname: Please don't say "It isn't hard". Programming can be a very tedious process for some.
As for some advice. I suggest you use a for loop to get the user input if you are storing the values into an array. Also, entering a character to exit will crash your program because you are reading it into an array of integers. I would suggest using zero as a terminator.
Example using no arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int sum = 0;
int value;
while(true)
{
cout << "Input a value (0 to sum) : ";
cin >> value;
if(value == 0)
break;
sum += value;
}
cout << "sum = " << sum << endl;