-2

I want to find the Grand Total by finding from the unit*price, but it gets 0.00, although it should be 12,640.00 so does anybody know how I can do this?

Here is the code

#include <stdio.h>

int main()
{
    FILE *in;

    char id[20][15];  // 20 persons   
    char des[20][15];
    int unit[30];
    float price[30];
    float sum = 0.0, total = 0.0;
    int i=0, j;

    in = fopen("price.txt", "r") ;
    if ( in == NULL )
    {
        printf( "Could not open file test.c" ) ;
        return 1;
    }

    while(!feof(in))
    {
        fscanf(in, "%s %s %d %f", &id[i], &des[i], &unit[i], &price[i]);
        i++;
    }

    printf("No. ID Descripton Price/Unit Unit Total Amount\n");
    for(j=0; j < i ;j++)
       printf("%d. %s %s %d %0.2f %0.2f \n",j, id[j], des[j], unit[j], price[j], unit[j]*price[j]);
    sum = unit[j]*price[j];
    total += sum;
    printf("Grand Total %0.2f",total);
    return 0;
}
Eduardo Pascual Aseff
  • 1,149
  • 2
  • 13
  • 26
  • 2
    And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Feb 16 '20 at 15:50
  • 3
    Only the `printf` statement is in the body of the `for` loop. The statements that update `sum` and `total` are not, and only run once. You already know how to use braces. – Igor Tandetnik Feb 16 '20 at 15:51
  • 3
    Clue: What do *you* think is the 'body' of your `for(j...)` loop? – Adrian Mole Feb 16 '20 at 15:51
  • 1
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Yunnosch Feb 16 '20 at 15:54
  • 1
    https://en.cppreference.com/w/cpp/algorithm/max_element , https://en.cppreference.com/w/cpp/algorithm/accumulate – Jesper Juhl Feb 16 '20 at 15:55
  • 2
    Another victim of moving from Python to C.... Neither language is undisputably better than the other, but this change is deadly. – Yunnosch Feb 16 '20 at 15:56
  • Since you have no braces (`{}`) on your loop, only the *first* statement following it is actually *in* the loop. Indentation is *not* significant in C++ (unlike Python). – Jesper Juhl Feb 16 '20 at 16:04
  • `FILE *in;` - personally I stop right there. If this is supposed to be C++, then why are we using C style file I/O? – Jesper Juhl Feb 16 '20 at 16:10
  • 2
    Please change the tag from C++ to C. There is ZERO C++ code in here. – A M Feb 16 '20 at 16:22

1 Answers1

1

I see that you are a beginner. Well, the error here is that your for loop never actually calculates the sum By default, the for loop only reads the line below it, unless there is any code enclosed curly braces{} spanning multiple lines.

for(j=0; j < i ;j++)
{    
    printf("%d. %s %s %d %0.2f %0.2f \n",j, id[j], des[j], unit[j], 
    price[j], 
    unit[j]*price[j]);
    sum = unit[j]*price[j];
    total += sum;
}

Also, just an advice, please try to indent your code better. It will help you in the future :)

Best.

rasengan__
  • 883
  • 8
  • 16