2

I am trying to make a program in the C language that can take in employee information in array struct (not parallel) form and output it into a text file. The program will compare the job code and calculate the commission rate of the employee. I have 2 questions:

1 - the commission of the salesPerson is always 0 even when I enter one of TEL, SAL, SSR. Is it a problem with my string comparison?

2 - how do I output my results into a text file (inside the project folder)?

Edit - I have figured out how to print my file into a txt. Thank you all for your help!

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>

//job codes
char TEL[4] = "TEL";
char SAL[4] = "SAL";
char SSR[4] = "SSR";

//create a sales person structure
struct salesPerson
{
    char name[31];
    int salesID;
    char jobCode[4];
    double totalSales;
    double commission;
}e[10]; //make an array of 10 employees

int _tmain(int argc, _TCHAR* argv[])
{
    //get employee info
    for(int i=0; i<3; i++)
    {
        printf("\nEnter the sales person's name: ");
        scanf(" %s", &e[i].name);
        printf("\nEnter the sales person's ID: \n");
        scanf(" %d%*c", &e[i].salesID);
        printf("\nEnter the sales person's job code: \n");
        scanf(" %s", &e[i].jobCode);
        printf("\nEnter the total sales of the sales person: ");
        scanf(" %lf", &e[i].totalSales);

        //determine commission based on jobCode
        if(strcmp(e[i].jobCode, TEL) == 0)
        {
            e[i].commission = e[i].totalSales*0.02;
        }
        if(strcmp(e[i].jobCode, SAL) == 0)
        {
            e[i].commission = e[i].totalSales*0.05;
        }
        if(strcmp(e[i].jobCode, SSR) == 0)
        {
            e[i].commission = e[i].totalSales*0.07;
        }
        else
        {
            printf("\n----------");
        }
    }

    printf("\n%d\n", e[0].commission);
    printf("\n%d\n", e[1].commission);
    printf("\n%d\n", e[2].commission);

    //print stuff to txt file
    FILE *fpOut, *fpIn;
    /*
    if ((fpIn = fopen("c:\\temp\\salesEmployees.txt", "w")) == NULL)
    {
        printf("Error opening the file for processing\n\n");
    }
    else
    {
        fpOut = fopen("c:\\temp\\salesEmployees.txt", "w");
        for(int i=0; i<3; i++)
        {
            //fscanf(fpIn,"%[^\n]", &e[i].name);
            //fscanf(fpIn,"%f%*c", &e[i].salesID);

            fprintf(fpOut, "\nName: %s", e[i].name);
            fprintf(fpOut, "\nSalesID: %d*c ", e[i].salesID);
            fprintf(fpOut, "\nJob code: %s ", e[i].jobCode);
            fprintf(fpOut, "\nTotal sales: %.2lf", e[i].totalSales);
            fprintf(fpOut, "\nCommission earned: %.2lf", e[i].commission);
            fprintf(fpOut, "\n\n");
        }

        //fclose(fpOut);
    }*/

}
4
  • The string comparisons for commission looks right - but you should probably be using else if so that when you fibd the right one you stop checking - that will also prevent it from prining the "\n----------". Commented Nov 20, 2013 at 1:45
  • Your commented out code looks like a good start on outputting to a txt file. What is the problem you're facing, exactly? Commented Nov 20, 2013 at 1:53
  • I commented out the code for printing to txt because it crashes my program, I haven't figured out how to fix it. Also, I was hoping to make the txt file inside of the project folder and still work when I move the project around. Also thx to Floris I fixed my comparison problem Commented Nov 20, 2013 at 2:03
  • I have answered the second question with a second answer... Commented Nov 20, 2013 at 3:19

2 Answers 2

1

Always look at the warnings from your compiler. You have

printf("\n%d\n", e[0].commission);

Where you give an integer format specifier, but the argument is double. The first few bytes of that may well be zero - which is why the result prints as zero. Try changing it to:

printf("\n%.2lf\n", e[0].commission);

And you will get the commission in dollars and cents. Same for the other lines.

Sign up to request clarification or add additional context in comments.

Comments

1

To answer the second part of your question (about program crashing, and keeping file in same directory): you open the same file for input and output without closing in between; not sure why the input is even there. Assuming (for simplicity) that you only want to save to file what was entered from the keyboard, you can just remove all reference to the fpIn. As for the "keep file in same folder" - just use a relative path. When you run from c:\my\dir, then opening a file salesOutput.txt will cause it to be written to c:\my\dir\salesOutput.txt.

Complete and working code (some changes to accommodate my compiler settings...):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//job codes
char TEL[4] = "TEL";
char SAL[4] = "SAL";
char SSR[4] = "SSR";

//create a sales person structure
struct salesPerson
{
    char name[31];
    int salesID;
    char jobCode[4];
    double totalSales;
    double commission;
}e[10]; //make an array of 10 employees

int main(int argc, char* argv[])
{
    //get employee info
    int i;
    for(i=0; i<3; i++)
    {
        printf("\nEnter the sales person's name: ");
        scanf(" %s", &e[i].name);
        printf("\nEnter the sales person's ID: \n");
        scanf(" %d%*c", &e[i].salesID);
        printf("\nEnter the sales person's job code: \n");
        scanf(" %s", &e[i].jobCode);
        printf("\nEnter the total sales of the sales person: ");
        scanf(" %lf", &e[i].totalSales);

        //determine commission based on jobCode
        if(strcmp(e[i].jobCode, TEL) == 0)
        {
            e[i].commission = e[i].totalSales*0.02;
        }
        if(strcmp(e[i].jobCode, SAL) == 0)
        {
            e[i].commission = e[i].totalSales*0.05;
        }
        if(strcmp(e[i].jobCode, SSR) == 0)
        {
            e[i].commission = e[i].totalSales*0.07;
        }
        else
        {
            printf("\n----------");
        }
    }

    printf("\n%lf\n", e[0].commission);
    printf("\n%lf\n", e[1].commission);
    printf("\n%lf\n", e[2].commission);

    //print stuff to txt file
    FILE *fpOut;
    {
        if((fpOut = fopen("salesEmployees.txt", "w")) == NULL)
        {
          printf("Unable to open file - quitting\n");
          return -1;
        };
        int i;
        for(i=0; i<3; i++)
        {
            fprintf(fpOut, "\nName: %s", e[i].name);
            fprintf(fpOut, "\nSalesID: %d*c ", e[i].salesID);
            fprintf(fpOut, "\nJob code: %s ", e[i].jobCode);
            fprintf(fpOut, "\nTotal sales: %.2lf", e[i].totalSales);
            fprintf(fpOut, "\nCommission earned: %.2lf", e[i].commission);
            fprintf(fpOut, "\n\n");
        }
        fclose(fpOut);
    }
}

Obviously it is desirable (and necessary) to add addition I/O, error checking on inputs, etc - but this should get you past the point of "it's not working and I don't know why". I did wonder why you have the *c in the printout of the sales ID - it just gets added to the output - but I decided not to remove it.

Good luck with the coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.