Thread: Problem with loops

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Problem with loops

    This program is supposed to have the user input a positive value including zero, tally valid and invalid(-2 or less) numbers, then find the average of the valid numbers. -1 is used as a sentinel value. The problem I'm having is that when I put in a invalid number it will work, but put in another invalid number, it doesn't work. I might be wrong, but it seems that it is treating it as a valid number and just subtracting it from the total used to compute the average.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main (void) {
    
    int	num, sum, valid, invalid;
    float avg;
    
    	num=0;
    	sum=0.0;
    	valid = 0;
    	invalid = 0;
    	printf("Please enter a positive value or -1 to terminate the program:");
    	scanf("%d", &num);
    
    	while (num!=-1) {
    		if (num<=-2) {
    			invalid++;
    			printf("\n%d is not a valid positive value!!\n", num);
    			printf("Please enter a positive value or -1 to terminate the program:");
    			scanf("%d", &num);
    		}
    		else {
    			sum=sum+num;
    			valid++;
    		}
    		printf("Please enter a positive value or -1 to terminate the program:");
    		scanf("%d", &num);
    	}
    	avg=sum/valid;
    	printf("sum=%d\n", sum);
    	printf("valid=%d\n", valid);
    	printf("invalid=%d\n", invalid);
    	printf("avg=%.2f\n", avg);
    }
    There might be better ways to do this but I hvae to use while, if, and else loops.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, go read FAQ entry, which will show you why you're using main incorrectly.

    [edit]
    You're double-prompting for numbers when an invalid one occurs:
    Code:
    if (num<=-2) {
    			invalid++;
    			printf("\n%d is not a valid positive value!!\n", num);
    			printf("Please enter a positive value or -1 to terminate the program:");
    			scanf("%d", &num);
    		}
    		else {
    			sum=sum+num;
    			valid++;
    		}
    		printf("Please enter a positive value or -1 to terminate the program:");
    		scanf("%d", &num);
    Oh, and I can crash your program on this line:
    Code:
    avg=sum/valid;
    [/edit]


    Quzah.
    Last edited by quzah; 10-19-2004 at 07:10 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Thank you for your help. That did the trick. I know about the main (void), but that is what my instructor wants . We know the program can crash, but we haven't got far enough crash-proof our program .

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but that is what my instructor wants
    I wonder what other crap they're going to teach you then if they can't even get page 1 stuff right?
    Worse still, how will you tell whether they're teaching you crap or not?

    Better ask them what gets(buff) and fflush(stdin) do then.
    If they get both of those wrong as well, I'd walk away from the course because I doubt you'll learn anything of value from such an idiotic tutor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Salem
    If they get both of those wrong as well, I'd walk away from the course because I doubt you'll learn anything of value from such an idiotic tutor.
    Sadly, that is not always an option.
    Lucky me, I had the pleasure to learn from a few good profs during the last semesters. One of them used to give a (really significant) amount of bonus points whenever a group could prove him or anyone from his staff wrong. Didn't happen too often, but it did . Combined with bonus points from the various competions he held (fastest code, cleanest code, safest code... you get it), some of us passed the semester-end exam even before it took place.
    So, I guess that's enough offtopic talk from me...
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 01:09 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM