C project

This is a discussion on C project within the C Programming forums, part of the General Programming Boards category; The idea of the project is to read in 5 strings from a file. Then we need to print the ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    C project

    The idea of the project is to read in 5 strings from a file. Then we need to print the array, then sort the array, then allow the user to search in the array for a strings position number.

    Here is what we have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_NUM 25
    #define MAX_SIZE 10
    
    void readFile(FILE* fpIn, int num, char arr[][MAX_SIZE]);
    void printArr(char arr[][MAX_SIZE], int num);
    void bubble(char arr[][MAX_SIZE]);
    void interact(char arr[][MAX_SIZE]);
    int srch(char arr[][MAX_SIZE], int item);
    
    int main(int argc, char* argv[])
    {
    printf("Authors: Tyler Tracey and Andrea Boyd");
    printf("\nId No. : N00767255     :  N00823811");
    printf("\nDate   : April 20th 2012 @ 8 a.m.");
    printf("\nCourse : COP2220\n");
    
    int num = (atoi(argv[2]));
    FILE* fpIn;
    int i;
    
    char arr[MAX_NUM][MAX_SIZE];
    
    readFile(fpIn, num, arr);
    printArr(arr, num);
    bubble(arr);
    interact(arr);
    
    return 0;
    
    }
    
    void readFile(FILE* fpIn, int num, char arr[][MAX_SIZE])
    {
    int i;
    
    while((fpIn = fopen("names", "r")) != NULL)
       {
           for(i = 0; i < num; i++)
              {
                 fscanf(fpIn, "%s", &arr[MAX_NUM][i]);
              }
       }
    
       fclose(fpIn);
    
    }
    
    void printArr(char arr[MAX_NUM][MAX_SIZE], int num)
    {
    int i;
    
    for( i = 0; i < num; i++ )
    {
        printf("%s : %s \n", arr[i][0], arr[i][1] );
    }
    
    
    
    }
    
    void bubble(char arr[MAX_NUM][MAX_SIZE])
    {
    
    }
    
    void interact(char arr[MAX_NUM][MAX_SIZE])
    {
    int item = 0;
    srch(arr, item);
    }
    
    int srch(char arr[MAX_NUM][MAX_SIZE], int item)
    {
    
    }
    The problem is our program is crashing after the initial print of our names so we cannot see if the other things we change are working. Please help ASAP! This is due in about 9 hours!!

    We are using a linux-based system to run the file so that cmdline is: ./a.out infile 5
    Last edited by Ellondu; 04-19-2012 at 09:41 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > fscanf(fpIn, "%s", &arr[MAX_NUM][i]);
    You're writing way past the end of your array

    Replace this with just
    fscanf(fpIn, "%s", arr[i] );

    > printf("%s : %s \n", arr[i][0], arr[i][1] );
    arr[i] is a string
    arr[i][0] is a char
    You can't print chars with %s

    If you're compiling with gcc, then add these options
    -W -Wall -Wextra
    and it will tell you when you're making a mess of printf/scanf calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    In your readFile function you are accessing your array out of bounds. In the following snippet.
    Code:
    while((fpIn = fopen("names", "r")) != NULL)
       {
           for(i = 0; i < num; i++)
              {
                 fscanf(fpIn, "%s", &arr[MAX_NUM][i]);
    You have defined arr as arr[MAX_NUM][MAX_SIZE], so you can only access MAX_NUM - 1 and MAX_SIZE - 1 elements. So using arr[MAX_NUM] is out of bounds. Remember arrays in C start at 0 and end at size -1.



    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes Project (Not class project)
    By adam.morin in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2011, 01:48 AM
  2. Paid project - The Free Marketing Project
    By sharefree in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-27-2010, 03:15 PM
  3. Can You Help Thİs Project?
    By demir12 in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 02:53 AM
  4. C++ Project
    By Rhino1775 in forum C++ Programming
    Replies: 9
    Last Post: 10-17-2003, 11:29 AM
  5. your 11th & 12th grade c++ project or similar project
    By sleepyheadtony in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-13-2002, 05:14 PM

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