|
-
Aug 19th, 2002, 04:41 AM
#1
Thread Starter
PowerPoster
Me have 'C' problem...
How can I 'error check' the scanf function so that when I do:
scanf("%d", &t);
it won't go berserk if a non-decimal number (or character) is entered?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 04:49 AM
#2
transcendental analytic
not sure what you mean, i'm used to C++ and not C, but what if you scanf a string and then use atof to test the conversion
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 19th, 2002, 04:50 AM
#3
Hyperactive Member
[holds nose]Bhaaaarrrrppppp![/holds nose]
Technical Content in Chit Chat Alert
-
Aug 19th, 2002, 04:52 AM
#4
Thread Starter
PowerPoster
Re: [holds nose]Bhaaaarrrrppppp![/holds nose]
Originally posted by Jim Brown
Technical Content in Chit Chat Alert
Bugger off! 
This is what I mean:
VB Code:
//check scanf return value
if (scanf("%d", &t) == 1) {
//check for quit request
if (t == -1) {
//print quit message
printf("Ok, see ya later!\n");
return 1;
} else if (LOW <= t <= HIGH) {
//print translated number
printf("%d: %s\n", t, translate(t));
} else {
//print error message
printf("ERROR: %d is outside the translatable range!\n", t);
}
}
When scanf receives a character, it goes berserk and keeps printing some line over and over, non stop. It has to be killed.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 04:55 AM
#5
Hyperactive Member
Re: Re: [holds nose]Bhaaaarrrrppppp![/holds nose]
Originally posted by rjlohan
Bugger off! 
I know you don't really mean that, and that you're just upset about Saturday's game.....
-
Aug 19th, 2002, 04:55 AM
#6
transcendental analytic
I willl have a look, btw "LOW <= t <= HIGH" you're comparting a boolean (LOW<=t) with HIGH
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 19th, 2002, 05:01 AM
#7
Thread Starter
PowerPoster
Originally posted by kedaman
I willl have a look, btw "LOW <= t <= HIGH" you're comparting a boolean (LOW<=t) with HIGH
Ta. I didn't really know if that would work. I was gonna test it, but at this stage the scanf prob is my main one.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 05:01 AM
#8
Thread Starter
PowerPoster
Re: Re: Re: [holds nose]Bhaaaarrrrppppp![/holds nose]
Originally posted by Jim Brown
I know you don't really mean that, and that you're just upset about Saturday's game.....
I'm just upset that I turned it off after 55 minutes...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 05:04 AM
#9
transcendental analytic
whatever it is, its not doing it to me, it writes both characters and numbers without problem, can you show me all of your code?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 19th, 2002, 05:07 AM
#10
Thread Starter
PowerPoster
Code:
//external libraries
#include <stdio.h>
#include <errno.h>
#define LOW 1
#define HIGH 1
#define LANG "English"
//external functions
extern char* translate(int number);
//main function
int main(int argc, char* argv[])
{
//number to be translated
int t = -1;
//Print title
printf("### %s Number Translator ###\n", LANG);
//run program
while (1)
{
//allow user to request a number translation
printf("Enter a number (%d-%d) to be translated (-1 to quit):", LOW, HIGH);
//check scanf return value
if (scanf("%d", &t) != 0) {
//check for quit request
if (t == -1) {
//print quit message
printf("Ok, see ya later!\n");
return 1;
} else if ((LOW <= t) && (t <= HIGH)) {
//print translated number
printf("%d: %s\n", t, translate(t));
} else {
//print error message
printf("ERROR: %d is outside the translatable range!\n", t);
}
}
}
//return from program
return 1;
}
The value of t should be passed to the external function translate if LOW <= t <= HIGH, otherwise the error message should be displayed.
It's just a simple program, but that scanf thing is a problem.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 05:12 AM
#11
Thread Starter
PowerPoster
It keeps printing the line from:
printf("Enter a number (%d-%d) to be translated (-1 to quit):", LOW, HIGH);
when it goes berserk, and puts a 0 there. It's like the scanf function is never called again...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 05:14 AM
#12
Retired VBF Adm1nistrator
Well couldn't you use %S instead in scanf
if (scanf("%S", ws) != 0) {
And then just check yourself the validity of ws ?
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 19th, 2002, 05:15 AM
#13
Thread Starter
PowerPoster
How do I validate a string as an integer in C?
(My C knowledge is limited...)
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 05:19 AM
#14
Retired VBF Adm1nistrator
I don't know C whatsoever, only java.
I just looked up scanf in MSDN
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 19th, 2002, 05:25 AM
#15
Thread Starter
PowerPoster
D'oh! Thanks anyway.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 05:30 AM
#16
transcendental analytic
well atof should return 0 if the conversion fails..
i'm still very unsure what you are trying to achieve
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 19th, 2002, 05:49 AM
#17
Thread Starter
PowerPoster
All the code is meant to do is accept an integer 0-10, and return the translation in a selection of languages. It's a uni assignment, the point of which is optional compilation through Unix makefiles.
This scanf thing was just causing me troubles though.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 05:59 AM
#18
transcendental analytic
well you go like this:
if (scanf("%s", s)) i= atoi(ws);
s could of course be "0", so a *s!='0' would help
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 19th, 2002, 06:00 AM
#19
Thread Starter
PowerPoster
Why atio(ws) when you scanned into s?
Typo?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Aug 19th, 2002, 06:07 AM
#20
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|