Thread: What is wrong in tnis code?

  1. #1
    zach
    Guest

    What is wrong in tnis code?

    Code:
        char buf;
        fgets(buf,sizeof(buf),stdin);
        if(buf=='1') writeBill();
    What is wrong in this code?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You declared buf to be a single char rather than an array of char. For storing a null terminated string, a single char can only ever store an empty string (i.e., a string with a length of 0). Also, this means that you are passing an argument of a wrong type as the first argument to fgets; your compiler should have warned you so you need to compile at a high warning level and pay attention to warnings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    You declared buf to be a single char rather than an array of char. For storing a null terminated string, a single char can only ever store an empty string (i.e., a string with a length of 0). Also, this means that you are passing an argument of a wrong type as the first argument to fgets; your compiler should have warned you so you need to compile at a high warning level and pay attention to warnings.
    Yes, a nice little mess. sorry. Zach

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this code!!!
    By agnit.thebest in forum C Programming
    Replies: 5
    Last Post: 07-07-2010, 10:18 PM
  2. What's wrong with this code?
    By Code Thug in forum C++ Programming
    Replies: 16
    Last Post: 03-04-2010, 01:36 PM
  3. What's wrong with this code?
    By ManiacBR in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2006, 05:41 PM
  4. something wrong with code
    By dankassdann in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2006, 11:27 AM
  5. What's wrong with my code?
    By Codefish in forum C++ Programming
    Replies: 4
    Last Post: 07-27-2005, 05:19 PM

Tags for this Thread