Like Tree2Likes
  • 2 Post By Salem

Need help in Inputs and outputs

This is a discussion on Need help in Inputs and outputs within the C Programming forums, part of the General Programming Boards category; Hello guys, I'm new to programming and found this site to be helpful. I'm need a little help understanding something ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    Need help in Inputs and outputs

    Hello guys, I'm new to programming and found this site to be helpful. I'm need a little help understanding something about C which i can't seem to find the right answer. This is not write a code for my homework but it is a guide me with my homework. So my assignment is

    Write a program to read the employee file and create a payroll register. The register will contain the following data:

    A. Employee number (print left justified)
    B. Department
    C. Pay rate
    D. Exempt
    E. Hours worked
    F. Base pay (pay rate * hours worked)

    Employee No. Department Pay rate Exempt Hours worked
    0101 41 8.11 Y 49
    0722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45

    What i don't understand is how i write that in note pad so C would read it. For example I write a code to tell the user to enter the employee number
    printf("Enter employee number\n");
    what is the next step to grab the department number and pay rate ext. form notepad. Thats the part that confuses me.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    1. There is less clarity on what is the content of your input file and output file. Please explain separately.
    2. You should consider, Is your input file changes dynamicaly or it will be static for process lifetime?
    3. You can take help of fopen(), fgets(), strcmp(), strtok().
    4. You can use array and it technique of writing and reading from index. It will help you to search.

    And please ask specifically what you need. logic of it or complete code
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Hate to do it but... Let me google that for you

    Did that help? Perhaps you weren't sure what to search for originaly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User Sokion's Avatar
    Join Date
    Jun 2012
    Posts
    26
    Can I ask a question please? (I shalt not ask for assignment help)

    I am working on a similar project, and I want the file to retain its Input after the program terminates, I am working on a Binary file, and every time I exit the program normally and reopen it, I cannot find the info in the file.

    I compiled the whole thing in Dos, and even reinstalled a 32bit windows 7 and the problem remains.

    Would you look at the code? (I am using a data structure to hold the whole thing together, basically three large size character arrays)...

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    This is my code so far. now im having issue with writing it in the notepad and opening it. Im not sure how im suppose to write it on notepad to open it and read it properly.
    Code:
    // Creates a Payroll Register from an Employee File Saved on the Computer
    
    
    
    
     
    #include <stdio.h>
    #include <stdlib.h>
     
    //Function Declarations
        int getEmpData      (FILE* spEmpDataIn,
                             int* empNum, int* dept, float* rate, char* exempt, int* hrs);
        int writeEmpData    (FILE* spEmpDataOut, 
                             int empNum,  int dept,  float rate,  char exempt,  int hrs, 
                             float basePay);
        void calcPay        (float rate,  int hrs,  float* basePay);    
     
    int main (void)
    {
     
    //Local Declarations
        FILE* spEmpDataIn;
        FILE* spEmpDataOut;
         
        int   empNum;
        int   dept;
        float rate;
        char  exempt;
        int   hrs;
     
        float basePay;
     
    //Statements
        printf("Creating Payroll Register...\n");
        if (!(spEmpDataIn = fopen ("C:\\Employee.txt", "r")))
            {
             printf("Could not open file\a\n");
             return 100;
            } //if open input
         
        if (!(spEmpDataOut = fopen ("C:\\PayrollRegister.txt", "w")))
            {
             printf("Error Opening Payroll Register\a\n");
             return 102;
            } //if open output
     
        while (getEmpData
              (spEmpDataIn, &empNum, &dept, &rate, &exempt, &hrs))
            {
             calcPay      (rate, hrs, &basePay);
             writeEmpData (spEmpDataOut, empNum, dept, rate, exempt, hrs, basePay);
            } //while
     
        fclose (spEmpDataIn);
        fclose (spEmpDataOut);
     
        printf("Payroll Register Completed\n");
        return 0;
    } //main
     
    /*=====================getEmpData=====================
        Reads data from the employee file.
           Pre   spEmpDataIn is an open file
                 empNum, dept, rate, exempt, hrs pass by address
           Post  reads employee information
                 if data read   --returns 1
                 if EOF or error--returns 0
    */
    int getEmpData (FILE* spEmpDataIn, int* empNum,  int* dept, 
                    float* rate,       char* exempt, int* hrs)
    {
    //Local Declarations
        int ioResult;
     
    //Statements
        ioResult = fscanf(spEmpDataIn, "%4d%2d%4f%1c%2d", &empNum, &dept, &rate, &exempt, &hrs);
        if(ioResult == EOF)
            return 0;
        else
            return 1;
    } //getEmpData
     
    /*===========================calcPay===========================
        Determines base pay based on the employees pay rate and hours worked.
            Pre     rate and hrs contain employee pay rate and hours worked
                    basePay is the adress of the variable
            Post    Base Pay is copied to the address
    */
    void calcPay (float rate, int hrs, float* basePay)
    {
    //Statements
        *basePay = rate * hrs;
        return;
    } //calcPay
     
    /* =======================writeEmpData=======================
        Write payroll register to output file.
            Pre     spEmpDataOut is an open file
                    empNum,dpt, rate, exempt, hrs, and basePay have values to write
            Post    Data written to file
    */
    int writeEmpData (FILE* spEmpDataOut, int empNum,  int dept, 
                      float rate,         char exempt, int hrs, 
                      float basePay)
    {
    //Statements
        fprintf(spEmpDataOut, "%-4d %d %f %c %d %f\n",
                               empNum, dept, rate, exempt, hrs, basePay);
        return 0;
    } //writeEmpData

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Copy this:

    Code:
    Employee No. Department Pay rate Exempt Hours worked
    0101 41 8.11 Y 49
    0722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45
    except for the first line into the text file you want to read.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    the only problem im having is that its not reading the text file. so in notepad my my inputs are
    [code]
    0101 41 8.11 Y 49
    0722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45[/cpde]

    saved it as Employee.txt

    it complies but has a blank screen and this is my result from the locals


    Code:
    		rate	-1.0737418e+008	float
    		exempt	-52 ''	char
    -		fpEmpDataIn	0xcccccccc {_ptr=??? _cnt=??? _base=??? ...}	_iobuf *
    		_ptr	CXX0030: Error: expression cannot be evaluated	
    		_cnt	CXX0030: Error: expression cannot be evaluated	
    		_base	CXX0030: Error: expression cannot be evaluated	
    		_flag	CXX0030: Error: expression cannot be evaluated	
    		_file	CXX0030: Error: expression cannot be evaluated	
    		_charbuf	CXX0030: Error: expression cannot be evaluated	
    		_bufsiz	CXX0030: Error: expression cannot be evaluated	
    		_tmpfname	CXX0030: Error: expression cannot be evaluated	
    		empNum	-858993460	int
    +		spEmpDataOut	0xcccccccc {_ptr=??? _cnt=??? _base=??? ...}	_iobuf *
    		dept	-858993460	int
    		basePay	-1.0737418e+008	float
    		hrs	-858993460	int
    cant figure out whats im doing wrong

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    Compile with a compiler that warns you when you screw up printf/scanf calls.

    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘getEmpData’:
    foo.c:76: warning: format ‘%4d’ expects type ‘int *’, but argument 3 has type ‘int **’
    foo.c:76: warning: format ‘%2d’ expects type ‘int *’, but argument 4 has type ‘int **’
    foo.c:76: warning: format ‘%4f’ expects type ‘float *’, but argument 5 has type ‘float **’
    foo.c:76: warning: format ‘%1c’ expects type ‘char *’, but argument 6 has type ‘char **’
    foo.c:76: warning: format ‘%2d’ expects type ‘int *’, but argument 7 has type ‘int **’
    > - fpEmpDataIn 0xcccccccc ...
    In debug builds, uninitialised memory is filled with deliberate patterns rather than left as "garbage"
    Troubleshooting Common Problems with Applications: Debugging in the Real World
    Quote Originally Posted by msdn
    Table 1. Potential patterns
    Pattern Description
    0xFDFDFDFD No man's land (normally outside of a process)
    0xDDDDDDDD Freed memory
    0xCDCDCDCD Uninitialized (global)
    0xCCCCCCCC Uninitialized locals (on the stack)
    So whenever you see any of these patterns, you know you're doing something with data you haven't set up properly.
    stahta01 and Sokion like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating seperate functions for inputs, outputs and calculations?
    By toby2604HASSELB in forum C Programming
    Replies: 3
    Last Post: 10-21-2010, 03:26 AM
  2. Different outputs on different OS
    By darksifer in forum C Programming
    Replies: 13
    Last Post: 10-19-2010, 02:45 PM
  3. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 05:28 AM
  4. Binary Inputs and Outputs *Files Questions
    By nicz888 in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 03:17 AM
  5. Outputs
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2002, 07:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21