0

In need some homework help again. Please I am new at this and PLEASE PLEASE do not post YELLING post at me for wrapping the code because I don't know what that is. If you would like to explain it to me in plain english I am willing to learn.

Here goes I have to write a program taht uses a for structure to sum a sequence of integers. Assume that the first interger read specifies the number of values remaining to be entered. Here is what I came up with. How do I have the user of the program enter the number of values before the loop. Please help TY Jessie

#include<iostream>
using namespace std;
int main()
{
int i,s=0,x;
for(i=0;i<5;i++)
{
cout<<"Type an integer: ";cin>>x;
s=s+x;
}
cout<<"The sum is : "<<s<<endl;
return 0;
}

4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by ~s.o.s~
0

In need some homework help again. Please I am new at this and PLEASE PLEASE do not post YELLING post at me for wrapping the code because I don't know what that is. If you would like to explain it to me in plain english I am willing to learn.

Here goes I have to write a program taht uses a for structure to sum a sequence of integers. Assume that the first interger read specifies the number of values remaining to be entered. Here is what I came up with. How do I have the user of the program enter the number of values before the loop? Please help TY Jessie

#include<iostream>
using namespace std;
int main()
{
int i,s=0,x;
for(i=0;i<5;i++)
{
cout<<"Type an integer: ";cin>>x;
s=s+x;
}
cout<<"The sum is : "<<s<<endl;
return 0;
}

Thanks

0

>> and PLEASE PLEASE do not post YELLING post at me for wrapping the code because I don't know what that is

Ok then you need to learn. See the links in my signature which will show you how to do it.

>>a for structure
there is no such thing. What you mean is a for loop. A structure is an entirely different thing.

>>How do I have the user of the program enter the number of values before the loop
just put a prompt above that loop and ask how many integers to you want to enter.

>>for(i=0;i<5;i++)
replace the value '5' with x, which was entered from the above prompt.

-1

This is one way of handling it. Study it and you'll get your head around it. Hope it helps byeee

#include<iostream>

using namespace std;

int main()
{
    int input;
    int i;
    int value;
    int sum=0; // must be initialized with 0

    cout << "How many numbers to enter?: ";
    cin >> input;

    for (i=1; i<=input; i++)
    {
        cout << "Enter number " << i << ": ";
        cin >> value;
        sum += value;
    }

    cout << "Sum is: " << sum << endl;
    return 0;
}
Comments
Giving answers again
0

Ask from the user the number of values that he would want to sum up.
Something like:

int max_values = 0 ;
cout << "Enter the max values you want: " ;
cin >> max_values ;

for( i = 0; i < max_values; ++i )
{
   // do all here
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.