User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,219 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,256 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1287 | Replies: 10
Reply
Join Date: Dec 2007
Posts: 9
Reputation: Archimag is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Archimag Archimag is offline Offline
Newbie Poster

Some help with input check....

  #1  
Dec 29th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,639
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Some help with input check....

  #2  
Dec 29th, 2007
> void main()
This is wrong, main returns int.
http://c-faq.com/ansi/voidmain.html
http://faq.cprogramming.com/cgi-bin/...&id=1043284376

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

And while I'm on a roll, try this as well
http://www.catb.org/~esr/faqs/smart-...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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Dec 2007
Posts: 9
Reputation: Archimag is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Archimag Archimag is offline Offline
Newbie Poster

Re: Some help with input check....

  #3  
Dec 29th, 2007
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.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,639
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Some help with input check....

  #4  
Dec 29th, 2007
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 36
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Some help with input check....

  #5  
Dec 29th, 2007
Off-topic:
Why declare one element array of char?
char class_name[NAME_LENGTH],choice[1];
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Join Date: Dec 2007
Posts: 9
Reputation: Archimag is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Archimag Archimag is offline Offline
Newbie Poster

Re: Some help with input check....

  #6  
Dec 31st, 2007
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
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 36
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Some help with input check....

  #7  
Dec 31st, 2007
Originally Posted by Archimag View Post
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.
  1. char choice;
  2. do {
  3. printf("Please choose one of the options below:\n"
  4. "1- Create/update class. \n"
  5. "2- Delete class. \n"
  6. "3- Insert new species. \n"
  7. "4- Delete species. \n"
  8. "5- Update species. \n"
  9. "6- Print species. \n"
  10. "7- Print class. \n"
  11. "8- Exit. \n");
  12.  
  13. choice = getch();
  14. switch(choice) {
  15. case '1':
  16. printf("This is create/update class part");
  17. break;
  18. case '2':
  19. printf("This is Delete class");
  20. break;
  21. //......
  22. //......
  23. }
  24. } 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.
Last edited by invisal : Dec 31st, 2007 at 9:43 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Join Date: Dec 2005
Posts: 3,639
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Some help with input check....

  #8  
Dec 31st, 2007
> 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?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Dec 2007
Posts: 9
Reputation: Archimag is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Archimag Archimag is offline Offline
Newbie Poster

Re: Some help with input check....

  #9  
Dec 31st, 2007
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....
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....
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 36
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Some help with input check....

  #10  
Dec 31st, 2007
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC