Thread: comparing strings

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    comparing strings

    In this function, I am reading in a 'license plate' and comparing to all 'licence plates' in my array. If there is a match, then null that part of the array (the car is leaving). It seemed to work fine but I discovered that if the plate number began with 'd' then the program nulls all plate numbers beginning with d. My for loop looks okay but I am not sure how to fix it. Suggestions???


    void Delcar (char cars [MAX][PLATE])//function to delete a car
    {

    int i,x;
    int counter;
    counter=CINIT;
    char tarray[PLATE]={'\0'};
    char ch;

    do
    {
    scanf("%s",tarray);

    {for (x=LINIT;x<MAX;x++)

    for (i=LINIT;i<=PLATE;i++)

    {
    if (cars[x][i]==tarray[i])
    {
    cars[x][i]='\0';
    printf("Car left space %d\n",x+1);

    counter++;
    }

    break;

    }
    }
    }
    while (counter<=CINIT);


    Options(); //function to call main menu
    scanf("%c",&ch);

    }

  2. #2
    Unregistered
    Guest
    You don't compare strings with ==

    Well, OK, so you can in C++, but not C.

    You must use either strcmp (string compare) or stricmp (insensitive string compare)


    instead of "if (cars[x][i]==tarray[i])"

    use "if(strcmp(cars[x], tarray)==0)" (if it equals 0, they're the same)

    or, to make it fancy do "if(!strcmp(car[x], tarray))"

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    Works fine now.

    Thanks!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I personally loathe un-natural return values, and even though strcmp is really a sort - friendly function, it is commonly used for "exact matching". A wrapper function does the trick:

    Code:
    
    int Identical( char s[], char t[] ) {
    
    if( strcmp( s, t ) == 0 )
    return 1;
    
    return 0;
    } 
    
    
    Of course the preferred may be:
    
    bool Identical( char *s, char *t ) {
    
    return strcmp( s, t ) == 0 ? true : false;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM