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!
#include <iostream>
usingnamespace 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;
elseif (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;
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.
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.
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?
#include <iostream>
usingnamespace 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;
elseif (start >= stop)
cout << endl << "Error, input value of start must be less than the input of stop." << endl << endl;
elseif ((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;
}