Thread: trouble with simple arrays

  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
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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 03: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
    4,513
    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,815
    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.
    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, Buttercup, 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, 01:12 PM
  2. Arrays, pointers, trouble..
    By Aino in forum C Programming
    Replies: 2
    Last Post: 08-05-2010, 01:27 PM
  3. Trouble with Arrays
    By blurx in forum C Programming
    Replies: 4
    Last Post: 10-08-2009, 08: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, 08:11 PM