Write a program that uses a for statement to sum a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value per input statement. A typical input sequence might be
5 100 200 300 400 500
where the 5 indicates that the subsequent 5 values are to be summed.
`

#include <iostream>
using namespace std;
int main()
{
    int x,sum=0,y,n;
    cout<<" Please enter your assumed integer: ";
    cin>>x;
    cout<<" Numbers: ";
    cin>>n;

    for (y=1;y<=x;y++)
    {

        sum=+n;
    }
    cout<<" Sum = "<<sum<<endl;
    return 0;
}

`

when I debug the program it repeats numbers word three times and I don't know what is the problem..

Recommended Answers

You need to move the prompt for Numbers (lines 8 and 9) to inside the y loop.
line 14 should be: sum += n;
After that you'll need to flush the input stream.

Jump to Post

All 2 Replies

You need to move the prompt for Numbers (lines 8 and 9) to inside the y loop.
line 14 should be: sum += n;
After that you'll need to flush the input stream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.