Thread: Basic Questions

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    4

    Basic Questions

    Hi, I'm an absolute beginner to coding in C, and I'm attempting to practice by writing a program where a user responds to Yes/No questions in a series, like if it were a game. The problem that I'm having is that after the first question is answered, and the second pops up, the program doesn't give me (the user) a chance to respond to it; the program abruptly ends. Obviously, there must be something simple that I'm missing here to go from one question to another without it terminating. I'd appreciate any advice.

    Here's what I've made so far:

    Code:
    #include <stdio.h>
    
    char answer, answertwo;
    
    int main()
    {
    
    printf ("Are you laughing? (Y/N)\n" );
    scanf ("%c", &answer);
    
    if (answer == 'y' || answer == 'Y')
    printf("\nGood\n");
    else if (answer == 'n' || answer == 'N')
    printf("\nBye\n");
    
    
    printf("Do you want to read? (Y/N)\n ");
    scanf ("%c, &answertwo");
    if (answertwo == 'y' || answertwo == 'Y')
    printf("\nGood\n");
    else if (answertwo == 'n' || answertwo == 'N')
    printf("\nBye\n");
    
    return 0;
    
    }
    Last edited by NightTower; 02-02-2017 at 04:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Ordinarily, when scanf is processing your input, it will ignore all spaces, tabs and newlines.

    But this doesn't happen when you're using the %c conversion, it will take the next character regardless.
    Try this program to see.
    Code:
    #include <stdio.h>
     
    int main()
    {
    /* There was no need for these to be global variables */
    char answer, answertwo;
     
    printf ("Are you laughing? (Y/N)\n" );
    scanf ("%c", &answer);
    printf("You entered character %d\n", answer );
    if (answer == 'y' || answer == 'Y')
    printf("\nGood\n");
    else if (answer == 'n' || answer == 'N')
    printf("\nBye\n");
     
     
    printf("Do you want to read? (Y/N)\n ");
    scanf ("%c, &answertwo");
    printf("You entered character %d\n", answertwo );
    if (answertwo == 'y' || answertwo == 'Y')
    printf("\nGood\n");
    else if (answertwo == 'n' || answertwo == 'N')
    printf("\nBye\n");
     
    return 0;
     
    }
    You should see that you get 10 printed (which is the ASCII for newline) at the second print statement.

    Fortunately, the solution is very easy.
    Code:
    /* Put a leading space before the %c character to skip leading blank characters */
    scanf (" %c", &answer);
    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.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Quote Originally Posted by Salem View Post
    Ordinarily, when scanf is processing your input, it will ignore all spaces, tabs and newlines.

    But this doesn't happen when you're using the %c conversion, it will take the next character regardless.
    Try this program to see.
    Code:
    #include <stdio.h>
     
    int main()
    {
    /* There was no need for these to be global variables */
    char answer, answertwo;
     
    printf ("Are you laughing? (Y/N)\n" );
    scanf ("%c", &answer);
    printf("You entered character %d\n", answer );
    if (answer == 'y' || answer == 'Y')
    printf("\nGood\n");
    else if (answer == 'n' || answer == 'N')
    printf("\nBye\n");
     
     
    printf("Do you want to read? (Y/N)\n ");
    scanf ("%c, &answertwo");
    printf("You entered character %d\n", answertwo );
    if (answertwo == 'y' || answertwo == 'Y')
    printf("\nGood\n");
    else if (answertwo == 'n' || answertwo == 'N')
    printf("\nBye\n");
     
    return 0;
     
    }
    You should see that you get 10 printed (which is the ASCII for newline) at the second print statement.

    Fortunately, the solution is very easy.
    Code:
    /* Put a leading space before the %c character to skip leading blank characters */
    scanf (" %c", &answer);
    I'm too basic to follow most of your explanation, but I still attempted to build the program using your edits, and the program now gives me a "You entered 89," after I respond to the first question, and then it actually prompts the second this time, but crashes once I answer.

    This is the code, copy/pasted from your edits, that I used to build it:

    Code:
    #include <stdio.h>
    
    int main()
    {
    /* There was no need for these to be global variables */
    char answer, answertwo;
    
    printf ("Are you laughing? (Y/N)\n" );
    scanf (" %c", &answer);
    printf("You entered character %d\n", answer );
    if (answer == 'y' || answer == 'Y')
    printf("\nGood\n");
    else if (answer == 'n' || answer == 'N')
    printf("\nBye\n");
    
    
    printf("Do you want to read? (Y/N)\n ");
    scanf (" %c, &answertwo");
    printf("You entered character %d\n", answertwo );
    if (answertwo == 'y' || answertwo == 'Y')
    printf("\nGood\n");
    else if (answertwo == 'n' || answertwo == 'N')
    printf("\nBye\n");
    
    return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Quote Originally Posted by Salem View Post
    Ordinarily, when scanf is processing your input, it will ignore all spaces, tabs and newlines.

    But this doesn't happen when you're using the %c conversion, it will take the next character regardless.
    Try this program to see.
    Code:
    #include <stdio.h>
     
    int main()
    {
    /* There was no need for these to be global variables */
    char answer, answertwo;
     
    printf ("Are you laughing? (Y/N)\n" );
    scanf ("%c", &answer);
    printf("You entered character %d\n", answer );
    if (answer == 'y' || answer == 'Y')
    printf("\nGood\n");
    else if (answer == 'n' || answer == 'N')
    printf("\nBye\n");
     
     
    printf("Do you want to read? (Y/N)\n ");
    scanf ("%c, &answertwo");
    printf("You entered character %d\n", answertwo );
    if (answertwo == 'y' || answertwo == 'Y')
    printf("\nGood\n");
    else if (answertwo == 'n' || answertwo == 'N')
    printf("\nBye\n");
     
    return 0;
     
    }
    You should see that you get 10 printed (which is the ASCII for newline) at the second print statement.

    Fortunately, the solution is very easy.
    Code:
    /* Put a leading space before the %c character to skip leading blank characters */
    scanf (" %c", &answer);
    Ohh never mind; I see what you mean. I didn't realize what you meant at first. Thanks! I'm assuming the space allows for new input, then?

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Quote Originally Posted by Salem View Post
    Ordinarily, when scanf is processing your input, it will ignore all spaces, tabs and newlines.

    But this doesn't happen when you're using the %c conversion, it will take the next character regardless.
    Try this program to see.
    Code:
    #include <stdio.h>
     
    int main()
    {
    /* There was no need for these to be global variables */
    char answer, answertwo;
     
    printf ("Are you laughing? (Y/N)\n" );
    scanf ("%c", &answer);
    printf("You entered character %d\n", answer );
    if (answer == 'y' || answer == 'Y')
    printf("\nGood\n");
    else if (answer == 'n' || answer == 'N')
    printf("\nBye\n");
     
     
    printf("Do you want to read? (Y/N)\n ");
    scanf ("%c, &answertwo");
    printf("You entered character %d\n", answertwo );
    if (answertwo == 'y' || answertwo == 'Y')
    printf("\nGood\n");
    else if (answertwo == 'n' || answertwo == 'N')
    printf("\nBye\n");
     
    return 0;
     
    }
    You should see that you get 10 printed (which is the ASCII for newline) at the second print statement.

    Fortunately, the solution is very easy.
    Code:
    /* Put a leading space before the %c character to skip leading blank characters */
    scanf (" %c", &answer);
    Hey I have a follow-up question: How could I make the program repeat itself at the end? Let's say the last question were, "Do you want to play again? Y/N" How could I make the program repeat itself? I've tried searching for loop explanations, but I'm having trouble understanding them, or if that's even what I need to be looking for.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Loops are exactly what you want to use for that purpose. If you're learning from a book, loops should be covered fairly early on (if you're not learning from a book, I recommend you do).

    You can also find plenty of tutorials online, such as here.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    scanf ("%c, &answertwo");
    What could be wrong here?
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Few Basic Questions
    By Euclid365BCE in forum C Programming
    Replies: 2
    Last Post: 10-21-2015, 07:46 AM
  2. Basic Questions
    By Panda_ in forum C Programming
    Replies: 15
    Last Post: 09-23-2009, 04:24 PM
  3. Some Basic questions
    By BlaX in forum C Programming
    Replies: 7
    Last Post: 06-30-2009, 09:51 AM
  4. hello. basic questions
    By johnnyboy in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2008, 10:58 AM
  5. Basic questions about C
    By FlatLost in forum C Programming
    Replies: 6
    Last Post: 11-01-2005, 01:08 PM

Tags for this Thread