Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I am learning C: a little problem with a simple source code

Reply
Thread Tools

I am learning C: a little problem with a simple source code

 
 
jacob navia
Guest
Posts: n/a
 
      11-01-2011
Le 01/11/11 21:21, Ben Bacarisse a écrit :
> James Kuyper<(E-Mail Removed)> writes:
>> On 11/01/2011 02:51 PM, zemir wrote:

> <snip>
>>> c=getch();

>>
>> You also haven't bothered to check for the possibility that getch()
>> might have failed.

>
> If it's the getch I remember, it can't fail -- by which I mean there is
> no error return that can tell you anything.
>
> <snip>


In a macintosh man getch gives

All routines return the integer ERR upon failure and an integer
value other than ERR (OK in the case of
ungetch()) upon successful completion.

POSIX specifies:

Upon successful completion getch(), mvgetch(), mvwgetch() and wgetch()
return the single-byte character, KEY_ value, or ERR. When in the
nodelay mode and no data is available, ERR is returned.

getch is not specified by ISO C
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-01-2011
On 2011-11-01, zemir <(E-Mail Removed)> wrote:
> Yes, of course........... int main() is more right but my choice is
> such common that it is not more a error......


This is not how computers work. A common mistake is still a mistake.

This is like saying that, if enough people believe that gravity's
acceleration on the earth's surface is 9 meters/second/second, it's not
bad to use that number to design an airplane.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-01-2011
zemir <(E-Mail Removed)> wrote:
> Hi all and thanks for your suggestions, but i have solved. I think my
> solution it is not good solution and more of you will be not very
> happy but it seems to work fine, very fine. Please read:


Actually, there's notthing "solved".

> #include <stdio.h>;
> #include <math.h>;
> const float Pi = 3.14159265358979323846;
> float area(float r)
> {
> return(r*r*Pi);
> }
> float perimeter(float r) {
> return(2*Pi*r);
> }
> void main() {


int main( void )

> system("cls");
> float r=0; int c;
> printf("This programm calculate Area and Perimenter of a Circle");
> printf("\n\nPlease insert radius value (in cm): ");


fflush( stdout );

> scanf("%f", &r);
> if( r > 0 ) {


If scanf() didn't find anything that looked like a number 'r' now has
some random value.

> printf("Area is cm2: ");
> printf("%.2f", area(r));
> printf("\nPerimeter is cm: ");
> printf("%.2f", perimeter(r));


You could do that all in a single line and you should have a '\n'
at the end.

> } else {
> printf("Value must to be a valid number\n");
> printf("Press anykey to continue");


End that with a '\n'.

> c=getch();
> system("circle");


WTF is that supposed to do?

> return 0;


So if the user entered a negative radius (or something not a number
and the value in 'r' was a negative number) you stop the program?
And the message to the user shoud be "Press any key to end the
program"...

> }
> printf("\nDo you want to continue? (y or elsewhere to exit)");


fflush( stdout );

Well, whatever the user enters, the program will stop. That's
nothing what your original program was supposed to do.

> c=getch();
> if(c==121) {


if ( c == 'y' )

> system("circle");
> return 0;
> }


return 0;
> }


All you did is write a program that behaves complete different.
That's not a solution in my books. A solution would be to cor-
rect the mistakes you had in the first version. I.e. check the
return value of scanf() and if it tells you that not as many
items could be read (in your case 1) as was expected clear the
input buffer by e.g. calling getc() until you find a '\n'. Then
you can call scanf() again without it trying to read the stuff
again it already rejected the last time round.

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      11-01-2011
"zemir" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> printf("\nDo you want to continue? (y or elsewhere to exit)");
> c=getch();
> if(c==121) {
> system("circle");
> return 0;
> }
> }
>
> Then i have another question about variable c: the y value of int c, i
> think, it is ascii code so it is the same in all the system?


Mostly. But it's still better to write:

if (c=='y') ...

then people can see what 121 was supposed to mean. And 'y' will work in
non-ascii systems too.

--
Bartc

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-02-2011
zemir <(E-Mail Removed)> writes:
> On Nov 1, 9:24 pm, James Kuyper <(E-Mail Removed)> wrote:
>> On 11/01/2011 04:17 PM, zemir wrote:
>> ...
>>
>> > On Nov 1, 8:06 pm, Ian Collins <(E-Mail Removed)> wrote:
>> >>> void main() {

>>
>> >> This should be int main(). On modern compilers, this won't compile
>> >> because you are returning values.
>> > Yes, of course........... int main() is more right but my choice is
>> > such common that it is not more a error......

>>
>> Just because it's a popular error doesn't make it cease to be an error.
>> Get it right now, while you're still learning. The fewer bad habits you
>> pick up now, while you're still new to the language, the less you'll
>> have to unlearn before you can be employable.

>
> Ok, from now and in the future i will use always "int main()"
> I am sure thai it is the right choice.


For C, "int main(void)" is correct. (For C++, it's "int main()".)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      11-02-2011
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:
>zemir <(E-Mail Removed)> writes:
>> On Nov 1, 9:24 pm, James Kuyper <(E-Mail Removed)> wrote:
>>> On 11/01/2011 04:17 PM, zemir wrote:
>>> ...
>>>
>>> > On Nov 1, 8:06 pm, Ian Collins <(E-Mail Removed)> wrote:
>>> >>> void main() {
>>>
>>> >> This should be int main(). On modern compilers, this won't compile
>>> >> because you are returning values.
>>> > Yes, of course........... int main() is more right but my choice is
>>> > such common that it is not more a error......
>>>
>>> Just because it's a popular error doesn't make it cease to be an error.
>>> Get it right now, while you're still learning. The fewer bad habits you
>>> pick up now, while you're still new to the language, the less you'll
>>> have to unlearn before you can be employable.

>>
>> Ok, from now and in the future i will use always "int main()"
>> I am sure thai it is the right choice.

>
>For C, "int main(void)" is correct. (For C++, it's "int main()".)


As CLC returns to its roots...

(What about casting the return value of malloc()?)

--
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com). But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

Nevertheless, the above quote is dead-on, because, the thing is - business
in one breath tells us they don't need to be regulated (which is to say:
that they can morally self-regulate), then in the next breath tells us that
corporations are amoral entities which have no obligations to anyone except
their officers and shareholders, then in the next breath they tell us they
don't need to be regulated (that they can morally self-regulate) ...

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-02-2011
On Nov 1, 3:27*pm, zemir <(E-Mail Removed)> wrote:

> [...] i am writing a
> small programm to calculate the perimeter and the area of a circle;
> this is:
>
> #include <stdio.h>;
> #include <math.h>;


there should not be a semi-colon ( after a #include (or indeed any
pre-processor directive)

> float area(float r)
> { * * * * * * * * * * * * * * * * * ** * * * * * * */*define area
> function*/
> * * * * return(r*r*3.14);}


the layout is odd (maybe partly your browser) So I assuem you meant
somethign like this:-

float area(float r)
{
/*define area function*/
return(r*r*3.14);
}

a simple consistent layout i better. return is not a function so it
needs no brackets. And a bit of whitespace makes things easier to read

float area(float r)
{
/*define area function*/
return r * r * 3.14;
}



 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      11-02-2011
"Kenny McCormack" <(E-Mail Removed)> wrote in message
news:j8r5ph$5pg$(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> Keith Thompson <(E-Mail Removed)> wrote:
>>zemir <(E-Mail Removed)> writes:


>>> Ok, from now and in the future i will use always "int main()"
>>> I am sure thai it is the right choice.

>>
>>For C, "int main(void)" is correct. (For C++, it's "int main()".)

>
> As CLC returns to its roots...


zemir is right. All these mis-declarations of 'main' are so widespread,
wouldn't it have been easier to have accepted them into the standard, than
have to be point them out every time? Especially as the compilers used don't
seem to be complaining.

> (What about casting the return value of malloc()?)


(That's also been mentioned so many times that I've no idea now whether you
are supposed to cast or not. (And no I'm not interested in knowing; the way
I do it seems to work so I don't care.))

--
Bartc

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-02-2011
On 11/02/2011 07:04 AM, BartC wrote:
....
> zemir is right. All these mis-declarations of 'main' are so widespread,
> wouldn't it have been easier to have accepted them into the standard, than
> have to be point them out every time? Especially as the compilers used don't
> seem to be complaining.


I'm not happy with that idea, but I could live with it. However, in this
particular case his definition of the body of main() attempts to return
a value. Even if support for void main() were made mandatory, I think
that returning a value from main() when it has been declared as not
returning a value should still be a constraint violation.
--
James Kuyper
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-02-2011
On 11/02/2011 06:53 AM, Nick Keighley wrote:
> On Nov 1, 3:27�pm, zemir <(E-Mail Removed)> wrote:
>
>> [...] i am writing a
>> small programm to calculate the perimeter and the area of a circle;
>> this is:
>>
>> #include <stdio.h>;
>> #include <math.h>;

>
> there should not be a semi-colon ( after a #include (or indeed any
> pre-processor directive)



Every preprocessor directive includes, as part of the directive, the
terminating new-line. Therefore, "a semi-colon ( after a ...
preprocessor directive" would have to look like this:

#include <stdio.h>
;

I think what you mean is that they cannot have a ';' as the last
character before the terminating new-line.

Comments get removed during translation phase 3, while preprocessing
directives are not translated until phase 4, so

#include <stdio.h> //;

would also be legal. I assume that you meant a ';' that was not part of
a comment.

There are still four kinds of preprocessing directives which can have a
';' as the last character before the terminating new-line, even if it's
not part of a comment:

#define semicolon ;
#This qualifies as a pre-processing directive (6.10p3);
#error This is an error;
#pragma highlight red ;

However, those are the only four.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[I'm learning C]: Learning to use ucontext Andrey Popp C Programming 5 01-31-2012 01:05 AM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
having a little problem with some code for a little game I am creating. ThaDoctor C++ 3 09-28-2007 03:28 PM
Learning C and Learning Make/Configure/Building/Linking Hal Vaughan C Programming 7 03-21-2006 05:07 PM
e-learning, (collaborative learning environment) collinm Java 1 09-08-2005 09:52 PM



Advertisments