Like Tree1Likes
  • 1 Post By grumpy

trouble with simple arrays

This is a discussion on trouble with simple arrays within the C Programming forums, part of the General Programming Boards category; I have this assignment that I thought would be super easy, but for some reason I can't get it to ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58

    trouble with simple arrays

    I have this assignment that I thought would be super easy, but for some reason I can't get it to work, can you give me some insight?
    Code:
    #include <stdio.h>
    
    int main ()
    {
    char fName[12];
    char lName[12];
    float payRate[12];
    float hours[12];
    float salary[12];
    int empNbr;
    int i, n;
    int a = 0;
    int b = 0;
    
    printf("Enter number of Employees");
    scanf("%d", &empNbr);
    
     for (i= 0; i < empNbr + 1; i++)
     {printf("Enter Employee's First Name:");
     scanf("%c\n", &fName[a]);
         printf("Enter Employee's Last Name:");
         scanf("%c\n", &lName[a]);
             printf("Enter pay rate:\n");
             scanf("%f\n", &payRate[a]);
                 printf("Enter hours worked:\n");
                 scanf("%f\n", &hours[a]);
                 /*     salary[a] = payRate[a] * hours[a];
                        a++;
    */
     }//for1
    
     for (n = 0; n < empNbr + 1; n++)
     {
       printf("\t First Name  \t Last Name \t Pay Rate \t Hours  \n");
       printf("\t fName[b] \t lName[b] \t payRate[b] \t hours[b] \n");
       b++;
    
     }//for2
    
     return 0;
    }//main

  2. #2
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    it compiles, it asks for the first name, last name and prints the next 3 lines and then asks for the first name again.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    it asks for the number of employees and I can enter it.
    it then asks for the first name, I enter it
    it asks for the last name and when I enter it, it prints "what is payrate, what is hours, what is first name:
    all without allowing me to enter pay rate or hours.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    It probably has something to do with you telling scanf() to read a single character when the data is actually a person's name.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    ahh, good catch, I changed it to a string but it didn't fix my problem. Also, I fixed the printf on the display portion \t %s \t %s instead of \t fName[b].
    Last edited by time4f5; 07-11-2011 at 04:09 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    any thoughts on why this program is only asking for inputs on the name arrays and skips the floats?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Can you re-post the code to dhow us how you implemented this?

  9. #9
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    Code:
    #include <stdio.h>
    
    int main ()
    {
    char fName[12];
    char lName[12];
    float payRate[12];
    float hours[12];
    float salary[12];
    int empNbr;
    int i, n;
    int a = 0;
    int b = 0;
    
    printf("Enter number of Employees");
    scanf("%d", &empNbr);
    
     for (i= 0; i < empNbr + 1; i++)
     {printf("Enter Employee's First Name:");
     scanf("%s\n", &fName[a]);
         printf("Enter Employee's Last Name:");
         scanf("%s\n", &lName[a]);
             printf("Enter pay rate:\n");
             scanf("%f\n", &payRate[a]);
                 printf("Enter hours worked:\n");
                 scanf("%f\n", &hours[a]);
                    salary[a] = payRate[a] * hours[a];
                        a++;
    
     }//for1
    
     for (n = 0; n < empNbr + 1; n++)
     {
       printf("\t First Name  \t Last Name \t Pay Rate \t Hours \t  \n");
       printf("\t %s \t %s \t %2f \t %2f \t %2f \n", fName[b], lName[b], payRate[b], hours[b], salary[b]);
       b++;
    
     }//for2
    
     return 0;
    }//main

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Remove the \n from your scanf() statements...

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    In addition to removing the \n from scanf statements, make sure you are reading to a string rather than a single character. scanf("%s", fName) not scanf("%s", &fName[a]); The %s format specifier does not turn a character into a string.

    If you want to be able to read and later print out multiple strings, the char arrays need to be two-dimensional, not one dimensional.

    An array of 12 char does not hold 12 strings.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble multiplying these arrays...
    By UCFuser in forum C Programming
    Replies: 3
    Last Post: 03-30-2011, 02:12 PM
  2. Arrays, pointers, trouble..
    By Aino in forum C Programming
    Replies: 2
    Last Post: 08-05-2010, 02:27 PM
  3. Trouble with Arrays
    By blurx in forum C Programming
    Replies: 4
    Last Post: 10-08-2009, 09:25 PM
  4. trouble assigning arrays
    By shintaro in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2008, 08:31 AM
  5. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 09:11 PM

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