Thread: Program giving errors

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Unhappy Program giving errors

    /* There are errors in this program,, I do not want the program altered in any way as I won't learn that way
    All I need is to know is what line the errors are on, and explain why they are errors please so I can correct the program myself
    THANKS */

    /* Hi_Low&Average.C */
    #include <stdio.h>

    void main()
    {
    int hi, low, number , num_count, value,
    average, total;

    /* set hi to owest integer value and low to highest
    integer so that they will change */
    hi = -32768;
    low = 32767;
    number = 0 = num_count = 0 = total = 0;

    /* test to ensure number of inputs is positive */
    while ( number < 1 ) do
    {
    printf("\nEnter number of repititions > ");
    scanf("%d" ,&number);
    if( number < 1 ) then
    printf("\nNumber must be > 1\n");
    }

    /* loop for input of numbers */
    for( num_count = 0, num_count < number; num_count++ )
    {
    prinf("Enter number #%d > ", num_count+1);
    scanf ("%d" , &value );
    /* test for highest and lowest values */
    switch ( value )
    {
    case value > hi : hi = value;
    break;
    case value < low: low = value;
    break;
    case default: break;
    }
    total:= total + value;
    }

    /* Calculate and display average */
    average = total / number;
    printf("\nAverage of %d numbers is %d", number, average);

    /* display highest and lowest values */
    printf("\nHighest Value is %d, Lowest is %d" , hi, low);

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    >>number = 0 = num_count = 0 = total = 0;

    for one thing, this line would be better off as:

    number = num_count = total = 0;

    makes sense to you why, I hope.
    Code:
    while ( number < 1 ) do 
    { 
    printf("\nEnter number of repititions > "); 
    scanf("%d" ,&number); 
    if( number < 1 ) then 
    printf("\nNumber must be > 1\n"); 
    }
    You obviously haven't read up on how to loop correctly, try:
    Code:
    while ( number < 1 ) 
    { 
    printf("\nEnter number of repititions > "); 
    scanf("%d" ,&number); 
    if( number < 1 )
    {
    printf("\nNumber must be > 1\n"); 
    }
    for a while loop, and
    Code:
    do
    { 
    printf("\nEnter number of repititions > "); 
    scanf("%d" ,&number); 
    if( number < 1 )
    {
    printf("\nNumber must be > 1\n"); 
    } 
    while ( number < 1 );
    for a do-while loop.

    also, there's no "then" in C, so get rid of that, and replace it with a bracket {

    Code:
    for( num_count = 0, num_count < number; num_count++ )
    change that to

    for( num_count = 0; num_count < number; num_count++ )
    because you had a comma there instead of a semicolon.

    Code:
    switch ( value ) 
    { 
    case value > hi : hi = value; 
    break; 
    case value < low: low = value; 
    break; 
    case default: break; 
    }
    change that to two if statements, because you can't evaluate expressions in a switch-case.

    Code:
    total:= total + value;
    this isn't Pascal! you meant total= total + value; right?

    also, on your very last line add a } to close off the main function




    Warning: don't look at this if you don't want to see the fixed version of your program - I took the liberty of doing so as I was fixing it
    Code:
    #include <stdio.h> 
    
    int hi, low, number , num_count, value, average, total; 
    
    void main() 
    { 
    	/* set hi to owest integer value and low to highest 
    	integer so that they will change */ 
    	hi = -32768; 
    	low = 32767; 
    	number = num_count = total = 0; 
    
    	/* test to ensure number of inputs is positive */ 
    	while ( number < 1 ) 
    	{ 
    		printf("\nEnter number of repititions > "); 
    		scanf("%d" ,&number); 
    		if( number < 1 ) 
    		{
    			printf("\nNumber must be > 1\n");
    		}
    
    	}
    
    
    /* loop for input of numbers */ 
    	for( num_count = 0;num_count <number; num_count++ ) 
    	{ 
    		printf("Enter number #%d > ", num_count+1); 
    		scanf ("%d" , &value ); 
    		/* test for highest and lowest values */ 
    		if(value > hi)
    			hi = value;
    		else if(value < low)
    			low = value;
    
    	total= total + value; 
    	} 
    
    	/* Calculate and display average */ 
    	average = total / number; 
    	printf("\nAverage of %d numbers is %d", number, average); 
    
    	/* display highest and lowest values */ 
    	printf("\nHighest Value is %d, Lowest is %d\n" , hi, low);
    }

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm senseing a homework question here

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    13
    Thanks guys.
    Must still be trying to do the pascal thang .
    Thanks for your help.
    And NO not homeworking trying to get the C thang hard to shake pascal.

  5. #5
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    oh yeah, and before I forget - use int main() and don't forget to add return 0; before your last }

  6. #6
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    The code tags keep things neat too, Ken. I've heard great things from the people who urge members to read the posting guidelines and faq, Andy. By they way, that is a great example for stretching the legs with a language.

    Best Regards,

    New Ink -- Henry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-10-2009, 11:12 PM
  2. Errors in Dev-C++ program
    By sinking2008 in forum C Programming
    Replies: 5
    Last Post: 05-15-2008, 09:00 AM
  3. Cubic equation program not giving me right answers
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2006, 05:42 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 10:40 AM