Results 1 to 20 of 20

Thread: Me have 'C' problem...

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    Unhappy 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]
    -----------------------------------------

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350

    Exclamation [holds nose]Bhaaaarrrrppppp![/holds nose]

    Technical Content in Chit Chat Alert
    .

  4. #4

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    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:
    1. //check scanf return value
    2.      if (scanf("%d", &t) == 1) {
    3.          //check for quit request
    4.          if (t == -1) {
    5.          //print quit message
    6.             printf("Ok, see ya later!\n");
    7.             return 1;
    8.          } else if (LOW <= t <= HIGH) {
    9.             //print translated number
    10.             printf("%d: %s\n", t, translate(t));
    11.          } else {
    12.             //print error message
    13.             printf("ERROR: %d is outside the translatable range!\n", t);
    14.          }
    15.      }

    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]
    -----------------------------------------

  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350

    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.....
    .

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  7. #7

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  8. #8

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    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]
    -----------------------------------------

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  10. #10

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  11. #11

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  13. #13

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    How do I validate a string as an integer in C?
    (My C knowledge is limited...)
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  14. #14
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I don't know C whatsoever, only java.
    I just looked up scanf in MSDN
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  15. #15

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    D'oh! Thanks anyway.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  17. #17

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  19. #19

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Why atio(ws) when you scanned into s?

    Typo?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yeah sorry
    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
  •  



Click Here to Expand Forum to Full Width