Problem with strcmp, beginner>

This is a discussion on Problem with strcmp, beginner> within the C Programming forums, part of the General Programming Boards category; I am having trouble with this code, when I run it an error message comes up saying that the prog ...

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

    Unhappy Problem with strcmp, beginner>

    I am having trouble with this code, when I run it an error message comes up saying that the prog has performed an illegal option. I cannot see what the problem is, can you help please.



    #include <stdio.h>

    main()
    {
    char choice1 = 'Z';
    int j = 0;
    char Y[]="Y";
    char N[]="N";
    printf("\nPlease conform that these details are correct (Y/N)");
    choice1 = getchar();
    getchar( );

    if(strcmp(Y, choice1))
    {
    printf("1 student record saved to file");
    j=j+1;
    }
    else
    {
    if((strcmp(choice1, N))==1)
    {
    printf("in if 1");
    }
    else
    {
    printf("\n Not a valid option");
    }
    }
    return 0;
    }

  2. #2
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    strcmp(string1,string2) returns a 0 is both strings are identitcal, -1 is String1 is smaller (alphabetically before) than String2 and 1 is String2 is smaller (alphabetically) than String1. Don't quote me on those last 2 options, don't have my book to confirm that, but I do know if the strings are identical strcmp(string1,string2) will return 0.

    With that in mind, your if's are wrong. They should be:
    Code:
    if (strcmp(Y,choice1) == 0)
    {
       printf("1 student record saved to file");
       j++;
    }
    else
    {
       if (strcmp(N,choice1) == 0) /*IF USER ENTERED NO*/
          printf("In if 1");
       else
          printf("Not a valid option");
    }
    Additionally, since all you really want to know is when choice1 is Y, you don't necessarily have to check to see if choice1 is N, as any other option would be something you are not interested in (IMHO).
    DrakkenKorin

    Get off my Intarweb!!!!

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Still trouble

    I have used the corrected code but it still gives me the same error?

  4. #4
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    Heyho,

    Code:
    if (strcmp(Y,choice1) == 0)
    If I'm not totaly confused after this ..........in* day I had strcmp takes strings as arguments and your choice1 is just a char, so it would have to be &choice1. Strange if it's compiling like this...

    Anyway, since you are just using one char ('Y' or 'N') for the users decision you don't need strcmp at all, because there is just one single char After you've read the input a simple:

    Code:
    if (choice == 'Y')
    {
      // ...
    };
    and the same for the 'N' choice should do it...

    greetings
    cody
    #include "reallife.h"

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Thanks

    It works like a dream, cheers

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I know this is kinda off the point, but you shouldn't rely on strcmp to return 1 or negative 1. It will return a negative number if str1 is before str2, and a positive number if str1 is after str2. What those numbers are, you don't know.
    Callou collei we'll code the way
    Of prime numbers and pings!

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