Ben Bacarisse
8/30/2011 11:51:00 PM
Jack <spamtrap@trashcan.invalid> writes:
> On Tue, 30 Aug 2011 21:29:25 +0000, John Gordon wrote:
>
>> In <j3jj2t$eq3$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid>
>> writes:
>>
>>> Ok, here's the deal...my instructor wants us to do the following
>>> program. Only one problem: what must I use to get this program to
>>> work right? Do I use combinations of if, for and while statements, or
>>> just if statements or what? I have tried nested if and while
>>> statements and CANNOT get the program to execute properly... Perhaps
>>> an experienced programmer(unlike myself) could give me a little insight
>>> on how to do this problem...
>>
>> A good start would be to show us what you wrote. Perhaps we can explain
>> where you went wrong.
>
> John --- Sure. Thought this forum was empty for a while, good to get a
> reply at last. I'd like to fix this up by 4PM today!
A reply was posted with in half an hour. That's about as good as you
can expect from Usenet (you may be reading this as a web forum but it is
really a Usenet news group).
> here is the code I have already written...maybe you could
> expand to it a little?
>
> long scan_int(void)
> {
> int c ; /* character, nb type is int not char to allow EOF */
> int sign='+'; /* + for positive number, - for negative */
> long sum=0; /* stores sum */
>
> while((c=getchar())==' '); /* whitespace delimeters */
So far so good but do you really find that easy to read? I'd write it
like this:
long scan_int(void)
{
int c; /* character, nb type is int not char to allow EOF */
int sign = '+'; /* + for positive number, - for negative */
long sum = 0; /* stores sum */
while ((c = getchar()) == ' '); /* whitespace delimeters */
Note the indentation and extra spaces. They get even more significant
later.
On a more important note, ' ' is not the only white space. Look up the
function in the header ctype.h (isspace, etc.) or, if you think you
should not use these yet, write a more complex test.
> if(c=='-') /* negative */
> {sign='-';
> c=getchar();
> while((c=getchar())==' ');
> if(c<'0'||c>'9')
> return(INVALID);}
>
> else if(c=='+') /* positive */
> {sign='+';
> c=getchar();
> while((c=getchar())==' ');
> if(c<'0'||c>'9')
> return(INVALID);}
I find this very hard to read all jammed-up like that. Do you really
want to accept input like this: " + 99"? scanf's %d format does not
so I suspect you should not do so either.
> if(c<'0'||c>'9')
> {while(c!=' ') /* space delimeter */
> c=getchar();
Are you sure this loop will end? Here's a tip: every time you write a
loop ask if will always end. If you think it will try to prove yourself
*wrong*. If you can't find any way to make the loop go on forever, you
may be safe!
> return(INVALID);}
In general, input functions should not read stuff that they don't
understand. If there is non-numeric data you should probably leave it
alone. Unfortunately you are hampered by a terrible specification. If
you do as you are told (emulate scanf and use the given driver) you can
not test non-numeric input because it will be left unread.
> if(c=='-'||c=='+')
> return INVALID;
>
> if(c>='0'&&c<='9')
> {while(c>='0'&&c<='9')
> {sum=sum*10+(c-48); /* update sum */
48? Hint: what does this do: printf("%d\n", '0'); ?
> c=getchar();}
> }
>
> else return(INVALID);
You are very nearly home and dry.
Once you've tidied it up, I think you will find that you can remove a
few of the invalid return tests. It's tempting to return an error as
soon as you know something is wrong, but the code is often easier to
follow if you have fewer tests and later ones will often catch earlier
errors.
> if(sign=='-')
> sum=sum*(-1);
> return(sum);}
You can set sign to anything you like as soon as you see the + or the -
in the input. Given that freedom, can you think of something to set it
to that would simplify this last bit if code?
I've likely missed some tings because the code is all crushed up and not
easy for me to read, but it looks pretty close to done.
--
Ben.