Having problems with arrays

This is a discussion on Having problems with arrays within the C Programming forums, part of the General Programming Boards category; I wrote a program that calculates the weekly pay for five people for my C programming class, but I'm having ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Having problems with arrays

    I wrote a program that calculates the weekly pay for five people for my C programming class, but I'm having trouble with it when I try to compile and debug it. At first it would prompt me for the name, hours worked, and hourly rate, but would output $0 for everything. And now, (I don't know what I did), it won't even execute the program. Instead, I'll just get this error message: An unhandled exception of type 'System.AccessViolationException' occurred in hourlyratearray.exe

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


    Below is the code for my program. What am I doing wrong?

    Thanks in advance!


    Code:
    #include <stdio.h>
    #include <string.h> 
    
    
    int main(void)
    {
    	int i;
    	double hourlyRate[5], hoursWorked[5], taxesOwed[5], overTime[5], netPay[5], grossPay[5], basePay[5];
    	char name[5];
    
    	// gathers info
    	for (i=0; i<5; i++)
    	{
    		
    		printf("Enter name.\n");
    		scanf("%s", &name[i]);
    		printf("What is their hourly rate?\n");
    		scanf("%f", &hourlyRate[i]);
    		printf("How many hours did they work this week?\n");
    		scanf("%f", &hoursWorked[i]);
    
    		// processes input
    		if( name[i] == -1 || hourlyRate[i] == -1 || hoursWorked[i] == -1)
    		{
    			printf("please reenter a nonnegative number");
    			break;
    		}
    
    		if(hoursWorked[i] > 40)
    		{
    			basePay[i] = hourlyRate[i] * hoursWorked[i];
    			overTime[i] = (hoursWorked[i] - 40) * 1.5 * hourlyRate[i];
    			taxesOwed[i] = ((hourlyRate[i]*40) + overTime[i])*.2;
    			netPay[i] = (hourlyRate[i]*40) + overTime[i] - taxesOwed[i];
    					printf("%s: \n", name[i]);
    			printf("Hours worked: %f\n", hoursWorked[i]);
    			printf("Hourly rate: $%.2f\n", hourlyRate[i]);
    			printf("Base pay: $%.2f\n", basePay[i]);
    			printf("Overtime pay: $%.2f\n", overTime[i]);
    			printf("Taxes paid: $%.2f\n", taxesOwed[i]);
    			printf("Net pay: $%.2f\n", netPay[i]);
    
    
    		}
    		else
    		{
    			basePay[i] = hourlyRate[i] * hoursWorked[i];
    			taxesOwed[i] = (hourlyRate[i]*40) * .2;
    			netPay[i] = (hourlyRate[i]*40) - taxesOwed[i];
    			printf("%s: \n", name[i]);
    			printf("Hours worked: %f\n", hoursWorked[i]);
    			printf("Hourly rate: $%.2f\n", hourlyRate[i]);
    			printf("Base pay: $%.2f\n", basePay[i]);
    			printf("Taxes paid: $%.2f\n", taxesOwed[i]);
    			printf("Net pay: $%.2f\n", netPay[i]);
    		}
    
    
    	}
    
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (1) You only have one name "string" which is five characters long. If you want five names (and it appears that you do), you could maybe do char name[5][80].
    (2) You need to scanf a %lf to get a double.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char name[5];
    This stores one 4 character name. If you want to store five names, you need a two dimensional array:
    char name[5][30];

    > scanf("%s", &name[i]);
    And this would be:
    scanf("%s", name[i]);

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    It works now!! Thank you SO much!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with arrays
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 08:21 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  4. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 03:13 PM
  5. Help!!! Problems with arrays.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-08-2002, 09:21 PM

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