Thread: [Code] Help with identifying errors

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    1

    Unhappy [Code] Help with identifying errors

    Hello! I need help in identifying errors here and corrections is needed actually...

    Code:
     
    #include <stdio.h>
    
    
    int main()
    {
    
    
        float temp;
        char child;
        int i;
    
    
        printf("Children A&E Department.  We can only treat 3 children each time. \n\n");
        for (i = 0; i < 3; i++);
        {
            printf("Enter body temperature of patient %d in degree Celcius: ", i + 1);
            scanf("%.1f",  &temp);
    
    
            for (;; // forever loop
            {
                printf("Is patient a child age 0 to 11 (y/n): ");
    
    
                fflush(stdin);   //need to have  fflush(stdin);  if you scanf(“%c”, …) to read a char from user
                scanf("%c", &child);
                if (child = 'y' || child = 'n');   //will repeat until user enters 'y' or 'n'.
                {
                    break;
                }
            }
            if (child = 'y');
            {
                if (temper < 36.6);
                {
                   printf("Child patient's temper of %.1f deg Celcius is Below Average \n", &temp);
                }
                else if (temper <= 37.2);    //else if (temper >= 36.6 && temper <= 37.2)
                {
                    printf("Child patient's temper of %.1f deg Celcius is Normal \n", &temp);
                }
                else   //fever if body temperature is > 37.2 Deg Celcius
                {
                    printf("Child patient's temper of %.1f deg Celcius is Above Normal. Fever ! \n", &temp);
                }
            }
            else
            {
                printf("This is Children A&E Department here. Please go to Normal A&E Department. \n");
                i--;    //this loop is skipped (i.e. this loop is a “don’t care”), so don’t let i increment by one
                // continue;   //continue; will skip this loop, and go to i++ of for loop
            }
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,595
    After a quick glance, look at this snippet:

    Code:
     
            for (;; // forever loop   /////////// Missing a closing parentheses.
            {
                printf("Is patient a child age 0 to 11 (y/n): ");
     
     
                fflush(stdin);   //need to have  fflush(stdin);  if you scanf(“%c”, …) to read a char from user   NO, this invokes undefined behaviour, fflush() is only defined to be used with output streams.
                scanf("%c", &child);
    Instead of invoking undefined behaviour with fflush() just tell scanf() to skip leading whitespace (scanf(" %c", &child) (notice the leading whitespace).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,621
    Looks like a "fix the errors" homework.
    Tutor hands students some code with errors deliberately added, and it's the students job to fix it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in identifying a problem in my code.
    By mbelegu in forum C Programming
    Replies: 3
    Last Post: 12-10-2016, 08:50 AM
  2. Errors in code. Why?
    By Eric Le in forum C Programming
    Replies: 1
    Last Post: 11-03-2016, 02:05 AM
  3. What is the errors in this code? please help
    By Ma'd Saeed in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2014, 06:29 AM
  4. Need some help with errors in the code
    By lukis123 in forum C Programming
    Replies: 5
    Last Post: 11-03-2011, 11:00 PM
  5. 2 errors in my code
    By johnnyg in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2006, 08:04 AM

Tags for this Thread