Numbers in a Series

C++ program that sums up the integers in a range of values and prints the result. The computation will be done using two different methods: a while loop approach and a for loop approach. Prompt the user for the starting integer value, the ending integer value, and the integer increment between the two values, and read in these values. Print the sum for the while loop approach and the for loop approach.

Ok those are the directions, and with that I have this so far...



#include <iostream>
#include <stdlib.h>

using namespace std;


// ############################################################
int main(void)
{

int while_sum;
int for_sum;


cout << "First value: ";
int n1;
cin >> n1;

cout << "Ending value: ";
int n2;
cin >> n2;

cout << "Increment value: ";
int n3;
cin >> n3;
if (n1 > n2)
cout << "Error message";

return 0;

if( n3 <= 0)
cout << "Error message";

return 0;

while_sum = 0;
for (

int sum=0;
int c=n1;
while_sum(c < n2)
{
sum+=c;
c+=n3;
}




return 0;
} // End main


Is there anything that I am missing? The steps are here
1.Declare the variables that you will need for this program. When doing so, declare a variable to hold the sum from the while loop approach and a variable to hold the sum from the for loop approach
2.Prompt the user for three integer values, a starting value, an ending value, and an increment value, and read in these values
3.If the starting value is greater than the ending value, print an error message followed by return 0;, which causes the main function to end and the program to terminate
4.If the increment value is less than or equal to zero, print an error message followed by return 0;, which causes the main function to end and the program to terminate
5.Set the sum of the while loop approach to zero (This is analogous to pushing Clear on a calculator)
6.Beginning with the starting value, implement an algorithm that uses a while loop to add up the numbers in the given range and increment
7.Print the sum that results from using the while loop approach
8.Set the sum of the for loop approach to zero
9.Beginning with the starting value, implement an algorithm that uses a for loop to add up the numbers in the given range and increment
10.Print the sum that results from using the for loop approach. (As a way of testing your two implementations, the two sums should be the same


I have done all up to 6, and that's where I'm stuck. Is my while loop formed correctly?
1
2
3
4
if(n1 > n2)
  cout << "Error message.";

return 0; // this statement will execute regardless of the if statement above 


vs

1
2
3
4
5
if(n1 > n2)
{
  cout << "Error message.";
  return 0; // this statement will only execute if n1 > n2
}
for(int i = 0; i < 5; i++) // start at 0, increment i till 4 the drops out of for loop

which is similar to this while:

1
2
int i = 0; // start at 0
while(i < 5) { i++; } // increment i till 4, then drops out of while loop 
Hello. I have added your suggestion to the for loop. But I ran the program but I'm still not getting corrects values. I don't think the program is doing what the program is asking. Am i doing something wrong?


int main(void)
{

int while_sum;
int for_sum;


cout << "Enter a starting integer value: ";
int n1;
cin >> n1;

cout << "Enter an ending integer value: ";
int n2;
cin >> n2;

cout << "Enter a positive Increment value: ";
int n3;
cin >> n3;
cout<< "Sum (while loop)";

if (n1 > n2)
cout << "Error message";
return 0;

if( n3 <= 0)
cout << "Error message";

return 0;

while_sum = 0;
for(int i = 0; i < 5; i++) // start at 0, increment i till 4 the drops out of for loop
cout<< "Sum (for loop)";

int sum=0;
int c=n1;
int(c < n2);
{
for_sum+=c;
c+=n3;
}

return 0;
} // End main
As Texan said in his/her post, when your program hits your first "return 0;" your program will end. It looks like you still haven't put it in braces so as to include it in your error message if statements, so it will always exit.

(could you use code tags as well please, ta)
Hello, could you show me where I need to put the brackets, because I have put them in various places and I still get the error messages.

int main(void)
{

int while_sum;
int for_sum;


cout << "Enter a starting integer value: ";
int n1;
cin >> n1;

cout << "Enter an ending integer value: ";
int n2;
cin >> n2;

cout << "Enter a positive Increment value: ";
int n3;
cin >> n3;
cout<< "Sum (while loop)";

if (n1 > n2)
cout << "Error message";
return 0;
{
}

if( n3 <= 0)
cout << "Error message";




while_sum = 0;
for(int i = 0; i < 5; i++) // start at 0, increment i till 4 the drops out of for loop
cout<< "Sum (for loop)";

int sum=0;
int c=n1;
int(c < n2);
{
for_sum+=c;
c+=n3;
}

return 0;
} // End main
while_sum = 0;
This seems to be your problem. Are you trying to use a while loop here or is while_sum a variable?

int while_sum = 0; //try combining it if it's a variable.


if works like this...

if( n3 <= 0)
{
cout << "Error message";
}

int main(void) //I haven't seen this before.
int main(){

}//is better.
Last edited on
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
int main()
{

int while_sum = 0;
int for_sum = 0;
int n1;
int n2;
int n3;

cout << "Enter a starting integer value: ";

cin >> n1;

cout << "Enter an ending integer value: ";

cin >> n2;

cout << "Enter a positive Increment value: ";

cin >> n3;
//cout<< "Sum (while loop)";

if (n1 > n2){
cout << "Error message";
}

if( n3 <= 0){
cout << "Error message";
}

for(int i = 0; i < 5; i++) // start at 0, increment i till 4 the drops out of for loop 
{
cout<< "Sum (for loop)";
}
//int sum=0; 
//int c=n1; 
//int(c < n2);
//{ 
//for_sum+=c; 
//c+=n3; 
//} 

return 0;
} // End main

This should take out your errors... but your algorithm needs work. Give it a try and i'll check back later.
It works, but now I'm not getting the correct values, like I don't get values for the sum(while loop) nor the sum of the for loop. Is there a way to get those values?
yes there is, you can easily iterate a computation within the for loop and cout<< "the results" << sum << endl;


so it's whatever you want to display
cout << n1 << endl;

for(int i = 0; i < 5; i++) // start at 0, increment i till 4 the drops out of for loop
{
cout<< "Sum (for loop)" << n1 << endl;;
}

try the different objects and print them out.
you are so close to the answer.
I just realized what your program is suppose to do... lol.. it's basically a summation formula..
It's getting code to do this...
http://www.psychstat.missouristate.edu/introbook/sbk12m.htm

so let's see..

input 3 variables...
then ...
first value initialize...
end value goes into the for loop...
i guess then multiply or add by increment value?

okay, I am going to work based on these assumptions.
I think this is what you are looking for... if you have sample outputs I can trouble shoot the algorithm...


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

int main()
{

	int while_sum = 0;
	int for_sum = 0;
	int n1;
	int n2;
	int n3;

	cout << "Enter a starting integer value: ";

	cin >> n1;

	cout << "Enter an ending integer value: ";

	cin >> n2;

	cout << "Enter a positive Increment value: ";

	cin >> n3;
	//cout<< "Sum (while loop)";

	if (n1 > n2){
		cout << "Error message";
	}

	if (n3 <= 0){
		cout << "Error message";
	}
	
	for (int i = 0; i < n3; i++) // start at 0, increment i till 4 the drops out of for loop 
	{
		//cout << "Sum (for loop)" << i << endl;
		cout << "Sum (for loop)" << n1 << endl;
		n1++;
	}
	//int sum=0; 
	//int c=n1; 
	//int(c < n2);
	//{ 
	//for_sum+=c; 
	//c+=n3; 
	//} 

	return 0;
} // End main
I see, but when i go compile it, I still don't get the correct values after the for and while loops. here is an example of the values
Enter a starting integer value: 8
Enter an ending integer value : 121

Enter a positive increment: 17

Sum (using a while loop): 413

Sum (using a for loop): 413
I am getting 163 with the for loop. How would you do this calculation with a calculator?
Topic archived. No new replies allowed.