Adding with a while loop

adding a series of keyboard inputs using a while loop and terminating

Page 1 of 1

3 Replies - 13390 Views - Last Post: 08 September 2007 - 07:15 AM Rate Topic: -----

#1 arogers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-September 07

Adding with a while loop

Post icon  Posted 06 September 2007 - 12:05 PM

ok I have look for topics covering this and have not found any. I am having a problem with this. basically the program is set up to take what eer non-decimal number a user inputs from the keyboard and add it to the next one and repeatedly ask for another number until the user inputs -9999 at that point it's sapose to display the total of all the added numbers. Only what I keep getting is the numbers being entered are added to the terminating number. My question is why? what did i miss that is causing this? here's the code I have so far.

#include<iostream>
using namespace std;
void main()
{
	int numb(0),sum(0);
	cout<<"Pleas enter a non-decimal number.\n";
	cin>>numb;
	while(numb!=-9999)
	{
	   cout<<"Pleas enter a non-decimal number.\n";
		cin>>numb;
		sum+=numb;
	}
	cout<<"The sum is "<<sum<<endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Adding with a while loop

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Adding with a while loop

Posted 06 September 2007 - 01:17 PM

All you need to do is just rearrange the statements in your loop. Do the adding before asking for another number...

while(numb!=-9999)
{
	// Add the number, then prompt for another.
	sum+=numb;
	cout<<"Please enter a non-decimal number.\n";
	cin>>numb;

}



After you prompt, you will check it and if it is bad, it won't continue, giving you the result of an appropriate sum.

:)

Note: You might want to add an "e" to please and also mention to the user that -9999 terminates the number prompting (if this is going to be given to someone else or an assignment)

This post has been edited by Martyr2: 06 September 2007 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 arogers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-September 07

Re: Adding with a while loop

Posted 07 September 2007 - 09:14 AM

Thnk you for your help. that was confusing me and it never even dawned on me to put the sum+=numb before the prompt asking for another input.
:)

View PostMartyr2, on 6 Sep, 2007 - 01:17 PM, said:

All you need to do is just rearrange the statements in your loop. Do the adding before asking for another number...

while(numb!=-9999)
{
	// Add the number, then prompt for another.
	sum+=numb;
	cout<<"Please enter a non-decimal number.\n";
	cin>>numb;

}



After you prompt, you will check it and if it is bad, it won't continue, giving you the result of an appropriate sum.

:)

Note: You might want to add an "e" to please and also mention to the user that -9999 terminates the number prompting (if this is going to be given to someone else or an assignment)

Was This Post Helpful? 0
  • +
  • -

#4 dude_55_killua  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-September 07

Re: Adding with a while loop

Posted 08 September 2007 - 07:15 AM

hmmm... I'm only starting in programming!
I just want to know how to make a program in turbo c!
Our teacher in fundamentals gave a project and this project is all about conversion! She said we should use the turbo c!
My question is how to convert decimal to octal using turbo c?

I just only knew few about the c language!
here's what I got so far....

I have a question I know how to compute the remainder but I don't know how to bring back it again to the statement?for example:

for(x=1;x<=n;x++)-- this statement is an example of iterative statement
n=n%8;--- because it will be converted to octal!
but I don't know what's the next step...
for example convert 20 (base 10) to octal(base 8).

I just really need your reply as soon as possible because our project wiil be pass on tuesday (september 11,2007). Thanks for consideration
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1