help I'm lost

This is a discussion on help I'm lost within the C Programming forums, part of the General Programming Boards category; I have a really weird problem with my code. it all looks fine to me, but when I input x ...

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    help I'm lost

    I have a really weird problem with my code. it all looks fine to me, but when I input x it starts with a completely different number e.g. when I put in -15 it starts at 45. Can someone see the problem?

    Code:
    main()
    {
     int c,x,y,count=0;
    
     printf("Enter the starting variable:");
     x=getchar();
    
     for(count=0;count<50;count++,x++)
      {
        c=(x/((x%10)+1))%x;
        y=(x-c)%50;
        printf("X=%d		Y=%d\n",x,y);
        if(count%10==0)
          getch();
      }
     getch();
     return 0;
    }

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    you are getting a char from the keyboard, not int, use atoi to your char scanf a int.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >x=getchar();
    My initial reaction is to say that 45 is the numeric value of '-'.

    >use atoi to your char scanf a int.
    atoi should be avoided because it handles erroroneous input poorly and scanf should be avoided unless you know what you're doing (which few do). May I suggest fgets and sscanf?
    My best code is written with the delete key.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    Red face

    thanks, looks like I forgot about atoi().
    I guess it's been too long since I programmed something

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    to use scanf all I do is this
    scanf("%d", &intvar);
    while(getchar() != '\n');

    that works for me, that isn't so hard.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >that works for me
    An oft used excuse. However, you're use of scanf is still very unsafe if the call fails in some way.
    My best code is written with the delete key.

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    what is wrong with that, I know I could error check it, but then what is wrong, why not use scanf?

  8. #8
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    okay, I have one more question. It's the same program, but different topic. Maybe this just isnt' my day to program something, but I'm trying to do some file writing, and the K&R book's explanation of fprintf() isn't making enough sense to me. I've never used fprintf, just ones like fwrite() and fputc(). Can I use fprintf() in the same way as printf()?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >what is wrong with that, I know I could error check it, but then what is wrong, why not use scanf?

    Question 12.20
    Why does everyone say not to use scanf?


    http://faq.cprogramming.com/cgi-bin/...arch&match=all
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    nevermind, I answered my own questoin.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Originally posted by Prelude

    ....input poorly and scanf should be avoided unless you know what you're doing (which few do)......
    This sounds interesting - you are saying then that there are ways that scanf can be safely used? Are you aware of anyone who covers this topic in depth? (either online or in a book) I would be interested in learning more some time about the whole scanf thing, its dangers and its usefulness.

    kermit

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Wait a second - I should have read one of the previous posts more carefully - a link to Summit's FAQ... I should read whats there first possibly...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The getting lost and walking in circles
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 05-07-2008, 09:53 AM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  3. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 05:18 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21