Hello everyone,

I've been having a problem with some of my scanf() functions being skipped after I input a string with a space in between (e.g Jack Daniels). I've been trying to fix it, but it has been driving me crazy.

#include <stdio.h>
#include <stdlib.h>

struct Student
{	
	int studentNumber;
	int phoneNumber;
	char* studentName;
};

int main()
{
	struct Student *list;
	int numOfStudents;
	int x;
	int studentCounter = 1;

	printf("\nPlease enter the number of students you would like to input: ");
	scanf("%d", &numOfStudents);

	list = malloc(sizeof(list) * numOfStudents);

	for(x = 0; x < numOfStudents; x++)
	{	
		printf("\nPlease enter name for student #%d: ", studentCounter);
		scanf("%s", &list[x].studentName);
		printf("Please enter student number for student #%d: ", studentCounter);
		scanf("%i", &list[x].studentNumber);
		printf("Please enter phone number for student #%d: ", studentCounter);
		scanf("%i", &list[x].phoneNumber);	
	
		studentCounter++;
	}

	
}

In the code above, after I input the student name, the the 2 remaining scanf()s are disregarded and the loop iterates to 2, going to second student name. I tried flushing using fflush(stdout) but that did not fix anything either, lol. I searched around and found some suggestions that asked me to use scanf(” %[^\n]s”,a) instead of scanf("%s", a) but that did not work either. Any help would be much appreciated, as I think I am losing my mind to such a small hiccup/problem! :(

Recommended Answers

All 17 Replies

When you enter a number you have to press the <Enter> key, right??? Well, scanf() leaves that '\n' <Enter> key in the keyboard so the next time scanf() or some other keyboard function is called all it will get if the '\n' Enter key.

what you have to do is flush out that '\n' byte in the keyboard buffer after entering numeric data, e.g. call getchar() after scanf(). That may, or may not, be a complete solution if you enter a lot of keys after entering the numeric data, for example if you enter "123abc\n" scanf() will pick up the 123 and put it into the numeric variable but leave the rest of the stuff in the keyboard buffer. Unfortunately there is no standard solution to flushing out all the stuff in the input keyboard buffer. There are a few non-standard solutions but they are compiler dependent which may or may not work with your instructor's compiler should he/she want to compile your program.

@Ancient Dragon

Thanks for your quick reply buddy. Unfortunately calling getchar() after the first scanf statement inside the for-loop does not solve the problem. It still jumps the "Enter student number" and "Enter phone number" parts for first student.

What the output looks like is this:

Please enter the number of students you would like to input: 3

Please enter name for student #1: Jack Daniels
Please enter student number for student #1: Please enter phone number for student #1:
Please enter name for student #2: Please enter student number for student #2: (Here is where I can start inputting values again).

I hope that output helps clear up what I am having problems with.

You need two of them -- after lines 19 and 30. Note that phone number should be a character array so that you can enter dashes like 555-555-5555 or (555) 555-5555

@Ancient Dragon

char array for phone numbers noted :). Unfortunately the getchar()s still do not alleviate the problem.

NOTE: I am using GCC if that helps.

gcc might make a difference. I'm using vc++ 2010 express on Windows 7 and it worked with that compiler.

There is another problem with that structure -- see studentName below

struct Student
{
	int studentNumber;
	int phoneNumber;
	char studentName[40];
};

I compiled it with Code::Blocks and MinGW (port of gcc) and it worked ok too

#include <stdio.h>
#include <stdlib.h>

struct Student
{
	int studentNumber;
	int phoneNumber;
	char studentName[40];
};

int main()
{
	struct Student *list;
	int numOfStudents;
	int x;
	int studentCounter = 1;

	printf("\nPlease enter the number of students you would like to input: ");
	scanf("%d", &numOfStudents);
getchar();
	list = malloc(sizeof(list) * numOfStudents);

	for(x = 0; x < numOfStudents; x++)
	{
		printf("\nPlease enter name for student #%d: ", studentCounter);
		scanf("%s", list[x].studentName);
		printf("Please enter student number for student #%d: ", studentCounter);
		scanf("%i", &list[x].studentNumber);
		printf("Please enter phone number for student #%d: ", studentCounter);
		scanf("%i", &list[x].phoneNumber);
getchar();

		studentCounter++;
	}


}

I'll fire up vc++ and try it out myself then. Too bad our instructor wants all compilations on the server's gcc :(.

>>Too bad our instructor wants all compilations on the server's gcc

Your instructor is probably using *nix

>>I'll fire up vc++ and try it out myself then.
Make sure to correct the problem with that structure.

I used that exact code you provided (char studentName[40] instead of char *studentName) and it still had the same problem. Think I need a break, lol ..

What did you enter for each of the questions? scanf() using "%s" does not accept spaces in text you enter so you have to enter studentName as only one word. If you want to be able to enter two or more words for studentName then use fgets() instead of scanf(), e.g. fgets(list[x].studentName, sizeof(list[x].studentName), stdin);

What did you enter for each of the questions?

If I enter (Jack) for name it executes correctly. If I enter a full name (Jack Daniels) it does not work. I really need it to work with full names though :(. Do you think I should just make 2 arrays? One for first name and one for last? One more thing, why do you think that my previous struct studentName variable was incorrect? Kind of confused ..

>> I really need it to work with full names though
See my previous post how to fix it.

>> One more thing, why do you think that my previous struct studentName variable was incorrect?

It was incorrect because all you declared was a pointer, and there is no memory attached to pointers. The change I made to your structure provides enough room to enter 40 characters as the name. Pointers do not allow for any room unless you specifically call malloc() to allocate the memory.

>> I really need it to work with full names though
See my previous post how to fix it.

>> One more thing, why do you think that my previous struct studentName variable was incorrect?

It was incorrect because all you declared was a pointer, and there is no memory attached to pointers. The change I made to your structure provides enough room to enter 40 characters as the name. Pointers do not allow for any room unless you specifically call malloc() to allocate the memory.

Ohh yes! I was under the impression that I called malloc that, apologies. Would you happen to know a fix for scanning in full names though? I could always make 2 arrays for first name/last name; but I think it would look much neater if they were in one array index.

>> I really need it to work with full names though
See my previous post how to fix it.

Ohh, did not see that part from your post. Will try, thank you ;).

One crevet you need to know about fgets() -- it frequently puts '\n' at the end of the string which you will need to erase.

What did you enter for each of the questions? scanf() using "%s" does not accept spaces in text you enter so you have to enter studentName as only one word. If you want to be able to enter two or more words for studentName then use fgets() instead of scanf(), e.g. fgets(list[x].studentName, sizeof(list[x].studentName), stdin);

Yes, spaces are part of the problem.

You might want to notice this compiler warning as well (from the source code you initially posted):

andy@debian:~/prog/c$ cc -Wall temp.c -o temp
temp.c: In function ‘main’:
temp.c:26: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
temp.c:36: warning: control reaches end of non-void function

As for your scanf problems, you might want to consider switching to getline().
http://crasseux.com/books/ctutorial/getline.html

The getline function is the preferred method for reading lines of text from a stream, including standard input. The other standard functions, including gets, fgets, and scanf, are too unreliable. (Doubtless, in some programs you will see code that uses these unreliable functions, and at times you will come across compilers that cannot handle the safer getline function. As a professional, you should avoid unreliable functions and any compiler that requires you to be unsafe.)

This is the C forum and program, not c++, so using getline() is not an option.

getline() is also included with the GNU C compiler collection, in stdio.h. But it's not part of ANSI C. I see my error after re-reading the forum description. My apologies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.