Thread: if statement reads char value as variable

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    if statement reads char value as variable

    This is my second time using the char variable so please forgive the silly question. How do I make this work? It's towards the bottom.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
       int inLoop = 0, outLoop = 0, input;
       int numberOne, numberTwo;
       char inputChar;
    
       while (outLoop == 0) {
          srand( time(NULL) );
    
          while (inLoop == 0) {
             numberOne = rand();
             numberTwo = rand();
    
             printf("What is %d times %d? ", numberOne, numberTwo);
             scanf("%d", &input);
    
             if (input == (numberOne * numberTwo) ) {
                printf("Correct!\n");
                inLoop = 1;
             }
             else
                printf("Wrong, try again\n");
          }
    
    // --------------------------This is where the problem is occuring -------------------------------------------
       while (inputChar != y && inputChar != Y && inputChar != n && inputChar != N) {
          printf("Would you like to do another one(y or n)? ");
          scanf("%c", &inputChar);
    
          if (inputChar == y || inputChar == Y)
             continue;
          else if(inputChar == n || inputChar == N)
             inLoop = 1;
          else
             printf("You can only enter y or n\n");
       }
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I think y, Y, n, and N need to be in single quotes (i.e., 'y', 'Y', etc.).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > srand( time(NULL) );
    You should only do this once in the life of the program.
    In particular, doing it more than once a second is a really bad thing, it makes all your following rand() calls return the same values.

    > if (inputChar == y || inputChar == Y)
    Single quotes are needed, like
    if (inputChar == 'y' || inputChar == 'Y')
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 08:36 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM