Input Length

M

Markus Pitha

Hello,

What's the best way, to solve the difficulty of too big entries and
resulting wrong calculations of the program.
I want to give an example:

Imagine I have a calculation program and the first variable is int
(-32768 to 32767). So when I allow the user to enter values up to 32767
and the user adds then 10 to this value, it is out of range.
Or what should I do, when the user takes the square of 30000, it's then
out of range too.
What's the best method to solve such problems. Would it make sense to
choose the variables as big as possible, and restrict the user's input
to a number wide under this range?
I also thought about reading just a specific count of digits with fgets
from stdin, but when I enter some more digits, fgets saves the remaining
digits and writes it into the keyboard buffer.
What can I do?



Markus.
 
C

CBFalconer

Markus said:
What's the best way, to solve the difficulty of too big entries
and resulting wrong calculations of the program.
.... snip ...

See my post of 2006-01-31, subject line "checking and verifying
input line in a C program", in this newsgroup (c.l.c). Basically
use the provided streams, without buffers, and base things on
acquiring unsigned values. That code also illustrates prechecking
that a calculation will not overflow, based on limits.h.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 
F

Flash Gordon

Markus said:
Hello again,

Please provide context. It is entirely possible some people will *never*
see the article you are responding to. For information on how to post
through Google please read and follow the instructions in
http://cfaj.freeshell.org/google/
what do you think about my solution?

#include <stdio.h>

int main(void) {
int c;
int i = 0;
char str[11];

while( ((c = getchar()) != '\n') ) {
if (i < 10) {

Using magic numbers is asking for trouble. Later on you will change the
size of str and forget to change it here.
if (i < (sizeof str) - 1) {
str = c;
}
i++;
}
str[10] = '\0';


Yes another place you have to remember to change it.
str[(sizeof str) - 1] = '\0';
printf("Output: %s\n", str);

return 0;
}


It seems to be safe, or at least I wouldn't know, how to outfox it.

Breaking your program is easy in theory (and on some systems in
practice). Generate an end of file condition before sending a new line.
Then you will enter an infinite loop since getchar will continually
return EOF.
 
M

Markus Pitha

Hello again,

what do you think about my solution?

#include <stdio.h>

int main(void) {
int c;
int i = 0;
char str[11];

while( ((c = getchar()) != '\n') ) {
if (i < 10) {
str = c;
}
i++;
}
str[10] = '\0';

printf("Output: %s\n", str);

return 0;
}


It seems to be safe, or at least I wouldn't know, how to outfox it.


Markus.
 
M

Markus Pitha

Forget it, because It's still buffered! When I use a loop, I still get
values as long as the first string was, hmmm.
 
M

Markus Pitha

Hello,

Flash said:
Breaking your program is easy in theory (and on some systems in
practice). Generate an end of file condition before sending a new line.
Then you will enter an infinite loop since getchar will continually
return EOF.

Thanks I will try that tomorrow.


Best Regards,
Markus.
 
M

Mark B

Richard Heathfield said:
Mark B said:
if (i < (sizeof str) - 1) {
str[(sizeof str) - 1] = '\0';

What's the purpose of the extra parens?

They save the writer and the reader from having to reach for their
precedence tables.

Not in those examples they don't.
Had the -1 been inside of the parens (first example)
then *maybe* you could make that argument as I'm
guessing you are referring to the order of operations
between the relational and additive operators.
Any aforementioned reader/writer that knows that
the sizeof operator is listed in the precedence table
also knows where it ranks in relation to the relational
and additive operators.
It doesn't look stupid to me.

To each his own I guess... but it most definately
looks stupid to me.
 
F

Flash Gordon

Richard said:
Mark B said:
if (i < (sizeof str) - 1) {
str[(sizeof str) - 1] = '\0';
What's the purpose of the extra parens?

They save the writer and the reader from having to reach for their
precedence tables.

Yes, that is my reason for using them. It's also why when using malloc I
write:
ptr = malloc(n * sizeof *ptr)
instead of
ptr = malloc(sizeof *ptr * n)
It doesn't look stupid to me.

Nor me, obviously ;-)
 
C

CBFalconer

Markus said:
Forget it, because It's still buffered! When I use a loop, I still
get values as long as the first string was, hmmm.

If you continue to ignore advice and fail to provide proper context
you will find that people simply plonk you (automatically ignore
all your postings) to avoid the nuisance. See my sig. below.

Broken google is not a good interface to usenet.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Markus Pitha said:
Hello,


First I wrote that in reply to _my own_ message that people know that it
didn't work. After I wrote it, I saw that it could sound rude for
readers, so I deleted it again.
Obviously it didn't get deleted so fast, sorry about that.

Ok. You need to provide context for *any* followup, even if it's to
your own message. And because of the way Usenet works, cancelling an
article often doesn't work. Articles are copied from site to site all
over the world; a cancellation often can't catch up with all copies of
the article, even for sites that recognize cancel requests.
 
C

Chris Dollin

Markus said:
I wrote to _my own_ reply, so I didn't say that to another user.

If you were replying to yourself, you didn't need to use the newsgroup.
 
M

Markus Pitha

Hello,
If you continue to ignore advice and fail to provide proper context
you will find that people simply plonk you (automatically ignore
all your postings) to avoid the nuisance. See my sig. below.

First I wrote that in reply to _my own_ message that people know that it
didn't work. After I wrote it, I saw that it could sound rude for
readers, so I deleted it again.
Obviously it didn't get deleted so fast, sorry about that.


Markus
 
C

Chris Dollin

Markus said:
Hello,



I just replied to my own message that people don't need to think about
the reply before, because my solution was not a real solution as I found
out after I already postet my message.

Then quote enough text to make sense, please - remember that other people
may see messages in a different order than you do, may never see some
messages at all, and may see messages you don't; it's intrinsic to the
medium.
 
M

Markus Pitha

Hello,

Chris said:
If you were replying to yourself, you didn't need to use the newsgroup.

I just replied to my own message that people don't need to think about
the reply before, because my solution was not a real solution as I found
out after I already postet my message.

Markus
 
M

Markus Pitha

Hello,

Keith said:
Ok. You need to provide context for *any* followup, even if it's to
your own message. And because of the way Usenet works, cancelling an
article often doesn't work. Articles are copied from site to site all
over the world; a cancellation often can't catch up with all copies of
the article, even for sites that recognize cancel requests.

Ok, I'll make it so from now on. I was used to it from different forums.

Markus
 
M

Markus Pitha

Hello,
What's the best way, to solve the difficulty of too big entries and
resulting wrong calculations of the program.

I think that I finally found a solution. It looks a little bit
unconventional, but it finally seems to work. I built an endless loop
around it, to see if the entry is buffered and is written into the
variable after the loop passes the second time.

while(1) {
i = 0;
nmbr1[0] = '\0'; // that you don't get the input of the last time,
//if you only press enter after typing nothing.
while ((c = getc(stdin)) != '\n') {
if (i < 10) {
nmbr1 = c;
nmbr1[i+1] = '\0';
}
i++;
}
printf("output: %s\n", nmbr1);
}



Markus.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top