Thread: Looking for Advice on Strings and Structures

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17

    Looking for Advice on Strings and Structures

    Hi everyone,

    I'm working on a program for developing files for student marks. My issue is in the else statement. What's happening is that it prints the prompt for the First Name but then prints the prompt for the Last Name without allowing me to input anything. It works fine from that point on, and allows me to input values for all consecutive prompts (not shown here). As far as I can see there is no difference between the First Name prompt and the Last Name prompt, so maybe someone here can spot it?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    #define SIZE 50
    
    
    
    
    int AddStudent();
    
    
    int AddStudent()
    {
        FILE *fptr;
        char *MyString;
        int Flag;
    
    
        struct Students
        {
            char FirstName[SIZE];
            char LastName[SIZE];
            char StudentNumber[8];
            char Assignments[5][4];
            char Labs[5][4];
            char Midterm[4];
            char Final[4];
        };
    
    
        struct Students GNG1106[200];
    
    
        fptr=fopen("E://StudentFile.txt", "a+");
    
    
        if (fptr == NULL)
            printf("Unable to open file");
    
    
        else
        {
            printf("File was opened");
    
    
            printf("\nPlease enter the first name of the student: ");
            gets(GNG1106[0].FirstName);
    
    
            printf("\nPlease enter the last name of the student: ");
            gets(GNG1106[0].LastName);
    
    
            printf("\nPlease the student's student number: ");
            gets(GNG1106[0].StudentNumber);
         }
    }
    Cheers

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    We'll start with this right off:

    Code:
    gets(GNG1106[0].FirstName);
    Do not use gets()...ever.

    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17
    Thanks for the tip.
    So I switched to fgets()

    Code:
    else { printf("File was opened"); printf("\nPlease enter the first name of the student: "); fgets(GNG1106[0].FirstName, SIZE, stdin); printf("\nPlease enter the last name of the student: "); gets(GNG1106[0].LastName, SIZE, stdin); printf("\nPlease the student's student number: "); gets(GNG1106[0].StudentNumber, 9, stdin); } }


    But I still have the same issue with the first name not being able to be input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Advice for Getline() function of strings
    By Xianokitty in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2011, 10:56 AM
  2. i need advice about data structures
    By sawer in forum C Programming
    Replies: 2
    Last Post: 04-22-2006, 03:40 AM
  3. C Strings in Structures.
    By SlyMaelstrom in forum C++ Programming
    Replies: 15
    Last Post: 10-12-2005, 04:29 PM
  4. Strings and Structures
    By Kinasz in forum C Programming
    Replies: 3
    Last Post: 02-23-2003, 03:46 AM
  5. Working with arrays of structures, need advice...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-14-2002, 08:38 AM

Tags for this Thread