Connecting Tech Pros Worldwide Help | Site Map

Suppose this might be a noob question:

Newbie
 
Join Date: Sep 2009
Posts: 2
#1: Sep 20 '09
Given that two int variables, total and amount , have been declared, write a sequence of statements that:
initializes total to 0
reads three values into amount , one at a time.

After each value is read in to amount , it is added to the value in total (that is, total is incremented by the value in amount ).



__

I have no idea what I'm doing wrong and other options I could do
this is what i have:

total = 0;
amount = 0;

while (amount <3)
{
cin >> amount;
total += amount;
}
Newbie
 
Join Date: Jul 2009
Location: delhi, india
Posts: 16
#2: Sep 20 '09

re: Suppose this might be a noob question:


hi

Expand|Select|Wrap|Line Numbers
  1. ****spoonfeeding removed******
  2. Describe your solution but just don't code it up.
  3.      ---weaknessforcats       
  4.  
regards
Vipin Sharma
Member
 
Join Date: Aug 2008
Posts: 121
#3: Sep 20 '09

re: Suppose this might be a noob question:


You increment total but not amount.. .so the while loop keeps on executing for ever...
Newbie
 
Join Date: Jul 2009
Location: delhi, india
Posts: 16
#4: Sep 20 '09

re: Suppose this might be a noob question:


hi

you have to make your while loop run three times. but here you are checking amount less than 3 which should be changed.

vipin sharma
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#5: Sep 20 '09

re: Suppose this might be a noob question:


Quote:

Originally Posted by vipin sharma View Post

hi

you have to make your while loop run three times. but here you are checking amount less than 3 which should be changed.

vipin sharma

No - if amount is 0, the loop will run 3 times, which is correct. However, the OP is not incrementing the variable, and is left with an infinite loop.
Newbie
 
Join Date: Jul 2009
Location: delhi, india
Posts: 16
#6: Sep 21 '09

re: Suppose this might be a noob question:


Quote:

Originally Posted by Markus View Post

No - if amount is 0, the loop will run 3 times, which is correct. However, the OP is not incrementing the variable, and is left with an infinite loop.

Hi markus

The loop will run three times only when proper looping condtion is given. e.g.
Expand|Select|Wrap|Line Numbers
  1. amount =0;
  2. while(amount<3)
  3. {
  4. amount +=1;
  5. }
  6.  
But the coding done by Halp will execute while loop unless and untill user doesn't enter value greater than or equal to 3 in amount (remember he is taking input from user in variable amount). Any one can make this loop run any num of times by entering values below 3.
If I have understood problem correctly then he wants to add up 3 inputs from user.

vipin sharma
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,153
#7: Sep 21 '09

re: Suppose this might be a noob question:


HALP

You are trying to use amount for 2 different purposes
  1. As a loop control variable. Such a variable would be initialised to 0 and incremented on each loop iteration until enough itterations have been achieve.
  2. User input variable. Such a variable would not need initialisation but would be used to contain user input and then used in calculating the total of that input.
These 2 uses are mutually exclusive, trying to use the same variable for both is doomed to failure. You need 2 variables where you have 1.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#8: Sep 21 '09

re: Suppose this might be a noob question:


Quote:

Originally Posted by vipin sharma View Post

Hi markus

The loop will run three times only when proper looping condtion is given. e.g.

Expand|Select|Wrap|Line Numbers
  1. amount =0;
  2. while(amount<3)
  3. {
  4. amount +=1;
  5. }
  6.  
But the coding done by Halp will execute while loop unless and untill user doesn't enter value greater than or equal to 3 in amount (remember he is taking input from user in variable amount). Any one can make this loop run any num of times by entering values below 3.
If I have understood problem correctly then he wants to add up 3 inputs from user.

vipin sharma

Which is exactly what I said, though I concede I misread your previous post.
Newbie
 
Join Date: Sep 2009
Posts: 2
#9: Oct 16 '09

re: Suppose this might be a noob question:


Late response on my end, but thanks for the help!

Now we are learning about arrays etc etc
Ahh my brain
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,153
#10: Oct 16 '09

re: Suppose this might be a noob question:


Try reading Arrays Revealed
Reply