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?
:(
Printable View
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?
:(
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 :)
Technical Content in Chit Chat Alert
Bugger off! :pQuote:
Originally posted by Jim Brown
Technical Content in Chit Chat Alert
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.
I know you don't really mean that, and that you're just upset about Saturday's game.....Quote:
Originally posted by rjlohan
Bugger off! :p
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.Quote:
Originally posted by kedaman
I willl have a look, btw "LOW <= t <= HIGH" you're comparting a boolean (LOW<=t) with HIGH
I'm just upset that I turned it off after 55 minutes... :rolleyes:Quote:
Originally posted by Jim Brown
I know you don't really mean that, and that you're just upset about Saturday's game.....
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?
The value of t should be passed to the external function translate if LOW <= t <= HIGH, otherwise the error message should be displayed.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;
}
It's just a simple program, but that scanf thing is a problem.
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...
Well couldn't you use %S instead in scanf
if (scanf("%S", ws) != 0) {
And then just check yourself the validity of ws ?
How do I validate a string as an integer in C?
(My C knowledge is limited...)
I don't know C whatsoever, only java.
I just looked up scanf in MSDN :)
D'oh! Thanks anyway. :)
well atof should return 0 if the conversion fails..
i'm still very unsure what you are trying to achieve ;)
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.
well you go like this:
if (scanf("%s", s)) i= atoi(ws);
s could of course be "0", so a *s!='0' would help
Why atio(ws) when you scanned into s?
Typo?
yeah sorry