![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 6
Rep Power: 0
![]() |
[SOLVED] Variable array problem
I'm working on a project for one of my classes and I've run into a problem, normally I'd just ask one of the teachers but it's due Tuesday, and our classes don't resume until Tuesday.. anyways on to the problem.
I have to make a program that stores student information, registration number, name, etc., etc., using only C commands and no more than 2 globals. I've got the program done, however during testing I noticed that when entering a registration number for the student, I have it running a check in the array to find a match, if there isn't another one it keeps going otherwise it asks you to enter it again. Well herein lies the problem, we were told to go with a modular design for this, so I have the check in a seperate function doing that check and then returning, but not returning any type of value. Just a simple return; However during testing I've found out that the registration number which is set in the array as char reg_no[4]; takes the last name as well, so instead of j123 it ends up being j123smith, like it hadn't terminated or something. I'll post the code, hopefully my ramblings have made some sort of sense. I've truly tried to grasp this on my own before comming to the boards for assistance, but it's been 3 hours now and I don't think I'm any closer to the answer. void create_student(void)
{
char reg_no[4];
clrscr();
gtotal++;
student_index[gtotal].index = gtotal;
printf("%s", student_index[1].reg_no);
printf("\n%s", student_index[1].surname);
printf("\n%s", student_index[1].forename);
while (student_index[gtotal].index != 999)
{
printf("\nPlease enter a registration number, i.e., j123 :");
gets(student_index[gtotal].reg_no);
fflush(stdin);
if ((strlen(student_index[gtotal].reg_no) != 4) || (isdigit(student_index[gtotal].reg_no[0])) || (!isdigit(student_index[gtotal].reg_no[1])) || (!isdigit(student_index[gtotal].reg_no[2])) || (!isdigit(student_index[gtotal].reg_no[3])))
{
printf("\nYou must have exactly 1 letter and 3 numbers in\nthe format j123 for a total of 4 characters\n");
}
else
{
search_for_account();
}
}
printf("\nPlease enter the students surname: ");
gets(student_index[gtotal].surname);
fflush(stdin);
printf("\nPlease enter the students first name :");
gets(student_index[gtotal].forename);
fflush(stdin);
while ((student_index[gtotal].credits < 10) || (student_index[gtotal].credits > 15))
{
printf("\nPlease enter the number of credits being taken (10-15) :");
scanf("%d", &student_index[gtotal].credits);
fflush(stdin);
}
}
void search_for_account(void)
{
int loop;
for (loop = 1; loop <= MAX; loop++)
{
if (loop != gtotal)
{
if (student_index[loop].reg_no == student_index[gtotal].reg_no)
{
printf("\nStudent registration number already in use, please pick another");
return;
}
else
{
printf("%s", student_index[gtotal].reg_no);
student_index[gtotal].index = 999;
}
return;
}
}
}Anyone that is able to point me in the right direction, I'd be eternally gratefull. (btw, the little printf's with the reg_no and other stuff is just so I can see what the value is at a given stage while I was trying to figure out how I fubar'd this. Last edited by Hintshigen; Apr 10th, 2005 at 8:58 AM. Reason: Solved |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|