Results 1 to 3 of 3

Thread: Comparing two characters

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Question Comparing two characters

    Hello,

    I'm new to C++ and I was wondering how to compare two characters... Right now I'm using:

    Code:
        char *o;
        printf("Try Again? (n/y): ");
        scanf("%c", &o);
        int res = strcmp(o,"y");
        if (res == 0){
            system("CLS");
            goto calculate;
            }
        else if (res < 0 || res > 0) {
            printf("\n\n%c\n",o);
            printf("See ya!...\n");
            }
    But it's not working, because it doesn't execute the command after the first if (assuming both chars match).

    Thanks in advance!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    New Member
    Join Date
    Sep 2009
    Location
    Illinois
    Posts
    11

    Re: Comparing two characters

    Because strcmp needs to be 2 strings... Not a string and a character. To compare 2 characters, you can do it directly (use ==).

    So just try this:

    char o;
    printf("Try Again? (n/y): ");
    scanf("%c", &o);
    if (o == 'y') {
    system("CLS");
    goto calculate;
    }
    else {
    printf("\n\n%c\n",o);
    printf("See ya!...\n");
    }

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Comparing two characters

    Thanks! I'll try that!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width