davidw
davidw

Reputation: 45

How do I get multiple lines to print after ending this logical loop

Me again. The C rookie. I am working on an assignment to write a program that prompts a user to enter info after which the program should list back out the data that was entered. My code only prints the last record info that is entered.

For example, I enter the following info for employee #1 "Rookie Coder" as the first employee name and enter 25 and 40 for hourly wage and hours worked, respectively. Then, I enter the following info for employee #2 "Slow Learner" as the 2nd employee name and enter 20 and 45 for hourly wage and hours worked, respectively. The program only prints the info related to "Slow Learner". But I want to print out the info for both records entered.

Can someone please offer guidance on what I'm missing to get both records to print? Thank you from the C Rookie

// C Libraries Used

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

// Constant declerations

const float OTPAYFACTOR = 1.5;
FILE *userinputfile;                  //disk file (for input)

// Variable declerations
char deptname [21];
char firstname [10];
char lastname [10];
char fullname [21];
float hrsworked;
float hrwage;
int count;
char again;

// Function Prototypes


// M A I N   F U N C T I O N

int main (void){
    printf("Mountain Pacific Corporation\nDepartment Salary Program\n\n");
    printf("Please enter the name of the department: ");
    scanf("%s", &deptname);
    count = 0;    // Initialize this "counting" variable to zero to start
    printf("\n");
    count = 0;    // Initialize this "counting" variable to zero to start
    printf("\n");
   do {

      count++; // Increment the counting variable

      printf("Enter employee #%d: ", count);
      scanf("%s %s", &firstname, &lastname);
      strcpy(fullname, firstname);
      strcat(fullname, " ");
      strcat(fullname, lastname);
      printf("Enter the hourly wage of %s: ", fullname);
      scanf("%f", &hrwage);
      printf("Enter total number of hours: ");
      scanf("%f", &hrsworked);

      printf("\nThank you. Process another employee? ");
      scanf ("%s", &again);
      printf("\n");

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

      printf("End of processing.");

    printf("%s, $%0.2f, %0.2f: \n", fullname, hrwage, hrsworked);

return 0;
}

Upvotes: 1

Views: 77

Answers (3)

dbush
dbush

Reputation: 224387

You only have one set of variables that you use on each iteration of the loop. Anything stored the first time through the loop is overwritten the second time through the loop. So you'll only ever store the most recently entered set of values.

You want to declare each of the variables that are taking data as an array, that way you can save multiple values.

char deptname [5][21];
char firstname [5][10];
char lastname [5][10];
char fullname [5][21];
float hrsworked[5];
float hrwage[5];

...

do {

      printf("Enter employee #%d: ", count+1);
      scanf("%s %s", &firstname[count], &lastname[count]);
      strcpy(fullname[count], firstname[count]);
      strcat(fullname[count], " ");
      strcat(fullname[count], lastname[count]);
      printf("Enter the hourly wage of %s: ", fullname[count]);
      scanf("%f", &hrwage[count]);
      printf("Enter total number of hours: ");
      scanf("%f", &hrsworked[count]);

      printf("\nThank you. Process another employee? ");
      scanf ("%s", &again);
      printf("\n");

      count++; // Increment the counting variable

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

Then you need to loop through the array to print each element:

int i;
for (i=0; i<count; i++) {
    printf("%s, $%0.2f, %0.2f: \n", fullname[i], hrwage[i], hrsworked[i]);
}

Upvotes: 1

Achal
Achal

Reputation: 11921

remove the & symbol from below statement because deptname itself is string

scanf("%s", &deptname);

do same from below statements also

  scanf("%s %s", &firstname, &lastname);

you want to print all records but at last there is only one printf so obviously it prints only last employee records

printf("%s, $%0.2f, %0.2f: \n", fullname, hrwage, hrsworked);

if you wants to prints all employee data after giving 'N' option, you should store information before that, my suggestion is put all entries inside structure and then take array of structure and store every time.

Upvotes: 0

oetoni
oetoni

Reputation: 3887

:) naive from your side but don't worry. You are storing only the last data into the fullname, hrwage, hrsworked, so only the last output is saved there. You need a better data structure to store the data as they are inserted by user.

1) If you know how many inputs there will be you can use a predefined array of strings for strings for the name and floats for the hours and wage.

2) if you don't know then you will need an scaling data structure like for example a list in order to put/add/append elements to it ;)

for C Arrays check this and for Lists in C check this

Then last but not least after you fill them with the input you just need to loop with a for / while loop in order to print the array or list.

Hope this will help!

Upvotes: 0

Related Questions