Suppose this might be a noob question:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HALP
    New Member
    • Sep 2009
    • 2

    Suppose this might be a noob question:

    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;
    }
  • vipin sharma
    New Member
    • Jul 2009
    • 16

    #2
    hi

    Code:
    ****spoonfeeding removed******
    Describe your solution but just don't code it up.
         ---weaknessforcats
    regards
    Vipin Sharma

    Comment

    • Tassos Souris
      New Member
      • Aug 2008
      • 152

      #3
      You increment total but not amount.. .so the while loop keeps on executing for ever...

      Comment

      • vipin sharma
        New Member
        • Jul 2009
        • 16

        #4
        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

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by vipin sharma
          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.

          Comment

          • vipin sharma
            New Member
            • Jul 2009
            • 16

            #6
            Originally posted by Markus
            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.
            Code:
            amount =0;
            while(amount<3)
            {
            amount +=1;
            }
            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

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              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.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by vipin sharma
                Hi markus

                The loop will run three times only when proper looping condtion is given. e.g.
                Code:
                amount =0;
                while(amount<3)
                {
                amount +=1;
                }
                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.

                Comment

                • HALP
                  New Member
                  • Sep 2009
                  • 2

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

                  Now we are learning about arrays etc etc
                  Ahh my brain

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Try reading http://bytes.com/topic/c/insights/77...rrays-revealed

                    Comment

                    Working...