loops and sums

Pages: 12
You must add semi colons at the end of every line except for the include file the int main and the while loop
closed account (oz80RXSz)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main()
{
int totalSum = 0;
int current = 1;

while (Sum!= 5000);
{

totalSum ++;

cout<<totalSum;
cout<<The last number;
}
endl;
}


so you are saying that this should work right?
no you should not have semi colons on the end of a while loop
closed account (oz80RXSz)
while (totalSum!= 5000) ?
I have to go and maybe I should have done this in the first place follow this link for a tutorial: http://www.cplusplus.com/doc/tutorial/program_structure/ if you have any more questions after that I will answer it

C'ya,
Dr.Chill
no just
while (totalSum!= 5000)
with no question mark
follow the link
closed account (oz80RXSz)
ok thanks for the link I checked it out but i still need some help.What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
  int sum=0;
  int a=1;
  while(sum>=5000);
  a+=a;
  a++;
  sum=a++;
  cin<<first_number;
  cout<<totalSum;
  cout<<The last number;
  return 0;
}
You should REMOVE the semicolon. That is, NOT like this...

while(sum>=5000);

but like this...

while(sum>=5000)

Having said that, I don't know what your situation is, but if you are going to continue with C/C++ programming, then you MUST buy a book as well as go through tutorials. This is VERY, VERY, VERY basic C/C++ stuff.

I would recommend, for starters, C++ Primer Plus, Fifth Edition, by Stephen Prata. It is an EXCELLENT book on C/C++.
closed account (oz80RXSz)
hmm..ok I will but iy but what else am i missing and this is the question

I need to write a C++ program which answers;

How far do you have to go in adding together the sequence
1+2+3+4+...
for the sum to exceed 5,000?

My program should keep adding together the above number sequence until the sum ets to be more than 5,000 and in the end it should print out the last number and the final sum
..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
	int iAdd=1, iCounter=0, iTotal=0;

	while(iTotal <= 5000)
	{
		iAdd += iAdd;
		iCounter++;

		iTotal += iAdd;
	}

	cout << "Number of increments: " << iCounter << endl
	<< "Number of iTotal: " << iTotal << endl
	<< "Number of iAdd: " << iAdd << endl;

	return 0;
}


The output is:

Number of increments: 12
Number of iTotal: 8190
Number of iAdd: 4096
closed account (oz80RXSz)
hmm..but still there is something wrong bc itotal should be couple more than 5000
Then you simply need to modify the iteration. There are three variables there for you to work with. You could create one or two more if you need them, although for the iteration you want the three should be enough. The above example is a model that you should be able to work with.
closed account (oz80RXSz)
line 11
iAdd += iAdd;
if you change that toiAdd +=1

thanx for the help
closed account (oz80RXSz)
it works
*sigh
thank you lamblion
Last edited on
ComputerGeek listen to DrChill: READ A TEXT BOOK!

a few notes on your last post

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
using namespace std;

int main ()
{
  int sum=0;
  int a=1;
  while(sum>=5000);
  a+=a;
  a++;
  sum=a++;
  cin<<first_number;
  cout<<totalSum;
  cout<<The last number;
  return 0;
} 


First of all your condition in the while loop:
while(sum>=5000);
the condition in the brackets will evaluate to false as you initialised sum as:
int sum=0;
The condition in your while loop is checking weather sum is bigger then 5000, as it is 0 the while loop will not run.

This brings me onto:
while(sum>=5000);
There should be no semicolon at the end of this statement, also if you want the loop to loop through more then one statement use {} to create blocks

like this:
1
2
3
4
5
6
7
8

 while(sum>=5000)
{
       a+=a;
       a++;
       sum=a++;
}


the next three statements are invalid.
1
2
3
  cin<<first_number;
  cout<<totalSum;
  cout<<The last number;

first_number needs to be declared before it can be used. << opperator is wrongly used with cin.
total sum has not been declared and so cannot be used.
The last number is not a valid identifier for a variable. If you want to print out the text enclose it in " "

PLEASE!, read the basic language tutorial on this website, or in the book DrChill mentioned.
Last edited on
closed account (oz80RXSz)
ok thank you very much for all of you. I have started reading that believe me and now I feel better in C++
:D come back with questions
Topic archived. No new replies allowed.
Pages: 12