Thread: Can somebody help me with this (easy)

  1. #1
    Registered User
    Join Date
    Nov 2003

    Can somebody help me with this (easy)

    First I wanna say that I'm new to c, and this may be a stupid question but ......whatever.......

    OK, in the begining of this program I have to write, I have to ask the user if they want to continue on with the program or not. Its something like this.......

    blah blah blah, do you want to continue (user enters "yes" or "no"). If user enters "yes" the program continues, if "no" the program ends. This is what I have done.

    char x;
    printf("Do you want to continue?? yes or no??");
    scanf("%c\n", &x);

    if (x = 'yes')
    . (the rest of the program)

    I don't get an error, I get a warning so the program still works but i wanted to know what i did wrong. Can someone help me.

  2. #2
    Registered User
    Join Date
    Jan 2004
    You are committing few errors here.

    (1) You defined variable char x to grab the user’s input. And then you check whether it’s ‘yes’ to continue with the program, right? But the word ‘yes’ is a string value, it’s not a char. You can’t insert a string into char x. I suggest to check for a char something like ‘y’ inside the if statement, so it can be inserted into x with scanf.

    (2) You can’t check something against something with ‘=’. If you say, if (x = 'yes'), it means assign ‘yes’ to x! Instead do it with ‘==’ operator, if (x == 'y').

    Your program might be working. But it will not work correctly with above errors.
    Hope you got that.

    - Kasun
    Last edited by kasun; 01-16-2004 at 03:44 AM.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Also, with your scanf("%c\n", &x); will cause problems.

    On my compiler, if you type in 'y[enter]' you need to press enter a second time before the value is accepted.

    If you type in 'yes[enter]' you will get the 'y' and the 'es[enter] will be left in the buffer for the next input command. This will cause grey hair trying to figure out how to fix it.

    Switch from scanf() to fgets(buf, sizeof(buf), stdin); for character and string input. And define buf to be a good size for general input -- longer than a person is likely to type unless he's an idiot or vindictive
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Jan 2004
    /* Quick Y/N input example
       Very simple program. We declare a character variable. Then we begin an infinite loop,
       and prompt the user. After tossing the first letter of the user's input into the variable
       via the getchar() function, we check the value of the input to see if it is a Y or N.
       If it is a Y then we break the loop and continue.
       If it is a N then you deal with it however you want. In this case we exit the program. Note
       that we check for the lower (a) and uppercase (A) versions of the letter, as they are
       totally different values.
       If it is meaningless input then the loop repeats, prompting the user for input again.
       There are a lot of things I would do to make this program a lot better (error checking,
       clearing out the keyboard buffer, etc.), but for the sake of simplicity this gets the
       point across.
    #include <stdio.h>
    int main()
    {	char YesNo;
    	for (;;) 						/* Infinite loop. */
    	{	printf ("Continue? (y/n): ");
    		if (YesNo == 'y' || YesNo == 'Y') break;		/* Note - || means "or" */
    		if (YesNo == 'n' || YesNo == 'N') return 1;
    	printf("Continue program here.\n");
    	return 0;
    While I don't want to do your homework for you, the best way to learn is to look at code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM