Thread: Using Structures to produce Report

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Using Structures to produce Report

    Hello all I am needing to get this program to print off a report listing the data entered and then at the end put each employees total pay and then the overall total pay of everyone. It doesn't have to export a file, simply print the result. This is the code I have so far and it is simply producing a jumbled output, help would be great
    Code:
    #include <stdio.h>#define NUMRECS 6
    struct payrecord
    {
           int id;
           char name[20];
           float rate;
           float hrs;
           };
           
    int main()
    {
        int i;
        struct payrecord employee[NUMRECS] = {{3462, "Jones", 6.62, 40.0},
                                              {6793, "Robbins", 5.83, 38.5},
                                              {6985, "Smith", 5.22, 45.5},
                                              {7834, "Smith", 6.89, 40.0},
                                              {8867, "Timmins", 6.43, 35.5},
                                              {9002, "Williams", 4.75, 42.0},
                                              };
        float total[NUMRECS];
        float otot=0;
        
        for (i=0; i<NUMRECS; i++)
            total[i]=employee[i].rate*employee[i].hrs;
            otot=otot+total[i];
            printf ("%d %-20s %4.2f %2.1f %1.1f %f\n",
     employee[i].id, employee[i].name, employee[i].rate, employee[i].hrs, total[i], otot);
            
        system ("PAUSE");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I will show a portion of your code indented to show how the logic is working:

    Code:
    for (i=0; i<NUMRECS; i++)
        total[i]=employee[i].rate*employee[i].hrs;
    otot=otot+total[i];
    printf ("%d %-20s %4.2f %2.1f %1.1f %f\n",
    employee[i].id, employee[i].name, employee[i].rate, employee[i].hrs, total[i], otot);
    As you can see, only the first line after the "for()" loop is looped. If you want the following lines as part of the loop, you must enclose those statements in braces.

    Code:
    for (i=0; i<NUMRECS; i++)
    {
        total[i]=employee[i].rate*employee[i].hrs;
        otot=otot+total[i];
        printf ("%d %-20s %4.2f %2.1f %1.1f %f\n",
        employee[i].id, employee[i].name, employee[i].rate, employee[i].hrs, total[i], otot);
    }

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Wow thank you, I can't believe I forgot the braces It's been a long morning

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome!

    If you're not aware, a good easy debugging technique for simpler programs like this is to just sprinkle some "printf()" statements in your code to see the state of variables and what blocks are getting executed. Of course, using an actual debugger is much more informative, but the previous way is more beginner friendly.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Hopefully you may be able to help me out with something else, I am trying to set up this sequence using an if else chain or while statements whichever will work but I keep having problems with it, it'd be great if someone could look at it because once this is done I have to tackle a larger next step
    Code:
    puts("Enter a month, day, and year separated by spaces (mm dd yy):\n");
    if (scanf("%d %d %d", &tom.month, &tom.day, &tom.year) != 3);
    
    
    puts("Enter another month, day, and year separated by spaces (mm dd yy):\n");
    if (scanf("%d %d %d", &mom.month, &mom.day, &mom.year) != 3);
    
    
        if (tom.year>mom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.year>tom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);
        
        else (tom.year==mom.year){
        if (tom.month>mom.month)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.month>tom.month)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);}
        
        else if(tom.month==mom.month){
        if (tom.day>mom.day)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.day>tom.day)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);}
        
        else if (tom.day==mom.day)
        printf ("\nThe two dates are the same.\n\n");
        }}
    I keep getting errors with the last two closing brackets but I know the true issue is in how i set up my statements I'm just not sure where.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What are the problems you're having with this piece of code? And can you put it into a simple program that we can compile?

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Well the main issue is that it wont compile giving me an error on the line with the brackets closing out some of the if statements. I'm just not sure if using if statements is the best way to go, or use while statements. Essentially i just need to determine which date is larger/later
    Code:
    #include <stdio.h>
    
    
    struct date
    {
       int month;
       int day;
       int year;
    };
    
    
    int main()
    {
    
    
    struct date tom;
    struct date mom;
     
    puts("Enter a month, day, and year separated by spaces (mm dd yy):\n");
    if (scanf("%d %d %d", &tom.month, &tom.day, &tom.year) != 3);
    
    
    puts("Enter another month, day, and year separated by spaces (mm dd yy):\n");
    if (scanf("%d %d %d", &mom.month, &mom.day, &mom.year) != 3);
    
    
        if (tom.year>mom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.year>tom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);
        
        else (tom.year==mom.year){
        if (tom.month>mom.month)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.month>tom.month)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);}
        
        else if(tom.month==mom.month){
        if (tom.day>mom.day)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.day>tom.day)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);}
        
        else if (tom.day==mom.day)
        printf ("\nThe two dates are the same.\n\n");
        }}
    
    
    system ("PAUSE");
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Heed all compiler warnings and errors.

    Code:
    main.c||In function 'main':|
    main.c|33|warning: statement with no effect|
    main.c|33|error: expected ';' before '{' token|
    main.c|49|error: expected identifier or '(' before '}' token|
    main.c|52|error: expected declaration specifiers or '...' before string constant|
    main.c|52|warning: data definition has no type or storage class|
    main.c|52|warning: type defaults to 'int' in declaration of 'system'|
    main.c|53|error: expected identifier or '(' before 'return'|
    main.c|54|error: expected identifier or '(' before '}' token|
    ||=== Build finished: 5 errors, 3 warnings ===|
    Lines 20 and 24: You're not doing anything when the "scanf()" is returning the wrong value. It's in an "if()" statement, but that "if()" statement is followed by a null statement (which means to do nothing).

    Code:
    if (scanf("%d %d %d", &tom.month, &tom.day, &tom.year) != 3);
    
    // is equivalent to...
    
    if (scanf("%d %d %d", &tom.month, &tom.day, &tom.year) != 3)
        ;  /* do nothing */
    Line 33: You forget an "if" - you cannot put an argument after an "else" like that.

    Line 49: Where do those closing braces correspond to? If you indented your code better, the brace placement would be obvious.

    ---

    Indentation rules of thumb:

    1. If you use an opening brace, the following line(s) should be indented by one tabstop. If you have a closing brace, the following line(s) should be un-indented by one tabstop.

    2. If you have a single command immediately following a loop or conditional statement, without braces (so that just one command is executed after said statement), indent that one line of code by one tabstop.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Code:
    #include <stdio.h>
    #define max 1
    
    
    
    
    struct date
    {
       int month;
       int day;
       int year;
    };
    
    
    int main()
    {
    
    
    struct date tom;
    struct date mom;
    int i;
     
    puts("Enter a month, day, and year separated by spaces (mm dd yy):\n");
    scanf("%d %d %d", &tom.month, &tom.day, &tom.year);
    
    
    puts("Enter another month, day, and year separated by spaces (mm dd yy):\n");
    scanf("%d %d %d", &mom.month, &mom.day, &mom.year);
    
    
    for (i=0; i<max; i++){
        if (tom.year>mom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.year>tom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);
        
           while (tom.year==mom.year){
               if (tom.month>mom.month)
               printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
               else if (mom.month>tom.month)
               printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);
        
                   while (tom.month==mom.month){
                   if (tom.day>mom.day)
                   printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
                   else if (mom.day>tom.day)
                   printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);
        
                   while (tom.day==mom.day)
                   printf ("\nThe two dates are the same.\n\n");}}
    }
    system ("PAUSE");
    return 0;
    }
    I have gotten it where it now correctly identifies the larger/later date but now it is printing out infinite printf answers. The only time it prints correctly is if one "year" is greater than the other.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need to step through your logic (step by step) and understand what you're telling the program to do. Don't code it all out at once - write a few lines, compile, take care of warnings/errors, test what you have so far - add a few more lines, repeat.

    A development process

    For instance, look at some of your "while()" loops. They will clearly run forever.

    Code:
    while(condition)
    {
        /* if "condition" is true, then the code in this body will execute */
    
        /* however, if "condition" is never updated in this loop,
           it will always be true and the loop will go on forever */
    }
    I'd recommend taking a deep breath, getting rid of everything in your "for()" loop (just cut and paste it into a text editor if you don't want to lose it altogether), and start the process again, taking care that each step does exactly what you want it to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What output does it produce?
    By intimidator in forum C Programming
    Replies: 7
    Last Post: 04-24-2011, 01:58 AM
  2. Function to produce sum with int and char
    By quintenmater in forum C Programming
    Replies: 15
    Last Post: 12-06-2010, 01:33 AM
  3. I would like to Produce 2d and 3d plots
    By BobInNJ in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2009, 10:16 PM
  4. Can't Produce a buffer overflow
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 09-15-2008, 08:59 AM
  5. outputting a report arrays of structures
    By elusive in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 11:15 PM

Tags for this Thread