Click here to Skip to main content
14,693,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the program is meant to register member info into a global array member_array
the problem is that it is meant to take 9 inputs from user but when I test it .. the program takes only 6 of them before it terminates .. I don't know why!

What I have tried:

typedef struct
{
    int buildNum;//use validateNegative
    char street[10];
    char city[10];
} addressStruct;

typedef struct
{
    char name[10];
    char domain[10];

} emailStruct;

typedef struct
{
    char name[10];
    int ID;
    addressStruct address;
    int mobile;
    int age;
    emailStruct email;
    int booksBorrowed;
} member;
member member_array[50];
<pre>void registeration(void)
{

    gets(member_array[i].name);
    scanf("%d",&(member_array[i].ID));
    scanf("%d",&(member_array[i].address.buildNum));
    gets(member_array[i].address.city);
    gets(member_array[i].address.street);
    scanf("%d%d",&(member_array[i].mobile),&(member_array[i].age));
    gets(member_array[i].email.name);
    gets(member_array[i].email.domain);
    i++;
    return ;

}
int main()
{
  registeration();

  return 0;
}
Posted
Updated 6-Dec-17 23:21pm

We can't tell - it's going to depend on exactly what you input at any time, and we can't see you typing!
So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
In your code, you need a global int i variable as well. Moreover you should initialize it to 0.

That said, my advices are:
  • Do NOT use global variables, pass local variables as parameters to your functions.
  • Do NOT use the gets function, use fgets instead
   
The reason for reading not all input values is sourced by using gets and scanf variantly.

gets - C++ Reference[^] reads until a new line character has been read without storing the newline character in the read buffer.

scanf - C++ Reference[^] skips leading white spaces (including new line characters) and reads until the format does not match anymore. That usually lets new line characters in the system input buffer.

So there will be an unread new line character after calling scanf. When calling gets aftwerwards, that will read the new line character and stop resulting in an empty string in the passed buffer.

To avoid such problems, don't use both functions or ensure that new line characters are skipped before calling gets.

A common solution is to use fgets - C++ Reference[^] for all read operations (instead of gets as alredy suggested). For inputs that are not strings use conversion functions like atoi, atof, or sscanf for multiple parameters on one line.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900