Thread: Unexpected behaviour of a simple C program

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    3

    Question Unexpected behaviour of a simple C program

    Hi everybody
    I cannot understand why I cannot insert the second char variable.
    The program goes from the first character input directly to the first integer input.

    Unexpected behaviour of a simple C program-png

    Unexpected behaviour of a simple C program-b-png

    The program is written using codeBlocks 17.12.
    Can anybody help me?
    Cheers

    Luca

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why are you reading names as single chars rather than as strings?

    Anyway, the problem is that when you enter the first char, that enter is read as the second char, so it is read before you can actually enter your second char.
    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
    Registered User
    Join Date
    Feb 2019
    Posts
    3
    Thanks for your reply.
    If that is the problem, I cannot understand how to prevent it to happen while maintainig the structure of the program with those two distinct questions.
    Btw I used char variables because I just ask 1 letter at the time.
    Cheers

    Luca

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Lucky Luka
    If that is the problem, I cannot understand how to prevent it to happen while maintainig the structure of the program with those two distinct questions.
    Basically, the idea is that you need to read and discard the newline from the enter. This can be done by changing the format string for reading the second char to add a leading space to match the whitespace:
    Code:
    scanf(" %c", &last);
    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

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    3
    It works!
    Thank you!
    Cheers

    Luca

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected behaviour of pthread_cond_broadcast
    By Imago in forum C Programming
    Replies: 1
    Last Post: 01-17-2019, 12:10 PM
  2. Very simple program with unexpected output
    By Draylath in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2012, 10:38 AM
  3. Unexpected variable behaviour
    By Dondrei in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2009, 12:11 AM
  4. Unexpected behaviour with float
    By j.sreejit in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 09:53 PM
  5. Unexpected behaviour
    By fnoyan in forum C++ Programming
    Replies: 5
    Last Post: 03-05-2005, 09:45 AM

Tags for this Thread