Thread: scanf, getchar, gets, etc. are ignored.

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    14

    scanf, getchar, gets, etc. are ignored.

    I've managed to write up two lovely, wonderfully working programs that calculate, respectively, linear and quadratic equations. However, there are problems when I try to combine the two. If I put them in a while loop, or even an if statement (necessary to give the user a choice between linear and quadratic), the program will display the first printf line of the function and ignore the rest, going back to the loop, or ending the program. Any idea why it does this or how to fix it? I'm using the Linux GCC i686 compiler, but this code shouldn't be OS-specific.

    If it's helpful, I've attached a copy of one of the offending programs. The code under the primary 'if' statements are basically exactly the same as the two programs I've already written that work perfectly (yes, I know the code is somewhat inefficient at parts, but I'm fairly new to C).

    Thank you for any help you may be able to give.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    /*The only reason main() is an int is to eliminate these annoying warnings my
    compiler throws if it's a void.*/
    Those warning are correct. main() always returns an int.
    void main() is accepted by some compilers, but it's still wrong.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    I'd like to point out that your quadratic equation solver is conceptually flawed, too.

    Code:
    /*If 'n' is negative, don't continue (too lazy to do a function including 'i'
    right now, maybe later.*/
    		if (n<1)
    If it's less than 1, it doesn't have to be negative, does it? No, it can be anything from 1 to 0, and 0, too.

    For quadratic equations, if the discriminant(b^2 - 4ac) is:

    < 0 = No solution.
    = 0 = One solution; (-b) / (2a)
    > 0 = Two solutions. The whole formula; (-b +/- sqrt(b^2 - 4ac))/(2a).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cpjust View Post
    void main() is accepted by some compilers, but it's still wrong.
    Or more precisely, it's undefined!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    14
    As to the n<1 thing, yeah, that was just a typo, when I actually compiled the program, it was 0. And as for the void/int thing for the main function, it doesn't much matter for the programs that I write, because I don't need the main function to return anything. When I do make the main function an int to satisfy the compiler, I just have it return no error (value of 0). When it has to return something, I do, of course, make sure it's an int. As for the compiler/code ignoring the standard input commands, any ideas?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    And as for the void/int thing for the main function, it doesn't much matter for the programs that I write, because I don't need the main function to return anything. When I do make the main function an int to satisfy the compiler, I just have it return no error (value of 0). When it has to return something, I do, of course, make sure it's an int.
    I think you're missing the point. The standard states that main() shall return an int (on a hosted environment), so declaring it with a void return-type is non-standard.

    As for the compiler/code ignoring the standard input commands, any ideas?
    I tested by compiling with the MinGW port of gcc 3.4.5 on MS Windows XP, and it seems to accept my selection of linear/quadratic at the very least.

    What I suggest is that you go back to your original code that works. That is, where you have a version that works for computing the solution of linear equations and the version that works for computing the solution of quadratic equations. Change them such that they are placed in a function. Then combine them. Now, with functions, you are less likely to make a mess.
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("&#37;c", &type);
    If you try to put a loop around this, then the second time around, the %c will pick up the newline left behind by the previous scanf call, say the scanf("%f", &c); when you chose 'q' type.

    The problem is, the %c is the only normal conversion (except for scan sets) which doesn't follow the usual scanf rule of skipping white space and newlines.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    I would suggest you learn about fgets() and sscanf() as a means of dealing with input in a predictable manner.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by daltore View Post
    And as for the void/int thing for the main function, it doesn't much matter for the programs that I write, because I don't need the main function to return anything. When I do make the main function an int to satisfy the compiler, I just have it return no error (value of 0). When it has to return something, I do, of course, make sure it's an int. As for the compiler/code ignoring the standard input commands, any ideas?
    Yes, as Laserlight mentions, you're missing the point.
    Typically, a program returns something that can be picked up by other applications to know if your app fail or succeeded or such. This value is the value that is returned by main.
    If you declare main as void, then then behavior is undefined. It can return 0, it can return mumbo jumbo and if you're unlucky, it might crash. It all depends on the compiler, because such a behavior is not defined in the standard.
    So do everyone a favor and use only int main in the future and not void main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    14
    Okay, I get the point, you don't like void main(void). But thank you for telling me about fgets and sscanf, I will look into those.

    P.S. There is a 6 byte difference between int and void for the main function, and they work exactly the same for my compiler. I learned the void main(void) from a robotics training course where it was okay to use it for that microcontroller. I shall endeavor to use the int in the future.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I learned the void main(void) from a robotics training course where it was okay to use it for that microcontroller.
    That's probably an example of a freestanding environment, so such non-standard code is expected there (and indeed the standard does not specify anything in particular concerning program startup for such an environment).
    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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here's a very interesting discussion of why void main is bad and undefined.
    Read it if you only believe we don't like it. Btw, an int is 4 bytes (can also be 2 and 8 bytes, I believe).
    http://cboard.cprogramming.com/showthread.php?t=96659
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I've never seen a system with 8-byte ints, but that doesn't mean they don't exist.

    All the standard says is that an int must be able to store at least -32767 to +32767. (Not -32768 -- read up on one's and two's complement.) Most 16-bit systems have ints this size -- i.e., 2 bytes. That's the same size as shorts. Most 32-bit systems have 4 byte ints -- -2 147 483 647 to +2 147 483 647. 64-bit systems also usually have 4 byte ints, but I suppose they could use 8-byte ones. longs or long longs on these systems are often 8 bytes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Elysia View Post
    Btw, an int is 4 bytes (can also be 2 and 8 bytes, I believe).
    Actually, the only rule for the size of int is that it has to be more or equal to short and less or equal than long

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, the only rule for the size of int is that it has to be less or equal to short and less or equal than long
    Actually, the rule is sizeof(short) <= sizeof(int) <= sizeof(long), not sizeof(int) <= sizeof(short). The other rule is as dwks stated: int must support a range of at least -32767 to +32767.
    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

  15. #15
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by laserlight View Post
    Actually, the rule is sizeof(short) <= sizeof(int) <= sizeof(long), not sizeof(int) <= sizeof(short). The other rule is as dwks stated: int must support a range of at least -32767 to +32767.
    You were faster than I, correcting it. I made a writing error; thought I corrected fast enough.
    That's what I meant that the rule was.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  2. scanf and getchar
    By axe in forum C Programming
    Replies: 7
    Last Post: 01-11-2006, 04:45 AM
  3. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  4. my program skips scanf and getchar()
    By jk81 in forum C Programming
    Replies: 15
    Last Post: 11-29-2002, 05:54 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM