Thread: array help

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    7

    array help

    Code:
    void addNewEnrolment() {
    
    printf("\n       Student Number: ");
    	scanf("%s", NEW_ENR_STUDENT_NUMBER);
    	strcpy(edatabase[enrolment_record_count].ENR_STUDENT_NUMBER, NEW_ENR_STUDENT_NUMBER);
    	edatabase[enrolment_record_count].ENR_STUDENT_NUMBER[9] = '\0';
    }
    
    void addToEnrolmentDatabaseArray(char line[]){
    
    strncpy(edatabase[enrolment_record_count].ENR_STUDENT_NUMBER, data_pointer, 9);
    	edatabase[enrolment_record_count].ENR_STUDENT_NUMBER[9] = '\0';
    
    }
    This is a part of my C program, there is a function to add new enrolment, and another funtion to add record to the array. The variable is declared like this:

    char ENR_STUDENT_NUMBER[10];

    The problem I am having is that, when the user input number, let say: 120 , it is less than the array size, when i call functions to modify or delete the record using ENR_STUDENT_NUMBER, it says Record not Found, but the record is succesfully added in a text file. Another problem is that, with the same workings like above but different variable:

    char ENR_COURSE_DESCRIPTION[26];

    When the program asks for user input, whenever the user enter space between input, the program skips 2 other inputs and goes for the next. Can anyone help me solve this problem?

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    The problem is that you're reading a number as a string and that the "number" 120 is in fact not a number but the concatenation of characters '1', '2' and '0'. Worse, the ascii value for '1' is far off from being 1, so any operation treating it as a number won't work.

    I suggest you read the following FAQ entry about how to get a number from a user. It explains in every detail what the most safe way of reading a number is and gives a good example.

    A shorter but less good way of doing it would be to use scanf to read a number:

    Code:
    int i;
    scanf("%d", &i);
    but then you're at the mercy of the user input.

    Another way would be to convert the characters into numbers, which takes quite some practice:

    Code:
    int number = 0;
    char string[512];
    int i;
    
    scanf("%s", string);
    for (i = 0; i < strlen(string); i++, number *= 10)
    {
        number += (string[i] - '0');
    }
    I also recommend reading how to get a string instead of using scanf().
    Last edited by KONI; 03-26-2007 at 03:12 PM.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by KONI
    Code:
    for (i = 0; i < strlen(string) - 1; i++, number *= 10)
    {
        number += (string[i] - '0');
    }
    This loop doesn't process the last character of the string.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by brewbuck
    This loop doesn't process the last character of the string.
    fixed, thanks a lot

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    7

    hi

    If i use fgets for user input, how i can store the input in a variable, i need to put this record in an structure array database. I am maintaining this program, can you help explain in detail the add to database and enrollment part, i am not 100% understand on that part, alright.

    Code:
    //read file
    char line[80];
    
    while(fgets(line, 80, infile) != NULL) {
    			addToEnrolmentDatabaseArray(line);
    			enrolment_record_count = enrolment_record_count + 1;
    
    //add to database
    void addToEnrolmentDatabaseArray(char line[]) {
    	extern EnrolmentDatabase edatabase;
    	extern int enrolment_record_count;
    
    	char *data_pointer = line;
    
    strncpy(edatabase[enrolment_record_count].ENR_COURSE_DESCRIPTION, data_pointer, 25);
    	edatabase[enrolment_record_count].ENR_COURSE_DESCRIPTION[25] = '\0';
    	data_pointer += 25;
    
    //addEnrolment
    printf("   Course Description: ");
    	scanf("%s", NEW_ENR_COURSE_DESCRIPTION);
    	strcpy(edatabase[enrolment_record_count].ENR_COURSE_DESCRIPTION, NEW_ENR_COURSE_DESCRIPTION);
    	edatabase[enrolment_record_count].ENR_COURSE_DESCRIPTION[25] = '\0';

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by gunnerz View Post

    When the program asks for user input, whenever the user enter space between input, the program skips 2 other inputs and goes for the next. Can anyone help me solve this problem?
    If you study KONI's link to the FAQS, you can fix that user input problem. Never underestimate the power of a user to muck up input --> they're geniuses at it.

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM