• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Basic Xcode C Lang question...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic Xcode C Lang question...


  • Subject: Re: Basic Xcode C Lang question...
  • From: Markian Hlynka <email@hidden>
  • Date: Mon, 12 Jul 2004 08:53:25 -0600

On Jul 11, 2004, at 13:57, Isaac Rivera wrote:

Thanks all of you

The problem was I missed the space in the format string " %c". It is hard to read in the small type of the book and its not explained that there should be one.

Thanks for taking from your valueable Sunday time to help me out. Its clearer now.


ah, but WHY is the space necessary? It's necessary because of the _previous call,


scanf(" %lf", &value); /* Read the next value */


This call reads the value from stdin... and STOPS once it has the value. It does NOT read the newline. Thus,

scanf("%c", &answer ); /* Read response Y or N */

without a space reads the newline, \n. since tolower('\n') != 'n', this never happens:


     if( tolower(answer) == 'n' )    /* look for any sign of no */
       break;                         /* Exit from the loop      */

Note that the purpose of the space is to read _all whitespace up to the next value.


Another solutions would be to do this:

     scanf(" %lf\n", &value);          /* Read the next value       */

This would only let scanf read a value if it was followed by a newline.

Another option is to flush stdin until you find the newline:

	char mybuffer[1024];
	scanf(" %lf", &value);          /* Read the next value       */
	scanf("%s\n"), mybuffer);

This will read everthing up to the next newline; provided it doesn't exceed 1024 chars. We could test for this...
result = 0;
while (result != 1)
result = scanf("%s\n"), mybuffer);


scanf should only return 1 if it is successful (in this case). From the man page:

These functions return the number of input items assigned, which can be
fewer than provided for, or even zero, in the event of a matching fail-
ure. Zero indicates that, while there was input available, no conver-
sions were assigned; typically this is due to an invalid input character,
such as an alphabetic character for a `%d' conversion. The value EOF is
returned if an input failure occurs before any conversion such as an end-
of-file occurs. If an error or end-of-file occurs after conversion has
begun, the number of conversions which were successfully completed is
returned.



Of course, it would be better to use getchar in a while loop to look for the \n.


Someone already suggested fpurge, which is the best way to do this. The above is provided for illustrative purposes.


As to using scanf, it can be a very powerful tool, and in my experience easier to learn and use than iostreams for getting things working just exactly how you want them.


Markian
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.


References: 
 >RE: Basic Xcode C Lang question... (From: "Bill Lowrey" <email@hidden>)
 >Re: Basic Xcode C Lang question... (From: Isaac Rivera <email@hidden>)

  • Prev by Date: Couldn't generate .bb .bbg file
  • Next by Date: Re:Umbrella Frameworks and headers
  • Previous by thread: Re: Basic Xcode C Lang question...
  • Next by thread: Linux Deployment
  • Index(es):
    • Date
    • Thread