req modification to my pog

// 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;

return 0;
}
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;


This is just laziness though. He is asking someone to do modify some code for him.
Why not ask for help with doing it instead?

Oh and I think "Press the escape key to blah blah blah" is usually better.
The escape key is 0x1B. So if you do this:
1
2
3
4
5
if (myChar == 0x1B) { // Escape key pressed
    break;
} else {
    continue;
}
Last edited on
Topic archived. No new replies allowed.