Thread: C Help please

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    C Help please

    Whats up, I'm new here and new to programming. I have been looking around your forums for a couple of weeks and like it. I am sure I will be around here for a while.

    I am sure my questions for you guys will be pretty simple as i am a beginner in the programming world.

    We are working on an assingment for school. I am supposed to open a file and scan in some info, process it and output to a text file. I am calculating payroll to the output file. i a supposed to total up the gross total for each department, and alot of other stuff but at the moment i am totaling up gross for each department. I am having a hard time breaking my output file and putting the department grossTotal under each department.

    my code is as followed. could some one check it and tell me where i am going wrong. I am sorry if my explaination is hard to understand, and any help would be much appreciated. Thanks


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) {
        
        FILE *in_file;//file pointer
        FILE *out_file;// file to be saved
        char payfile, name[25], greatestNet, leastNet, empnum[5]; 
        float hours, overtime, rate, gross, deduct, net, depart, grossTotal, grossTotal1, netTotal, overTotal, avgGross, salary, stateTax, fedTax, medTax, ssTax;
        
        out_file = fopen ( "C:\\Documents and Settings\\Kevin\\Desktop\\projecttest\\payroll.txt", "w"); //Location of file after  it is written
        in_file = fopen ( "emps.dat", "r");//file i am opening for program the scan
        
          if( in_file == NULL ) {
            printf( "\nThe file cannot be opened.");//Dispayes message if file is not found
            printf( "\nPlease check that the file currently exists.");
            }    
          else {
            printf( "\nThe output file has been writen.\n");  //Displayes message when program has ran successfully
            
            }
          fprintf ( out_file, "Number    Name\t\t    Rate      Hours     Salary    Department    Overtime    Gross      Deductions    Net\n");
            
          while( fscanf( in_file, "%s %s %f %f %f %f", empnum, name, &rate, &hours, &salary, &depart) != EOF ) {
               gross = rate*hours;       
               stateTax = gross*.049;
               fedTax = gross*.1115;
               medTax = gross*.0145;
               ssTax = gross*.0145;
               deduct = ssTax+medTax+fedTax+stateTax;
               net = gross-deduct;
            
            if (salary >0){
              gross = salary;       
              stateTax = gross*.049;
              fedTax = gross*.1115;
              medTax = gross*.0145;
              ssTax = gross*.0145;
              deduct = ssTax+medTax+fedTax+stateTax;
              net = gross-deduct;
              }
          
           fprintf( out_file, "%-9s %-17s %-9.2f %-9.2f %-9.2f %-13.0f %-10.2f %-11.2f %-10.2f %-9.2f \n", empnum, name, rate, hours, salary, depart, overtime, gross, deduct, net);    
           }
             if (depart=10){
                 grossTotal = gross+gross+gross+gross+gross;
                 fprintf ( out_file, "\nTotal Gross For Department 10 = %.2f", grossTotal);
                 } 
    
         
         
         fclose (in_file);
         fclose (out_file);
            
         system("PAUSE");
         return 0;
         }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int main(int argc, char *argv[]) {
    You are using neither argc nor argv, so you can just declare main as:
    Code:
    int main(void)
    Code:
    	stateTax = gross*.049;
               fedTax = gross*.1115;
               medTax = gross*.0145;
               ssTax = gross*.0145;
    These are converting double to float, which is a bad thing. Those numbers are considered double, not float. Your compiler you should alert you on this even if warnings are not max. To make the numbers float you should do:
    Code:
    0.049f
    You can also make .049, etc to defines or const floats because you're referencing them in multiple positions in the source. It reduces the number of mistakes you can make.

    Variables payfile, avgGross, netTotal, grossTotal1, overTotal, greatestNet and leastNet are not used anywhere in the code.

    Code:
             if (depart=10){
    Assignment instead of comparison!

    Code:
           fprintf( out_file, "%-9s %-17s %-9.2f %-9.2f %-9.2f %-13.0f %-10.2f %-11.2f %-10.2f %-9.2f \n", empnum, name, rate, hours, salary, depart, overtime, gross, deduct, net);
    overtime was never initialized and thus will print garbage!

    You don't check if the output file fails to open. Bad!
    fscanf("%s", ...) is unsafe, especially with such small buffers! Read my signature for more information on how to fix this.

    Code:
    grossTotal = gross+gross+gross+gross+gross;
    Do you not know multiplication?
    Code:
    grossTotal = gross * 5;
    Also work a little on indentation.
    It should be consistent.
    Code:
    if (something)
    {
    	/* something */
    }
        if (something)
    That's an example of bad indentation. The second if should be at the same level as the first if. Why? Because the indentation tells us which block it belongs to. So code that belongs to the first if should be one more tab than the if itself. That tells me the code belongs to the if.
    And keep track of all { and }. They should be on the same level as well.
    Code:
    /* Good */
    while(something) {
    }
    
    /* Good */
    while(something)
    {
    }
    
    /* Bad */
    while(something)
    {
    	}
    
    /* Bad */
    while(something) {
    	}
    It makes wonders in reading the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Oh Realy?
    That was a bit much for me with my lack of understanding.

    I am guessing my code is bad by all of that. I understand some of what you said but not all.

    Your compiler you should alert you on this even if warnings are not max. To make the numbers float you should do:
    My compiler does not alert me of anything but syntax errors. (DEVC++) Now i am guessing my copiler is bad also.

    I am going to read what you said a little more and try to figure it out. Now i go from one thing i cant figure out to trying to figure out anther. Man this is some confusing stuff but I will get it>

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kcward View Post
    Oh Realy?
    That was a bit much for me with my lack of understanding.

    I am guessing my code is bad by all of that. I understand some of what you said but not all.
    Perhaps you should ask for clarification of what you don't understand.

    My compiler does not alert me of anything but syntax errors. (DEVC++) Now i am guessing my copiler is bad also.
    Probably because you can't use it right. Try adding -Wall at the command for one. It enables warnings.
    But then, C is such a poor type-safe language. It allows the most horrible things and conversions to be done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Try adding -Wall at the command for one. It enables warnings
    .

    i dont understand that

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is there some doc for the compiler? Then read it.
    I don't use the compiler because I don't like it, so I don't know it very well.
    Last edited by Elysia; 12-16-2007 at 10:58 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i dont understand that
    It applies more if you are running the compiler from the command line. Dev-C++ uses the MinGW port of gcc as its compiler by default, and the command line option to enable all warnings on gcc is -Wall.

    I have forgotten the user interface for Dev-C++, but try searching project options for the option to enable warnings, then set it to the highest level.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    ok well i looked at it. my morning or trying to successfully write some code just got real confusing.
    I went from trying to insert a line on my output file to..........i dont even know. Evertime I ask a question to someone who know about programming and by there responce makes me wonder about my schooling. I guess they should have had a class on how to use the stupid compiler, settings and all the other important stuff before having me open it and try and type in code. I have only been in programming for about three weeks and i am as lost as ever. I spend alot of money for school and it makes me wonder if i getting all the imformation needed.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Programming classes are hard and there are many compilers out there, so specifically picking one might be difficult.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    just wondering. What compiler do you recommend?
    i went into the help on my compilier to find out how to displays warnings
    i set it and it still compiles my code and no errors are found. judging by your first reply there is alot.
    as for it being hard. not real worried about that. thats what keeps me going. its the agravation of trying to figure out stuff i dont understand.

    i would like to know how to brake into my fprintf and total the gross for that department before scaning the next department. and do the same for each department.
    Last edited by kcward; 12-16-2007 at 11:41 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I use Visual Studio 2008. The Express version is free, so you can get it if you want. Set warnings to max in the compiler and I get all those warnings.
    Btw, for C, I really recommend setting "Treat errors as warnings" because C is very un-type safe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What compiler do you recommend?
    The compiler that you are using is okay, assuming you downloaded Dev-C++ 4.9.9.2. Other good options include MSVC8, which is included in Microsoft Visual Studio 2005 Express. The compiler from the 2008 version of Visual Studio should be good too.

    i went into the help on my compilier to find out how to displays warnings
    i set it and it still compiles my code and no errors are found. judging by your first reply there is alot.
    Your compiler should have emitted these warnings:
    Code:
    test.c: In function `main':
    test.c:45: warning: suggest parentheses around assignment used as truth value
    test.c:8: warning: unused variable `payfile'
    test.c:8: warning: unused variable `greatestNet'
    test.c:8: warning: unused variable `leastNet'
    test.c:9: warning: unused variable `grossTotal1'
    test.c:9: warning: unused variable `netTotal'
    test.c:9: warning: unused variable `overTotal'
    test.c:9: warning: unused variable `avgGross'
    These are converting double to float, which is a bad thing. Those numbers are considered double, not float. Your compiler you should alert you on this even if warnings are not max.
    Actually, those double literals are within the range of a float, so it did not matter in this case since there surely will be no loss of precision.
    Last edited by laserlight; 12-16-2007 at 12:01 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In Dev-C++:
    To add flags to the compiler (such as -Wall), go to Tools -> Compiler Options. You'll see "Add the following commands when calling compiler:". Put the flags there, and make sure the box is checked.

    Under Tools -> Editor Options, make sure "Auto Indent" is checked and "Smart Tabs" is not checked (smart tabs is what keeps your closing } from properly indenting).

    The only bad thing I can think of about "0.49" instead of "0.49f" is that the compiler is theoretically obligated to cast the expression into double, perform the multiply there, and then cast the result back to float. Nowadays, that's not that much of a problem. As mentioned, though, you should really make those magic numbers constants instead. And why are you computing everything twice?

    Code:
                 grossTotal = gross+gross+gross+gross+gross;
    I'm guessing you think this line adds up the five previously computed values of gross. Nope! Variables have no history; if you want to add things up like this, you have to add the current value into the total before you compute the next one. So, inside the loop, you would add the current value of gross into grossTotal, and then at the end of the loop the total is there waiting. (Make sure you initialize grossTotal, of course.)

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    The only bad thing I can think of about "0.49" instead of "0.49f" is that the compiler is theoretically obligated to cast the expression into double, perform the multiply there, and then cast the result back to float. Nowadays, that's not that much of a problem. As mentioned, though, you should really make those magic numbers constants instead. And why are you computing everything twice?
    It will get rid of warnings, at least. And warnings are bad things, especially in C. And since I do compile with Treat Warnings as Errors in C, I'm obligated to fix that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed