Ale
Hallan people..
I have something like this:

void main(){
	
	int flag=1,exit=1;
	cla_N *head=NULL;
	char class_name[NAME_LENGTH],choice[1];
	double diversity;

	while (exit)
	{
		printf("Please choose one of the options below:\n"
			   "1- Create/update class.                \n"
			   "2- Delete class.                       \n"
			   "3- Insert new species.                 \n"
			   "4- Delete species.                     \n"
			   "5- Update species.                     \n"
			   "6- Print species.                      \n"
			   "7- Print class.                        \n"
			   "8- Exit.                               \n");
		//scanf("%d", &flag);
		gets(choice);
		//choice=getchar();
		//scanf("%c",&choice[0]);
			
		switch (choice[0])
		{

			case '1':
				
				printf("Enter class name:\n");
				//gets(class_name);
				gets(class_name);
				printf("Enter diversity:\n");
				scanf("%lf", &diversity);	
				head=func1(head,class_name,diversity);
				break;

			case '7':

				printf("Enter class name:\n");
				//gets(class_name);
				gets(class_name);
				func7(head,class_name);
				break;
			
			case '8':

				exit=0;
				break;
		
			default:
				
				printf("Error: input incorrect! Please try again.\n");
				break;
		}
	
		
	}

	


}

When i choose for the first time it's okay.....after i'm done with my first action it prints the MENU (it's good) and autimatically prints the default option(error:...bla bla) and the menu again.....Means it's skips the 'gets'.....WHY? Any ideas? i tried the 'scanf', 'getchar', everything....what is the right way to check input to be valid? AND ONCE AND FOR ALL! CAN SOMEONE TELL ME WHAT'S WRONG WITH THE 'GETS' FUNC? IT TOTALLY DOES IT'S OWN THING AND I DON'T KNOW WHAT IT IS.

Recommended Answers

All 10 Replies

> void main()
This is wrong, main returns int.
http://c-faq.com/ansi/voidmain.html
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376

> gets()
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351

And while I'm on a roll, try this as well
http://www.catb.org/~esr/faqs/smart-questions.html#writewell

The real problem is you're mixing scanf() with gets() (ugh!!!) and getchar()
If you mix and match, as you're doing, then you'll come unstuck. The reason being they have different ideas as to what marks the end of input. scanf for example typically leaves a newline behind, and that's just what the others like to see as well.

The first bit of your code might look like this

char buff[BUFSIZ];
	char choice;

	while (exit)
	{
		printf("Please choose one of the options below:\n"
			   "1- Create/update class.                \n"
			   "2- Delete class.                       \n"
			   "3- Insert new species.                 \n"
			   "4- Delete species.                     \n"
			   "5- Update species.                     \n"
			   "6- Print species.                      \n"
			   "7- Print class.                        \n"
			   "8- Exit.                               \n");
		fgets( buff, sizeof buff, stdin );
		choice = buff[0];
		// sscanf( buff, "%c", &choice );  // would also work

ALWAYS use fgets() to read a line of input, then you can use array access, sscanf, or a whole bunch of other string functions to validate and convert the information.

For extra safety, check the return result of fgets() as well.

> exit=1;
This is also the name of a function in stdlib.h
Try to avoid such names to avoid later confusion.

Thanx....i tired...
None works! //fgets( buff, sizeof buff, stdin );
choice = buff[0]; // still the same problem.....after my first action is done it prints the menu , the error line and when the menu once again while waiting for an input.

Did you replace ALL your other usage of gets() and scanf() with fgets() and sscanf() ?

If you didn't, you haven't really solved anything yet.

Off-topic:
Why declare one element array of char?

char class_name[NAME_LENGTH],choice[1];

Well....a good input for my case is: a number between 1-8-> a number and then whatever but not some char and then the number....means i need to check only the [0] place...
i also tried choice[100] with the same result.......and in the example program i can insert: "dhgfdshjdhgdgdhg fdhgfdhgfdhnfnlnlnnlknl" means not limitd length of string....
And i can't use things like fgets or "buff", i don't know what it is really......
I really don't get it, why it skips the gets after the first input.....
1 action
Menu
Input...
2 action
Menu
! Errror message (default choice)
Menu !
Input

Well....a good input for my case is: a number between 1-8-> a number and then whatever but not some char and then the number....means i need to check only the [0] place...

Since you only need a single character input, you can alternatively use choice = getch();

i also tried choice[100] with the same result.......and in the example program i can insert: "dhgfdshjdhgdgdhg fdhgfdhgfdhnfnlnlnnlknl" means not limitd length of string....

Why would you want your user able to mindlessly input a large length of string where you need only a single character input?

This piece of code should works fine.

char choice;
    do {
		printf("Please choose one of the options below:\n"
			   "1- Create/update class.                \n"
			   "2- Delete class.                       \n"
			   "3- Insert new species.                 \n"
			   "4- Delete species.                     \n"
			   "5- Update species.                     \n"
			   "6- Print species.                      \n"
			   "7- Print class.                        \n"
			   "8- Exit.                               \n");

        choice = getch();
        switch(choice) {
            case '1':
                printf("This is create/update class part");
                break;
            case '2':
                printf("This is Delete class");
                break;
             //......
             //......
        }
    } while(choice != '8');

Useful Trick
To track the non-syntax errors, you can insert the printf() in the code you think that it is the cause to show what is really happened. For example (from your code)

while (exit)
	{
		printf("Please choose one of the options below:\n"
			   "1- Create/update class.                \n"
			   "2- Delete class.                       \n"
			   "3- Insert new species.                 \n"
			   "4- Delete species.                     \n"
			   "5- Update species.                     \n"
			   "6- Print species.                      \n"
			   "7- Print class.                        \n"
			   "8- Exit.                               \n");
		//scanf("%d", &flag);
		gets(choice);
                printf("%c", choice[0]);
		//choice=getchar();
		//scanf("%c",&choice[0]);
			
		switch (choice[0])
		{

By insert this printf() function, you're able to track what value you actually recieved.

> gets(choice);
Perhaps you should read the FAQs as well.

> I really don't get it, why it skips the gets after the first input.....
I told you already, mixing input methods is a disaster waiting to happen.

> And i can't use things like fgets or "buff", i don't know what it is really......
What do you mean you "don't know" ?
Are you supposed to restrict yourself to the few functions 'teacher' has told you about so far?

[TEX]Are you supposed to restrict yourself to the few functions 'teacher' has told you about so far?[/TEX]
Well....YES...it's stupid i know, but that's how it works....and they are very strict about that.
THANX for your help people...appriciate it....will try to implement some of the things you told me....But it's really a small piece of the program.....nearly no points.....The real assignment is linked lists and it's a pain in the butt so far......working hard on that....
For someone who only learning C for a couple of months 5 hours peer week it's a big assignment.....i hadn't used pointers yet in my previous homework and now it's really confusing me....these one and another assignment and then the big final exam! BUT I MUST SAY I GOT TO REALLY LOVE IT......it's fun bashing your head against the wall sometimes trying to write a piece of code....

Are you supposed to restrict yourself to the few functions 'teacher' has told you about so far?

Well....YES...it's stupid i know, but that's how it works....and they are very strict about that.
THANX for your help people...appriciate it....will try to implement some of the things you told me....But it's really a small piece of the program.....nearly no points.....The real assignment is linked lists and it's a pain in the butt so far......working hard on that....

man, some teachers are really brain damaged... first (and foremost) because they teach pure BS {old functions, non stardard functions, ...} and secondly because they try to cover their stupidity by restricting the students from using something else , new to the teacher but (to the sane world) obviously better...

Archimag i would suggest hearing the advice from other ones especially from this forum....{only good things can happen!} and make 2 versions one for you{so that you can learn} and one for the teacher {so that you can get a grade}

i also had some io problems in november so here is something that maybe of interest to you::
here, and here... the latter one is a great tutorial...

-nicolas

PS:: happy new year to everyone!!!! :)

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.