-2

I cant understand the error this in code. No outputs in this code. What is the error.

checked the .dat file and it contains data.

code:

#include <stdio.h>

int main()
{
    FILE *patPtrR;
    char name[50];
    int pCount,i,c[8];
    float total,avg;

    patPtrR = fopen("count.dat","r");

    if (patPtrR == NULL)
    {
            printf("\nError..\n");
            return -1;
    }

    fscanf(patPtrR, "%s %d %d %d %d %d %d %d %d", name,c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7]);

    printf("%s",name);

    while (!feof(patPtrR))
    {
            for(i=0;i<8;i++)
            {
                    total += c[i];
            }
            avg = total /8.0;

            printf("Name: %s\n",name);

            if (avg < 20)
                    printf("Severe dengue\n");
            else if (avg > 20 && avg < 50)
                    printf("Moderate denge\n");
            else if (avg > 50 && avg < 100)
                    printf("Mild dengue\n");

            fscanf(patPtrR,"%s %d %d %d %d %d %d %d %d",name,c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7]);
    }


    fclose(patPtrR);
    return 0;
}

whats wrong with this code. i cant get any output. No output for part B.
Need to out puts the result along with the name. No output for part b

  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 11 '18 at 06:55
  • 1
    Most importantly: Please make questions *self-contained*, without links to external sites that can go stale (and make the question worthless). – Some programmer dude Oct 11 '18 at 06:56
  • 2
    [Why_we_don't_answer_homework_questions](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – kiran Biradar Oct 11 '18 at 06:56
  • On an unrelated note: When you use [`scanf`](https://en.cppreference.com/w/c/io/fscanf) to read a string (with the `"%s"` format) it expects a pointer to the first element in the array (and of type `char *`), i.e. `&name[0]`. This also happens to be the same as plain `name` (as arrays naturally decays to pointers to their first element). With `&name` you get a pointer to the *array*, and its type is `char (*)[50]`. Also note that `"%s"` reads a *space delimited* "word", so you can't read a first and last name (separated by space) with a single `"%s"`. – Some programmer dude Oct 11 '18 at 06:58
  • 1
    This site is not for answering from scratch. Do some development and if you are stuck ask your queries here. We can help on that – Sanjeev S Oct 11 '18 at 07:05
  • @Someprogrammerdude i edited the question. Please look. – Shehan Rathnayake Oct 11 '18 at 07:33
  • While you should not use the address-of operator `&` for strings (as they already are or becomes pointers), you must still use it for any other format. Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) And don't forget to check what `fscanf` *returns*. – Some programmer dude Oct 11 '18 at 07:38
  • 1
    1. You should check the return value of `fscanf` rather than using `feof`. You are not using the & operator for `c[0]` `c[1]` etc. – Rishikesh Raje Oct 11 '18 at 07:40
  • Please show an short example of the `count.dat` file. – Jabberwocky Oct 11 '18 at 08:40
  • @Jabberwocky abc 10 20 30 40 50 60 70 80 \n pqr 80 70 60 50 40 30 20 10 – Shehan Rathnayake Oct 11 '18 at 09:30
  • @ShehanRathnayake please [edit] your question and put all clarifications _there_ – Jabberwocky Oct 11 '18 at 09:31

1 Answers1

2

Few problems here.

  1. fscanf needs pointer(int * in your case) to read in but you are passing int.

  2. Do not use while (!feof(patPtrR)) ((Why is “while ( !feof (file) )” always wrong?)) instead you can check return value of fscanf in while loop.

  3. You are not resetting the total in while loop.

    patPtrR = fopen("count.dat","r");
    
    if (patPtrR == NULL)
    {
        printf("\nError..\n");
        return -1;
    }
    
    while (9 == fscanf(patPtrR, "%s %d %d %d %d %d %d %d %d",
          name,&c[0],&c[1],&c[2],&c[3],&c[4],&c[5],&c[6],&c[7]))
    {
            total = 0; // Reset the total
            for(i=0;i<8;i++)
            {
                    total += c[i];
            }
            avg = total /8.0;
    
            printf("Name: %s\n",name);
    
            if (avg < 20)
                    printf("Severe dengue\n");
            else if (avg > 20 && avg < 50)
                    printf("Moderate denge\n");
            else if (avg > 50 && avg < 100)
                    printf("Mild dengue\n");
    
    }
    
    fclose(patPtrR);
    
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44