problem

K

kapil_ghai2003

Dear sir,
I want to input an integer value from keyboard and display on screen
"WELL DONE" that many times, how can i do this.
 
R

Richard Heathfield

(e-mail address removed) said:
Dear sir,
I want to input an integer value from keyboard and display on screen
"WELL DONE" that many times, how can i do this.

By writing a C program, testing it, and executing it.
 
P

per9000

Dear sir,
I want to input an integer value from keyboard and display on screen
"WELL DONE" that many times, how can i do this.

In case no one replied. Here's a solution that works for me:

#include <stdio.h>

int main(int argc, char * argv[])
{
int number;
int counter;

printf("Enter number >> ");

scanf("%d", &number);

/* allow values 1-99 only */
if (number > 0 && number < 100)
for(counter = 0; counter < number; counter++)
printf("WELL DONE\n");
else
printf("bad number.");

return(0);
}

How I compiled it
C:\temp>gcc -ansi -pedantic well_done.c -o well_done.exe

Tw examples of usage:
C:\temp>well_done.exe
Enter number >> foo
bad number.

C:\temp>well_done.exe
Enter number >> 2
WELL DONE
WELL DONE

Also see f.x:
http://en.wikibooks.org/wiki/C_Programming/Simple_Input_and_Output

Also: try to solve your problems by using your favorite search engine
- you learn a lot from it.

/Per

--

Per Erik Strandberg
..NET Architect - Optimization
Tomlab Optimization Inc.
http://tomopt.com/tomnet/
 
C

Chris Dollin

I want to input an integer value from keyboard and display on screen
"WELL DONE" that many times, how can i do this.

int main( void )
{
int n = getIntegerValueFrom( "keyboard" );
while (n > 0) displayOnScreen( "WELL DONE" );
return 0;
}

Easy-peasy. Now you just have to write `getIntegerValueFrom` (note that
Standard C doesn't know about "keyboards") and `displayOnScreen` (note
likewise that Standard C doesn't know about "screens").

I'd read from `stdin` and write to `stdout` myself; it's portable.
 
K

Keith Thompson

per9000 said:
In case no one replied. Here's a solution that works for me:
[code snipped]

Nobody else provided a solution for a very good reason. The original
poster was (apparently) asking us to do his homework for him.
Providing a complete solution on a silver platter doesn't do anybody
any favors.
 
P

per9000

<ot>
<perhaps also silly>

[...] The original
poster was (apparently) asking us to do his homework for him.
Providing a complete solution on a silver platter doesn't do anybody
any favors.

You might be right. I should perhaps only have linked him to wikibooks/
C/simpleIO, but at least I DID link him to wikibooks/C/simpleIO :)
All people were telling the kapil guy that his solution was:
- write a C program, test it, and execute it.
- Please don't multi-post on Usenet.

Not very helpful as I see it. It seems this list sometimes is h-bar:
hostile beyond all recognition. It seems the readers of this list are
willing to take the time to tell other people they are:
- stupid
- off topic
- not following comp.lang.c.netiquette

Why not just ignore a message you find stupid instead of replying? OK,
it does not feed the ego, but it's a lot easier. Or send a quick hint
like "scanf + printf" (like I should have done).

/Per

--

Per Erik Strandberg
..NET Architect - Optimization
http://tomopt.com/tomnet/

</perhaps also silly>
</ot>
 
R

Richard Bos

per9000 said:
Why not just ignore a message you find stupid instead of replying?

Because it does not teach the beginning schoolboy that getting someone
else to do your homework for you (as you did) is the perfect way to fail
three years on (so you did him a _great_ service - well done).

Richard
 
I

Ian Malone

per9000 said:
[...] The original
poster was (apparently) asking us to do his homework for him.
Providing a complete solution on a silver platter doesn't do anybody
any favors.

All people were telling the kapil guy that his solution was:
- write a C program, test it, and execute it.

> Not very helpful as I see it.

It's pretty useful if the OP takes it to heart. If they're
doing homework on C then you might imagine they'd be doing
a course on C, in which case it would work out better if
they actually try to learn C.
 
R

Richard Heathfield

per9000 said:

Why not just ignore a message you find stupid instead of replying?

Firstly, the message wasn't stupid, exactly. It appeared to be an
attempt by the OP to avoid doing his homework. Many people in clc
consider this to be dishonest, and are not shy about saying so.

Secondly, if you don't like the replies made by people like Keith, why
not just ignore them instead of replying? Because you had something to
say? Right. Well, so did Keith. And so did I.
OK, it does not feed the ego,

So what? The best help I can give the OP is to suggest that he writes
his own programs. Otherwise, he'll never learn how to write good C.
This isn't about ego, but about altruism. Anyone who is here for the
good of their ego is in the wrong place: comp.lang.c is notorious for
ego-busting.
but it's a lot easier. Or send a quick hint
like "scanf + printf" (like I should have done).

....but instead you wrote a program that demonstrated your lack of regard
for scanf's return value. So much for ego.
 
C

Charlton Wilbur

KT> Nobody else provided a solution for a very good reason. The
KT> original poster was (apparently) asking us to do his homework
KT> for him. Providing a complete solution on a silver platter
KT> doesn't do anybody any favors.

Except for that the solution shows several hallmarks of having been
written by an experienced programmer; if the professor is any good
he'll twig to them immediately, and the student will be worse off.

Not that this justifies doing homework, but if you're going to do
someone's homework, it's worthwhile to make sure that it won't be
mistaken for his or her homework.

Charlton
 
P

per9000

[...]
...but instead you wrote a program that demonstrated your lack of regard
for scanf's return value. So much for ego.
[...]

It seems me/my ego has been smashed many times today. Just once could
have been random bad luck, but it seems I deserve it. I will repent.

/P
 
C

Chris Dollin

Chris said:
while (n > 0) displayOnScreen( "WELL DONE" );

Grrrr. `while (n-- > 0) displayOnScreen( "WELL DONE" );`.

I /thought/ the --. I just didn't /type/ it.
 
C

Christopher Benson-Manica

Grrrr. `while (n-- > 0) displayOnScreen( "WELL DONE" );`.
I /thought/ the --. I just didn't /type/ it.

I noticed the infinite loop and figured it was intentional - after
all, the OP never stated that the program was required to display
"WELL DONE" *exactly* n times. (Although I suppose it does beg the
question of why you would read a value for n and then not actually do
anything useful with it.)
 
C

Chris Dollin

Christopher said:
I noticed the infinite loop and figured it was intentional - after
all, the OP never stated that the program was required to display
"WELL DONE" *exactly* n times. (Although I suppose it does beg the
question of why you would read a value for n and then not actually do
anything useful with it.)

Well, if `n` is zero then no WELLDONE's get displayed. Is that useful?
(Probably is if you can't find the interrupt key.)
 
K

Kenneth Brody

Dear sir,
I want to input an integer value from keyboard and display on screen
"WELL DONE" that many times, how can i do this.

Sure. Here's my initial untested version. You should probably add
some error checking. Let me know what grade you get on this assignment.

#include <stdio.h>

void main()
{
char *intbuf, *buffer;
int i;

printf("Enter an integer: ");
gets(intbuf);

buffer = (char *)malloc(sizeof("exit 999"));
sprintf(buffer,"exit %s",intbuf);
i = system(buffer2);

while ( i-- )
printf("WELL DONE");

}

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

Jens Thoms Toerring

per9000 said:
int number;
int counter;
printf("Enter number >> ");
scanf("%d", &number);
/* allow values 1-99 only */
if (number > 0 && number < 100)
for(counter = 0; counter < number; counter++)
printf("WELL DONE\n");
else
printf("bad number.");

This will only work by (bad?) luck: if the user didn't enter
a number you end up with testing some random value stored in
'number' (which could be a number between 1 and 99). Hint:
scanf() has a return value that tells you how many items
where matched. Moreover, what you output via the first call
of printf() could very well stay stuck in the internal buf-
fer, so the user may not see it. That's always a problem if
a line doesn't either end in a '\n' or you fflush() stdout.

Regards, Jens
 
R

Richard Harter

Sure. Here's my initial untested version. You should probably add
some error checking. Let me know what grade you get on this assignment.

#include <stdio.h>

void main()
{
char *intbuf, *buffer;
int i;

printf("Enter an integer: ");
gets(intbuf);

buffer = (char *)malloc(sizeof("exit 999"));
sprintf(buffer,"exit %s",intbuf);
i = system(buffer2);

while ( i-- )
printf("WELL DONE");

}

There are, ah, a few problems with this code. You wouldn't be trying to
get the OP into trouble would you?
 
F

Flash Gordon

Richard Harter wrote, On 09/05/07 16:37:
There are, ah, a few problems with this code. You wouldn't be trying to
get the OP into trouble would you?

He can't be, some of the lines are correct!
 
K

Keith Thompson

Charlton Wilbur said:
KT> Nobody else provided a solution for a very good reason. The
KT> original poster was (apparently) asking us to do his homework
KT> for him. Providing a complete solution on a silver platter
KT> doesn't do anybody any favors.

Except for that the solution shows several hallmarks of having been
written by an experienced programmer; if the professor is any good
he'll twig to them immediately, and the student will be worse off.

Not that this justifies doing homework, but if you're going to do
someone's homework, it's worthwhile to make sure that it won't be
mistaken for his or her homework.

The traditional way to do that is to provide an obfuscated solution
that satisfies the requirements, but that the student is unlikely to
be able to understand or explain.

Such an approach is arguably unfair to the student who might fall for
it. I prefer simply to tell the questioner to do his own homework (or
perhaps to offer to submit the solution directly to the instructor).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top