Thread: Arrays and Functions

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    But it DID work before when I had it all written in "main", so that can't be my problem.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what compiler are you using. I compiled and ran it with VC++ 6.0 and it worked ok for me. I changed the filename in the code below, so you will have to reset it if you copy this code.

    If this doesn't work for you either, scatter some debug print() statements around so that you can trace the program's execution. Or learn to use a good debugger.

    Code:
    /*This program will input data into two parallel arrays from scores.txt.
     *It will them print out the student's score according to their ID number.
     *
     *Input:
     *Student Number
     *
     *Outputs:
     *Student Number
     *Mark of Student
     */
     
    #include <stdio.h>
    #define MAXIMUM 100
    #define SENTINEL -99
    
    /*Function Prototypes*/
    int getData(int iId[], int Score[]);
    int searchArray (const int iArray[], int iNum, int iTarget);
    
    int main (void)
    {
    	int iId[MAXIMUM], iScore[MAXIMUM];     /*Arrays*/
    	int iNum, iTarget, iPosition; /*Array Parameters*/
    	
    	/*Call Function to get data*/
    	iNum = getData(iId, iScore);
    
    	if (iNum > MAXIMUM)
    	{
    		printf("Too much input data!\n");
    		return (-1);
    	}
    
    	printf("Enter a student number (-99 to quit): ");
    	scanf("%d", &iTarget);
    	while (iTarget != SENTINEL)
    	{
    		iPosition = searchArray (iId, iNum, iTarget);
    		if (iPosition == -1)
    		{
    			printf ("Not in Array!\n");
    			return(-1);
    		}
    
    		printf("Student number: %d\n", iId[iPosition]);
    		printf("Mark:           %d\n\n", iScore[iPosition]);
    		printf("Enter a student number (or -99 to quit): ");
    		scanf("%d", &iTarget);
    	}
    
    
    
    	return (0);
    }
    
    
    
    
    /*****FUNCTIONS*****/
    
    
    
    
    /*Function to input the data from the file into the arrays and print the data*/
    int getData(int iId[], int iScore[])
    {
        int iStatus, iNum;
    	FILE *inp;
    	
    	inp = fopen("scores.txt", "r");
    	
    	if (inp == NULL)
    	{
    		printf("Unable to open file!\n");
    		return (-1);
    	}
    		
    	iNum = 0;
    	iStatus = fscanf(inp, "%d %d", &iId[0], &iScore[0]);
    	while (iStatus == 2  && iNum < MAXIMUM)
    	{
    		iNum++;
    		iStatus = fscanf(inp, "%d %d", &iId[iNum], &iScore[iNum]);
    	}
    	
            fclose(inp); /*close the file*/
            return (iNum); /*return number of ints read*/
    }
    
    
    
    
    
    /*Function to search array for student #*/
    int searchArray (const int iId[], int iNum, int iTarget)
    {
    	int iFound, iPosition;
    	
    	/*Assume target has not been found*/
    	iFound = 0;
    	/*Loop until either target is found or we are at end of data*/
    	iPosition = 0;
    	while (iFound == 0 && iPosition < iNum)
    	{
    		if (iId [iPosition] == iTarget)
    		{
    			iFound = 1;
    		}
    		else
    		{
    			iPosition++;
    		}
    	}
    	if (iFound == 0)
    	{
    		return (-1);
    	}
    	else
    	{
    		return (iPosition);
    	}
    
    }
    Last edited by Ancient Dragon; 11-03-2005 at 01:37 PM.

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    I am using the exact same compiler. Your code is also exactly the way I have it.

  4. #19
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    then compile for debug, set a breakpoint at the beginning of main(), then step through the code to see what is happening. I'm running it on XP with the data file in the program's project directory (as I posted). Maybe your data file is not where you think it is???

  5. #20
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    Thanks I'll try what you said...but i have it set up so that if my file is non-existant, it will notify me.
    Code:
    if (inp == NULL)
    	{
    		printf("Unable to open file!\n");
    		return (-1);
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM