what am i doing wrong?

This is a discussion on what am i doing wrong? within the C Programming forums, part of the General Programming Boards category; So here's my problem: Write a program that reads a list of integers from thekeyboard and creates the following information: ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    what am i doing wrong?

    So here's my problem:
    Write a program that reads a list of integers from thekeyboard and creates the following information:
    a. Finds and prints the sum and average of the integers
    b. Finds and prints the largest and the smallest integer
    c. Prints a Boolean (true or false) if some of them are less than 20
    d. Prints a Boolean (true or false) if all of them are between 10 and 90
    The input data consist of a list of integers with a sentinel. The program must prompt the user to enter the integers, one by one, and enter the sentinel when the end of the list has been reached (represented by 99999).


    Here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      /*Local Definitions*/
      int num;
      int sum = 0;
      int larg = 0;
      int smal = 99998;
      int less = 0;
      int tween = 0;
      int count = 0;
      long avg;
      
      /*Statements*/
         printf("Enter numbers with <return> (99999 to stop):");
              while (scanf("%d", &num)!= 99999);
                count++;
                sum += num;
                avg = sum / count;
                {
                     if (num < smal);
                       {
                         smal = num;
                       }        
                     
                     if (num > larg);
                       {
                         larg = num;
                       }    
                     
                        
                     if (num < 20);
                       {
                              less = 1;
                       }    
                          
                     if (10 < num < 90);
                       {
                                tween = 1;
                       
                       }
               printf("The number of integers is: \t%d", count);
               printf("The sum of the integers is: \t%d", sum);
               printf("The average of the integers is: \t%d", avg);
               printf("The smallest integer is: \t%d", smal);
               printf("The largest integer is: \t%d", larg);
                          if (less = 1)
                            {
                             printf("At least one number was < 20:   True"); 
                            }                       
                          else printf("At least one number was < 20:   False");
                          if (tween = 1)
                          {
                             printf("All numbers were (10 <= n <= 90): True");
                         }    
                          else printf("All numbers were (10 <= n <= 90):  False");
                
                                 
        
        
      system("PAUSE");	
      return 0;
    }
    I am getting a "parse error at end of input" message, and I can't figure out why. Any help would be appreciated. Thanks
    melee

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    while (scanf("%d", &num)!= 99999);
    Beware of the semi-colon!

    Edit: Also your opening { after that line should be moved up higher (like inplace of the aforementioned semi-colon) and you seem to be missing a closing }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by melee
    Code:
    while (scanf("%d", &num)!= 99999);
    Also, scanf returns the number of arguments that it successfully converted/processed so the above will not do what you want it to. You probably want to do something like this:

    Code:
    scanf("%d",&num);
    while (num != 99999)
    {
        // Put your code that you want to loop through here
        scanf("%d",&num);
    }
    Quote Originally Posted by melee
    Code:
    if (10 < num < 90);
    Most of your if statements (except for the last couple) suffer from an improperly trailing semicolon. Get rid of them! Also you can't test the value of num like you are doing above. You should probably set this value to true initially and then in the loop set it to false if you encounter any value outside this range:

    Code:
    int tween = 1;
    ...
    if( num <= 10 || num >= 90 )
        tween = 0;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Yes - placing a colon right after a loop like that will cause the loop to do absolutely nothing - it will cycle again one it hits either a semicolon or a { } set.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    Ok, here's my (slightly) inproved code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      /*Local Definitions*/
      int num;
      int sum = 0;
      int larg = 0;
      int smal = 99998;
      int less = 0;
      int tween = 1;
      int count = 0;
      long avg;
      
      /*Statements*/
         printf("Enter numbers with <return> (99999 to stop):");
              scanf("%d", &num);
              while  (num != 99999)
               { 
                 count++;
                 sum += num;
                 avg = sum / count;
                     if (num < smal)
                       {
                         smal = num;
                    }          
                     
                     if (num > larg)
                      { 
                         larg = num;
                  }         
                     
                        
                     if (num < 20)
                       {
                              less = 1;
                    }       
                      if( num <= 10 || num >= 90 )
                       {
                             tween = 0; 
                         }    
                }    
               printf("The number of integers is: \t%d", count);
               printf("The sum of the integers is: \t%d", sum);
               printf("The average of the integers is: \t%d", avg);
               printf("The smallest integer is: \t%d", smal);
               printf("The largest integer is: \t%d", larg);
                          if (less = 1)
                            {
                             printf("At least one number was < 20:   True"); 
                            }                       
                          else printf("At least one number was < 20:   False");
                          if (tween = 1)
                          {
                             printf("All numbers were (10 <= n <= 90): True");
                         }    
                          else printf("All numbers were (10 <= n <= 90):  False");
                
                                 
        
        
      system("PAUSE");	
      return 0;
    }

    I got rid of the trailing semicolon's after the if statements, I changed the if (10 < num < 90); statement to if( num <= 10 || num >= 90 ) and changed the initialization of tween to true as opposed to false.
    I also moved the leading { up a few lines to include the first couple statements. I now compile fine, but the program stops after the first number is input. No output, and I can't input another number. There is something about this problem that just won't work itself out in my head.
    melee

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok you need to have a scanf() at the end of the loop to get the next number.

    Also
    Code:
    avg = sum / count;
    Needs to go outside the loop

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Move your initial printf and scanf inside the while loop.

    After you input the first number, the while loop is endless since there is no opportunity for num == 9999.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    ok, almost got it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      /*Local Definitions*/
      int num;
      int sum = 0;
      int larg = 0;
      int smal = 99998;
      int less = 0;
      int tween = 1;
      int count = 0;
      long avg;
      
      /*Statements*/
         printf("Enter numbers with <return> (99999 to stop):");
              scanf("%d", &num);
              while  (num != 99999)
               { 
                 count++;
                 sum += num;
                 
                     if (num < smal)
                       {
                         smal = num;
                    }          
                     
                     if (num > larg)
                      { 
                         larg = num;
                  }         
                     
                        
                     if (num < 20)
                       {
                              less = 1;
                    }       
                      if( num <= 10 || num >= 90 )
                       {
                             tween = 0; 
                         }    
                        scanf("%d", &num); 
                }    
                avg = sum / count;
               printf("The number of integers is: \t\t%d\n", count);
               printf("The sum of the integers is: \t\t%d\n", sum);
               printf("The average of the integers is: \t%d\n", avg);
               printf("The smallest integer is: \t\t%d\n", smal);
               printf("The largest integer is: \t\t%d\n", larg);
                          if (less = 1)
                            
                             printf("At least one number was < 20:   \tTrue\n"); 
                                                   
                          else printf("At least one number was < 20:   \tFalse\n");
                          if (tween = 1)
                          
                             printf("All numbers were (10 <= n <= 90): \tTrue\n");
                             
                          else printf("All numbers were (10 <= n <= 90):  \tFalse\n");
                
                                 
        
        
      system("PAUSE");	
      return 0;
    }
    The program works fine except it always prints "At least one number was < 20: True" and "All numbers were (10 <= n <= 90): True" even if the numbers were not true in those cases. Something wrong with my
    Code:
                          if (less = 1)
                            
                             printf("At least one number was < 20:   \tTrue\n"); 
                                                   
                          else printf("At least one number was < 20:   \tFalse\n");
                          if (tween = 1)
                          
                             printf("All numbers were (10 <= n <= 90): \tTrue\n");
                             
                          else printf("All numbers were (10 <= n <= 90):  \tFalse\n");
    segment?
    melee

  9. #9
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Look closely at
    Code:
    if (less = 1)
    and
    Code:
    if (tween = 1)

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    ahh. <-punches self in head
    Thank you Scribbler, Thantos, sean_mackrory, and hk_mp5kpdw!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

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