loops

This is a discussion on loops within the C Programming forums, part of the General Programming Boards category; I am taking a c program course(because it is required) I am just trying to muddle through it- no plans ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Question loops

    I am taking a c program course(because it is required) I am just trying to muddle through it- no plans on being a programmer.

    My program runs, but incorrectly. I know what is wrong, it is the while stmt, but just not how to fix it. It is a basic program that counts + and- inputs and computes the avg.

    I only want the 1st 2 print f stmts to print once, now it is printing as many times as the count is. Understand?

    while(num!=0){
    printf("Please input some numbers:\n");
    scanf("%f",&num);
    printf("I have got:\n");

    if(num==0.0)
    break;
    if (num<0.0){
    avgneg+=num;
    negcount++;
    }
    else if (num>0.0) {
    avgpos+=num;
    poscount++;
    }
    }
    if (poscount>0){
    printf(" %d positive numbers.", poscount);
    printf("Average is %.1f\n", avgpos/poscount);
    }
    if (negcount>0){
    printf(" %d negative numbers.",negcount);
    printf("Average is %.1f\n",avgneg/negcount);
    }
    }
    Thanks

  2. #2
    Unregistered
    Guest
    Place the two statements that you want to only occur once outside of the loop.
    Code:
    printf("Please input some numbers:\n"); 
    scanf("%f",&num); 
    printf("I have got:\n");
    
    while(num!=0)
    { 
      if(num==0.0) 
        break; 
      if (num<0.0)
      { 
        avgneg+=num; 
        negcount++; 
      } 
      else if (num>0.0) 
      { 
        avgpos+=num; 
        poscount++; 
      } 
    } 
    if (poscount>0)
    { 
      printf(" %d positive numbers.", poscount); 
      printf("Average is %.1f\n", avgpos/poscount); 
    } 
    if (negcount>0)
    { 
      printf(" %d negative numbers.",negcount); 
      printf("Average is %.1f\n",avgneg/negcount); 
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    I 've tried that
    it still prints like this when I run the program:

    Please input some numbers:
    1 2 -1 -2 0
    I have got:
    Please input some numbers:
    I have got:
    Please input some numbers:
    I have got:
    Please input some numbers:
    I have got:
    Please input some numbers:
    I have got:
    2 positive numbers.Average is 1.5
    2 negative numbers.Average is -1.5

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    Nevermind

    playing around with the code I finally got it to work properly
    -m

  5. #5
    Unregistered
    Guest
    what was your problem with your coding?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 01:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 03:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21