Need help with a coding for class please!

I am trying to make a program that calculates the sum of every Nth integer from a given start and stopping point. I am having trouble towards the very end with my while/do statement and I am having a hell of a time doing so. Would any of you be so kind as to point me in the direction of my mistake? Thanks in advance!

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
  #include <iostream> 
using namespace std;

int N, start, stop, counter, sum;


int main()
{
	// Welcome statement//

	cout << "Welcome, this program calculates the sum of every Nth integer from a given starting integer to a given stopping integer. \n\n";

	//Input of values//

	cout << "Please enter the value of N now..... ";
	    cin >> N;
	cout << "Please enter the value of start now..... ";
	    cin >> start;
	cout << "Please enter the value of stop now..... ";
	    cin >> stop;

	// If and else statements//

	if (N <= 0)
		cout << endl << "Error, input value N must be greater than 0. " << endl << endl;
	else if (start >= stop)
		cout << endl << "Error, input value of start must be less than the input of stop." << endl << endl;

	//else if ((start-stop) != N)
	//cout << Error, the difference between start and stop must be a multiple of interger N

	else (counter = start);
		(sum = 0);


*This statement is my problem:*

		while (counter <= stop) do
		    sum = (counter + sum)
		    //counter += N;
		    cout << sum;
It's just while (/*condition*/){ /*loop body*/ }. Drop the do.

Also, on line 32: else can't be followed by a condition. And the semicolon at the end shouldn't be there.
Last edited on
If you want a do/while loop though it would be like

1
2
3
4
do
{
     sum += counter;
} while(counter <= stop);
By the way
1
2
	else (counter = start);
		(sum = 0);
this is the same as
1
2
3
else
    counter = start;
sum = 0; //outside of the else 
are you sure that is what you want? The indentation says otherwise. Though if you were trying to compare it would be == and would need to be an else if.

PS your loop logic is wrong.
1
2
		while (counter <= stop) do
		    sum = (counter + sum)
when will it end? You never increment counter. I would just use a for loop for this or summation formula. Though you could just add an incremental statement in the while loop also. Again, I would highly suggest using summation instead of a loop.
Last edited on
Thank you guys! I think I got it figured out.

Giblit I was incrementing counter it was just in the comment that I had not taken out yet, and I am unsure. I'm not entirely sure what a summation statement is unless I know it by another name or don't know that name at all. The last part I have to figure out now is how to make the error message on line 29-30 work, but I am trying to figure that out now.

Thank you guys for your help!
Summation is a mathematical equation. http://en.wikipedia.org/wiki/Summation

Your program is really the sum of [output]start + Ni[output]

As far as your second problem with line 29-30 shouldn't it be something like
1
2
if(end - start < N)
    std::cerr << "Error - The difference between start and end must be greater than " << N << std::endl;

Though technically then the sum would be 0?
Last edited on
Okay so I figured out I needed to use modulus to get multiples of "N". And it compiles and runs the only problem that I have now is that if an error statement pops up it still solves the problem and shows the number any insight?


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
44
45
46
47
48
49
50
51

#include <iostream> 
using namespace std;

int N, start, stop, counter, sum;


int main()
{
	// Welcome statement//

	cout << "Welcome, this program calculates the sum of every Nth integer from a "
		"given starting integer to a given stopping integer. \n\n";

	//Input of values//

	cout << "Please enter the value of N now..... ";
		cin >> N;
	cout << "Please enter the value of start now..... ";
		cin >> start;
	cout << "Please enter the value of stop now..... ";
		cin >> stop;

	// If and else statements//

	if (N <= 0)
		cout << endl << "Error, input value N must be greater than 0. " << endl << endl;
	else if (start >= stop)
		cout << endl << "Error, input value of start must be less than the input of stop." << endl << endl;
	else if ((start - stop) % N != 0)
		cout << "Error, the difference between start and stop must be a multiple of integer N.";

	//Final Else statement and while loop//

	else
		(counter = start);
		sum = 0;

		while (counter <= stop)
		{
			sum = (counter + sum);
			counter += N; 
		}

	//Print Sum//

		cout << endl << endl << sum << endl << endl;

		return  0;
	
}
Last edited on
if you want more than one statement you need a block of code with { and }
1
2
3
4
5
6
7
8
9
10
11
12
13
	else
		(counter = start);
		sum = 0;

		while (counter <= stop)
		{
			sum = (counter + sum);
			counter += N; 
		}

	//Print Sum//

		cout << endl << endl << sum << endl << endl;
should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	else
        {
		(counter = start);
		sum = 0;

		while (counter <= stop)
		{
			sum = (counter + sum);
			counter += N; 
		}

	//Print Sum//

		cout << endl << endl << sum << endl << endl;
        }
Last edited on
I just figured it out! Thanks for confirming! Thank you for all your help too!
Not sure if you wanted to execute a dowhile or while but just drop the do, and while is supposed to be written with a body (parentheses):

while (counter <= stop)
{
sum = (counter + sum);
counter += N;
}
Topic archived. No new replies allowed.