Thread: While loop with multiple conditions.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    3

    While loop with multiple conditions.

    My code's while loop has two values in it, a weight and a value. my sentinel value is "-1". I have tried to modify the conditions in the while loop to everything I can think of but I'm at a loss. When I enter my sentinel value in my standard input, it ends the loop but also factors in the sentinel value in the average. below is the code:

    Code:
    <#include <stdio.h>
    
    
    int main (void) {
    /* variable definition: */
       unsigned int counter;
       int count;
       double avg, value, weight, sum, sumw;
    /* Initialize */
       counter =0;
       count = 0;
       sum = 0;
       sumw = 0;
       avg = 0.0;
    // Loop through to input values
       while (value != -1) 
       {
          printf("Enter a value and its weight, -1 to end: ");
          // use %lf for double, %f for float
          scanf("%lf %lf", &value, &weight);
          printf("%.2lf (value) %.2lf (weight)\n", value, weight);
          if (weight != 0) {
             sumw = sumw + weight;
             sum = sum + value * weight;
             count = count + 1;
          }
          else {
             printf("Weight must be positive\n");
          } // end if weight ok
       } // end reading input values and weights
    // Calculate avg if sumw is not 0
       avg = sum / sumw;
       printf("average is %lf\n " , avg );
       return 0;
    } // end main>

    and below is the output:

    2 1 2 1 -1
    stdout copy
    Enter a value and its weight, -1 to end: 2.00 (value) 1.00 (weight)
    Enter a value and its weight, -1 to end: 2.00 (value) 1.00 (weight)
    Enter a value and its weight, -1 to end: -1.00 (value) 1.00 (weight)
    average is 1.000000


    I have tried to modify the code to read:

    while (value && weight != -1)
    While (value || weight != -1)
    while (value, weight =! -1)
    while (value != -1)
    while (weight != -1)
    While ((value) (weight) != -1)
    while ((value), (weight) != -1)

    none of these seem to work.


  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,037
    Why do you have this code?

    Code:
    if (weight != 0) {
    Instead of

    Code:
    if (weight > 0) {
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,037
    You do realize that you need to set weight to zero before the scanf, right?
    Because if you do not enter a value it stays what it was before.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    I had this code there simply because I attempted to change so many things in the code before posting here, I forgot to change it back. I am painfully new at this btw.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    I set weight=0 under the initialization, I assumed this is what you meant. I also changed the "!=" back to ">". the results are still the same as before. instead of the program terminating after -1, the program is factoring in the -1 as a value and entering in a weight of 1 AND terminating the program, instead of just simply terminating it. If my input was 2 1 2 1 2 1-1, i should be getting the average = 2, instead I'm getting 1.25. Im sure there is an easy or obvious fix but I'm not sure how much longer I can butcher this code before figuring it out...

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,037
    No, that is not what I meant.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,037
    Code:
    weight = 0;
    scanf("%lf %lf", &value, &weight);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    I don't see the point of assigning 0 to weight just before the scanf: it will typically get overwritten immediately, and if it doesn't, that's because of an input error. Instead, check the return value of scanf before proceeding to use value and weight.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,037
    Quote Originally Posted by laserlight View Post
    I don't see the point of assigning 0 to weight just before the scanf: it will typically get overwritten immediately, and if it doesn't, that's because of an input error. Instead, check the return value of scanf before proceeding to use value and weight.
    That is the more correct way, yes. But, my way is simpler for newbies.
    It depends on whether to teach the more correct way or the simple way is better in this instance.
    I think either will work; but, I have no idea which the OP is likely to understand.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statement multiple conditions
    By Jack Gell in forum C Programming
    Replies: 1
    Last Post: 01-07-2018, 09:10 PM
  2. Multiple conditions for while
    By sfff in forum C Programming
    Replies: 5
    Last Post: 11-08-2009, 11:10 PM
  3. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  4. Multiple conditions for the while loop?
    By Olidivera in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2005, 03:47 AM
  5. Testing Multiple Conditions
    By awilmut in forum C++ Programming
    Replies: 16
    Last Post: 10-20-2002, 10:05 PM

Tags for this Thread