Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 04-15-2009, 07:27 PM   #1
Fully Optimized
Š88's Avatar
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default C: problem skipping user input

I'm using C in my program (a big part of my issue already) and a part of it requires a user to enter a name to be read from a text file. I started by using fgets but that created issues in itself. I reverted to using scanf which saw an improvement but here's what happens:

There is a loop that asks the user if she wishes to read, write or delete. If I select read, it then asks how many names I want to read. Then it goes through a loop of asking for a name, calling a procedure and returning a result and will loop for the number of names I chose to read.
Now that exits, and it asks the user if she wants to continue, if yes, it loops back and asks for an option ("no" exits obviously). Now this time if I select read, it prompts for the number of names to read, i enter a number but then it skips asking for any names and goes straight to where it asks if I want to continue.

Now here's a portion of the code:
  if (strcmp(selection, "read") == 0) {
   	   printf("Enter number of names: ");
   	   scanf("%d", &numNames);
   	    		if (numNames > 5) {
   	    			fprintf(stderr, "Number of names must be 5 or less\n");
   	    			while(j < numNames){
   	 					printf("Enter name: ");
   	 					scanf("%s", name);
and at the end where it asks if you wish to continue:

   	printf("Do you wish to continue? [Y/N]: ");
   	scanf("%s", answer);
	while (strcmp(answer, "Y") == 0 || strcmp(answer, "y") == 0);
any ideas how to fix it?

Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Old 04-30-2009, 08:50 PM   #2
In Runtime
Join Date: Dec 2001
Posts: 154
Default Re: C: problem skipping user input

I know very little about c but did you check your variables. you may have the set incorrectly. also check your if statements. I would try setting &numNames to 0 prior to entering the loop. By doing that will remove any trash in that memory location. Also double check your syntax, the smallest syntax error can through the whole program off.

Hid_Enigma is offline   Reply With Quote
Old 05-04-2009, 06:23 AM   #3
Beta Member
Join Date: May 2009
Posts: 2
Default Re: C: problem skipping user input

There is a small problem though: fgets also reads the <return> the user typed to ... The basic method of skipping input is to use the '*' to indicate ...
masudkarim is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 08:08 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0