Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2005, 12:29 PM   #1
Hintshigen
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 Hintshigen is on a distinguished road
[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
Hintshigen is offline   Reply With Quote
Old Apr 9th, 2005, 1:08 PM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Heh, I'm about to rush out so I can't take a real look but one thing (probably unrelated) that jumped out at me was this line:
        for (loop = 1; loop <= MAX; loop++)
Array elements start at 0, so unless you purposely want to ignore the first element in the array you should do for(loop = 0....
Cerulean is offline   Reply With Quote
Old Apr 9th, 2005, 1:17 PM   #3
Hintshigen
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 Hintshigen is on a distinguished road
Yeah I have gtotal initializing at 0 before main, and incrementing when selected in the menu when running create_student so it always starts at 1 and use position 0 to hold temp information elsewhere in the program.

But thanks for taking a look anyways
Hintshigen is offline   Reply With Quote
Old Apr 9th, 2005, 9:46 PM   #4
Hintshigen
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 Hintshigen is on a distinguished road
Is there a way to terminate the gets command the help file says that it's terminated by a new line which is then replaced by a null terminator....

Here's the thing, the string the reg_no goes into is set to [4], and if I remember correctly, if, when reading in a variable, if there isn't a null terminator it continues to collect data until full, gen protect, blue screen or it finds another. Which I assume is why when in testing if I set the reg_no to a123 and the surname to lastnamebob and then go to create a second student to see what information is stored in the first index of the array, I get

a123lastnamebob
lastnamebob
firstnamebob

So I'm assuming that somewhere in the search_for_account function, either by calling it or using a return, as I couldn't fathom anything else in there being the cause, that the return key press never counts until you enter the surname.

I've tried manually inserting a '\0' into reg_no[4] but that ends up wiping the surname for some reason, I've thrown new lines into the mixes, getch's, even some fancy smancy code thingy to only accept a return key press within the search_for_account function right before the return.

It seems like I'm almost on to something here, but it's just outta reach. Dunno if that makes any sense. Probably something small and I'll end up kicking myself for a while once I figure it out.

It should work, because it's based off of psuedocode that had to be done, verified and approved before I could start the coding, I would like to think the teacher isn't setting me up for failure.

Thanks for taking the time to read this and if you are able to point me in the right direction, thanks.
Hintshigen is offline   Reply With Quote
Old Apr 10th, 2005, 2:25 AM   #5
Rei
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 Rei is on a distinguished road
Never use gets

Never use gets. Ever. Don't just listen to me - look at the man page for gets; I'll quote:

"BUGS: Never use gets. Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead."

Do what it says. And remember: C-style strings *must* be null terminated; any string-handling functions expect the null termination. That means you need one more character than the maximum number of characters you expect to be input. Use a 5-character array, use fgets, read in only 4 characters, and then set the last character to \0 just in case.

Hope this helps
Rei is offline   Reply With Quote
Old Apr 10th, 2005, 8:56 AM   #6
Hintshigen
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 Hintshigen is on a distinguished road
Thanks, I got that part working now, I don't fully understand how to use fgets yet, a bit out of my league I think. I read a bunch of stuff I found on the net about it, but the best I could make out is that it reads from an already defined string. So the program is still using gets and scanf's.

Changing the array from 4 to 5 did help, though I don't understand why, if the array is set to 4 and registration number is limited to 4 characters a letter and three numbers. I thought it was like this

a234 <-- Four characters for the reg no
01234 <-- Array itself with space for a null terminator at the end

So why didn't that work, or am I thinking about this all wrong?

And massive thanks Rei, didn't even consider that it mighta been the array itself.
Hintshigen is offline   Reply With Quote
Old Apr 10th, 2005, 2:35 PM   #7
ice52
Newbie
 
Join Date: Apr 2005
Posts: 9
Rep Power: 0 ice52 is on a distinguished road
probably because when you declare an array of size 4 (array[4]) it makes 4 elements... not 5, i.e makes 0123, not 01234, element array[4] does not exist, not even for a null terminator.
__________________
www.ice52.co.uk
ice52 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC