سؤال

So here is my first ultra beginner computer programming question in C.

I need to set it up so that someone can type in their full name on the input. Here is part of the specs -

"You'll have to do a little thinking to figure out how to get the printed names lined up with all the other columns. The first hint is that it involves joining strings together, something called concatenation. Give it a go, and if you can't figure it out, look at the next document in this folder; it contains additional hints. Part of the purpose of this assignment is to implicitly teach you concatenation. Do NOT use tabs (\t) and make sure your C/C++ editor does not produce tab characters.

Do NOT use gets() in this program. Use scanf() for inputting the interactive information. If you try to use gets(), you may get VERY frustrated.

In essence, all numbers appearing in the report should be right-justified and decimal-aligned. All numbers appearing in the summary should appear without leading spaces (other than the one which normally separates the number from the previous word). Hourly wage amounts CAN be less than 10.00, so be very careful with your formatting. The sample output can appear correct, but you can still be docked a half-point if things don't align properly with hourly wages under $10.00." Additional hints:

  • You may assume that the employee name is always two names, a first name and a last name separated by a space. Also assume that there are never any spaces within a first name or within a last name. This allows you to use two scanf() calls instead of one gets() call. gets() would introduce some oddities that make things not work correctly later down the line.

  • You may also assume that neither name exceeds 10 characters in length.

  • The input from the Process another employee? question should be a single character. Assume that N or n will stop the loop, but that any other character will continue the loop.

Anyone know how to do this? When I use gets(which he says not to do), the loop screws up on the 2nd time around and it asks for the name and salary all in one line. And if I try to use 2 scanf statements, I get a crash or only 1 of the names input.

I was thinking the only way to do it is by outputting the names to a text file, then reading them in again. But is there some other way? I'm not allowed to ask for the names individually. A user might type a full name with one space, as it says in the specs.

Here is the code I wrote so far. I also need totals for all the gross', overtime hours and regular hours.

//stupid program

#include <stdio.h>
#include <strings.h>
#include <math.h>

//global variables

FILE *reportfile;   //output file
char department[21];
int count;
char name[21];
float hoursworked;
float hourlywage;
float overtimehoursworked;
float overtimehourlywage;
float gross;
char again;
char firstname;
char lastname;
float tothoursworked;
float totovertimehoursworked;
float totgross;

const float overtimerate = 1.5;
const float overtimethreshold = 40; //hours needed to get overtime


//function prototypes

void GetInfo(void);
void Finalreport(void);

//main

int main(void)
{

   reportfile = fopen("c:\\class\\kpaul-pay.txt","w");  //open output file

    //////////////////////////////////////////////////
     //        initialize accumulating variables
     /////////////////////////////////////////////////


     count = 0;
     tothoursworked = 0;
     totovertimehoursworked = 0;
     totgross = 0;

   GetInfo();

   fclose(reportfile);         //close output file




return 0;

}

void GetInfo (void)
{
    printf("Mountain Pacific Corporation\n");
    printf("Department Salary Program\n\n");
    printf("Please enter the name of the department: ");
    gets(department);
    fprintf(reportfile, "Mountain Pacific Corporation\n");
    fprintf(reportfile, "Department Salary Program\n\n");
    fprintf(reportfile, "%s\n\n", department);
    fprintf(reportfile, "Employee                Reg Hrs        Overtime Hrs                      Gross\n");
    fprintf(reportfile, "-----------------------------------------------------------------\n");

    do {


   printf("\nEnter employee #1: ");
   gets(name);

   printf("Enter the hourly wage of %s", name);
   scanf("%f", &hourlywage);

   printf("\nEnter total number of hours: ");
   scanf("%f", &hoursworked);

   if (hoursworked<=overtimethreshold)
    overtimehoursworked = 0;

   else if (hoursworked > overtimethreshold)
   overtimehoursworked = hoursworked - overtimethreshold;

   gross = (hoursworked*hourlywage) + (overtimehoursworked*overtimehourlywage);

       fprintf(reportfile, "%s%16.2f(%4.2f)%12.2f(%4.2f)    $%7.2f", name, hoursworked,     hourlywage, overtimehoursworked, hourlywage * overtimerate, gross);

       tothoursworked = tothoursworked + hoursworked;
       totovertimehoursworked = totovertimehoursworked +overtimehoursworked;
       totgross = totgross + gross;


      printf("\n");
      printf("Would you like another conversion? ");
      scanf ("%s", &again);

      printf("\n\n");

   } while (again!='N' && again!='n');

}
هل كانت مفيدة؟

المحلول

Careful use of the scanf format string is needed. %s indicates a string that is delimited by a white space, in non technical terms, scanf will read characters until it finds a whitespace character such as a tab or space.

This poses a problem for reading multiple words, this can be solved by using multiple %s in the format string, you must provide a seperate array for each word as an argument after the format string e.g.

scanf("%s%s%s", string1, string2, string3)

This scanf call expects three words from the user, where each word is delimited by a white space. Make sure there is allocated memory for each of arrays though otherwise you will write into memory the program does not own normally causing the program to crash. The exact same effect to the code above can be achieved by using scanf multiple times with a single %s in the format string.

If you need everything as one string the same array you can use strcat which will concatenate two strings together. The function is simple to use, but once again make sure your arguments are allocated arrays before calling strcat otherwise it will likely cause a crash or unexpected behaviour.

Here are references to both functions:

scanf - http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

strcat - http://www.cplusplus.com/reference/clibrary/cstring/strcat/

Hope this helps =]

نصائح أخرى

scanf with the format %s will accept a whitespace-delimited string.

The crash is most likely caused by overflowing (or failing to reserve space for) the input string.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top