Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [C] - Simple problem I assume - displaying one output
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
INFERNO2K




PostPosted: Wed Nov 10, 2004 5:05 pm   Post subject: [C] - Simple problem I assume - displaying one output

code:
/* Defined Libraries */

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

/* Functions outside of Main () */

char another(void) /* Loops the program by asking a question */
{
        char more;
        system("cls");
        printf("*** Do you wish to continue? (Y/N) ***  ");
        fflush (stdin);
        scanf("%c", &more);     
        printf("\n");
        return more;
}


int main (void)
{

        int          hours_worked,
                        option,
                        x,
                        y=0,
                        more = 'y',
                        count = 0;
                       
        double grosspay,
                        payrate[5],
                        holdpay[5];
                       
        char    fname[11],
                        lname[11],
                        holdname[5][22],
                        fullname[22];
                       
       
while (more =='Y' || more =='y')
{
        system("cls");
       
        for (x=0; x<5; x++)
        {
                printf("Enter payrate for option %d: ", x + 1);
                scanf("%lf", &payrate[x]);
        }

                printf("\n\n");
        do{     
                for(count=0; count<5; count++)
                {
                printf("\n\n");
                printf("First Name: ");
                scanf("%10s", fname);
                printf("Last Name: ");
                fflush(stdin);
                scanf("%10s", lname);
                printf("Hours worked: ");
                scanf("%d", &hours_worked);
                printf("Payrate option (#): ");
                scanf("%d", &option);
               
                grosspay = hours_worked * payrate[option-1];       
               
                strcpy(fullname,fname);
                strcat(fullname, " ");
                strcat(fullname, lname);
                strcpy(holdname[y], fullname);
                holdpay[y] = grosspay;
                }
                }while(x > 5);
                       
                /*holdname[y][x] = fullname[x];
                       
                /* Summary Information */
       
                more = another();
               
                x = 0;
                system("cls");
                printf("Weekly Gross pay for ABC Company");
                printf("\n\n");
       
                printf("Name           Gross pay");
                printf("\n");
                printf("%s    %10.2lf", holdname[x], holdpay[x]);
                printf("\n\n");

        }
}


My program is a payroll system. It asks the user to enter 5 payrates to be stored in an array.

Than five times, it asks the user for their name, last name, hours worked and which payrate option they are.

I want the program to display all five people in the output at the end. For now it is only displaying the last person I entered. I need it to display all five.

"Weekly Gross pay for ABC Company"

Name Grosspay
First Last xxx.xx
First Last xxx.xx
First Last xxx.xx
First Last xxx.xx
First Last xxx.xx

I am using minGW compiler for Windows.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Nov 10, 2004 5:27 pm   Post subject: (No subject)

Consider:

code:
printf("First Name: ");
scanf("%10s", fname);


You're scanning into the first element in the array each time.

Instead, try:

code:
printf("First Name: ");
scanf("%10s", fname[count]);
INFERNO2K




PostPosted: Wed Nov 10, 2004 5:30 pm   Post subject: (No subject)

That just stops the program after I enter the first name.

Sad
wtd




PostPosted: Wed Nov 10, 2004 5:37 pm   Post subject: (No subject)

I think the best advice I can give you (aside from writing the whole darn thing myself), is to write part of it, get that to work, then move on. Test often!

That's pretty much what they're gonbna tell you at Ars too.
INFERNO2K




PostPosted: Wed Nov 10, 2004 5:44 pm   Post subject: (No subject)

As you can see I've written the entire program.

I am confused on what I need to do fix it.
wtd




PostPosted: Wed Nov 10, 2004 5:58 pm   Post subject: (No subject)

INFERNO2K wrote:
As you can see I've written the entire program.


Yeah. Kinda your problem. When you program you need to do it in steps. Lots of small steps. If you know one thing is working fine, you can move onto the next piece without worrying about the one you've tested.

If you try to write the whole program, then test, you have a lot more to debug.

To me, if I had to identify a trouble spot, I'd say it's your arrays. Learning more about dealing with arrays will likely help you.
wtd




PostPosted: Wed Nov 10, 2004 6:10 pm   Post subject: (No subject)

One other quick suggestion. Your "another" function asks the user if they want to enter another name, then returns their input.

Really, there are only two answers. Yes or no. This is the perfect scenario for a boolean value.

C doesn't have a boolean type. Zero is false, and anything else is true. The "stdbool.h" header gives us some syntactic sugar for this, though.

code:
#include <stdbool.h>

bool another()
{
   char more;
   system("cls");
   printf("*** Do you wish to continue? (Y/N) ***   ");
   fflush (stdin);
   scanf("%c", &more);   
   printf("\n");

   return (more == 'y' || more == 'Y');
}


Now, when you want to test, instead of writing out:

code:
char more = another();
if (more == 'y' || more == 'Y') { /* yada yada */ }


You could simpy write:

code:
bool more = another();
if (more) {  /* yada yada */ }
md




PostPosted: Mon Nov 15, 2004 12:32 pm   Post subject: Re: [C] - Simple problem I assume - displaying one output

Here's part of the main function; only better formated and commented
code:

while (more =='Y' || more =='y')                                  // looks good
{
        system("cls");                                                     // clear the screen
   
        for (x=0; x<5; x++)            // for x = 0 to 4
        {
          printf("Enter payrate for option %d: ", x + 1);   // ask for a pay rate
          scanf("%lf", &payrate[x]);                                // get said pay rate
        }

        // ASSERT: x == 5

        printf("\n\n");                                                // print a couple of lines
   
        do                                                         // start a loop (I have no idea why this is here...)
        {   
           for(count=0; count<5; count++)               // for count = 0 to 4
          {
            printf("\n\n");               // blank lines
            printf("First Name: ");              // first name
            scanf("%10s", fname);                // ...
            printf("Last Name: ");                // last name
            fflush(stdin);                         // why is this here?
            scanf("%10s", lname);                 // ...
            printf("Hours worked: ");     // hours worked
            scanf("%d", &hours_worked);             // ...
            printf("Payrate option (#): ");        // pay rate
            scanf("%d", &option);                  // ...
     
            grosspay = hours_worked * payrate[option-1]; // calclulate gross pay     
     
            strcpy(fullname,fname);         // concatenate the first name and last name to maek a full nane
            strcat(fullname, " ");                // ...
            strcat(fullname, lname);     // ...
            strcpy(holdname[y], fullname);         // copy the full name to an array that stores full names       using index y
            holdpay[y] = grosspay;            // copy gross pay to an array that holds gross pay    using index y
                        // ASSERT: y == 0
          } 
                                       
      } while(x > 5);                        // do this while x > 5 (where did x change?); ASSERT: x == 5
         
      /*holdname[y][x] = fullname[x];             // I have no idea why this is here, i assume it's old code
                                                                       // that should be deleted
      /* Summary Information */
   
      more = another();          // ask if they wish to continue (not sure why, as you will anyway...)
     
      x = 0;                                      // set x to 0

      system("cls");                               // clear the screen
      printf("Weekly Gross pay for ABC Company");    // write a header
      printf("\n\n");                          // some blank lines
   
      printf("Name         Gross pay");    // another header
      printf("\n");                                       // a blank line
      printf("%s    %10.2lf", holdname[x], holdpay[x]);  // the name and pay of employee x (where x == 0)
      printf("\n\n");                          // some more blank lines

   }    // end of while (more =='Y' || more =='y')
} // end of function



Problems:

#1 you declare y as being an int... even though you always treat it as a char...
#2 what is the point of the do {} while loop? it is garunteed to only execute once, and it's written to do so; the loop is pointless
#3 asking if the user wishes to proceed, and then doing so anyway is pointless, perhaps if you wished to ask if they wished to enter more employees?
(note you would have to store these employees as well...)
#4 you say that the program only outputs the information for the last person, given that you always store an employees information at index 0, and then you
only output the employee at index 0 (do you see a loop?)...

To be blunt: your problem is a lack of thought. If you go through your code, and comment it it's fairly easy to see where errors like this are.
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Mon Nov 15, 2004 3:50 pm   Post subject: (No subject)

couldnt have said it better myself
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: