problem with strcmp

This is a discussion on problem with strcmp within the C Programming forums, part of the General Programming Boards category; hi there, the code below is supposed to check the response typed from keyboard and perform an action based on ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Question problem with strcmp

    hi there,
    the code below is supposed to check the response typed from keyboard and perform an action based on that. but the if statements never evaluates to zero even when the correct response is entered. what am i doing wrong?
    thanks in advance.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream.h>

    int main()
    {
    char temp, ch;
    int value;

    printf("please confirm(y/n):");
    scanf("%c", &temp);
    strcpy(&ch,strlwr(&temp));
    printf("%c\n", ch);

    value = strcmp("y", &ch);
    printf("comp result= %d\n ", value);

    if ((strcmp("y", &ch)) == 0)

    {
    printf("confirmed\n");
    exit(99);
    }
    else
    if ((strcmp("n", &ch)) == 0)
    {
    printf("declined\n");
    exit(99);
    }
    else
    printf("invalid choice (y/n)\n");

    return(0);
    }

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    you are using a char type, instead of a char * type with memory allocated to the pointer... strcmp expects the char * type... if you are only requesting a single character [as indicated by your scanf usage], you don't need to use strlwr and strcmp to alter and compare it...
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21