string comparison

This is a discussion on string comparison within the C Programming forums, part of the General Programming Boards category; I am having real trouble getting my character search to work and i think the problem is located in my ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    Question string comparison

    I am having real trouble getting my character search to work and i think the problem is located in my string comparison. if any one can help it would be hugely appreciated.

    My program revolves around a structure array in which i have already initialised all the values, and am not performing various functions on those values, in this case, searching (by ID, name, or date).
    My structure declaration for the program is as follows:

    Code:
    struct student {
    	int ID;
    	char Name[50];
    	char Date[10];
    	float Min_temp;
    	float Max_temp;
            int Valid;
    } record[20];
    I have written some code that asks the user what they would like to search by, ID, name or number, followed by what string/value they r looking for. depending on choice the code enters a different case of a switch statement, and runs through a for loop to check the appropriate field against every element in the structure array. a snippet of this code is shown below:

    Code:
    		case 1 :	printf("Enter value (1-20): \n");
    				scanf("%d",&y);
    				for(i=1;i<=20;i++) {
    					if(record[i].ID == y) {
    						printf("%-4d %-20s %-10s %.3f %.3f %d\n", record[i].ID, record[i].Name, record[i].Date, record[i].Min_temp, record[i].Max_temp, record[i].Valid);
    					}
    				}
    				break;
    		case 2 :	printf("Enter search string (no spaces): \n");
    				scanf("%s",&z);
    				for(i=1;i<=20;i++) {
    					if(record[i].Name == 'z') {
    now i know theres no problem with the code around this thats not shown, as if when i run the program i choose to search by ID it works perfectly. It just seems to be string comparison thats causing the problem :/

    thanks again for any help

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    Quote Originally Posted by sj999 View Post
    case 2 : printf("Enter search string (no spaces): \n");
    scanf("&#37;s",&z);
    for(i=1;i<=20;i++) {
    if(record[i].Name == 'z') {
    1.Remove the ampersand.
    2.Use strcmp to check for equality of your strings(== does not work for strings)
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Arrays are automatically passed as pointers without the &, so using the &, it becomes a pointer to pointer (type**).
    Arrays are treated as pointers if you try to compare them, so all you would do is compare the address of two pointers which is never guaranteed to be equal. Thus you must compare using the special function strcmp.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    10
    thanks alot for the explanation guys rly helped alot!!

    ive applied what u said and for a name search it is returning the correct result, however after the result is returned, then i get a segmentation fault, (my most hated fault :P), and for date, which is also a string i also get segmentation fault but no values returned correctly as with name.

    the code for my Name and Date switch cases are below:

    Code:
    		case 2 :	printf("Enter search string (no spaces): \n");
    				scanf("%s",z);
    				clearscreen();
    				printf("Search Result : \n\n");
    				printf("ID   Name                 Date       Min   Max     Valid \n\n");
    				for(i=1;i<=20;i++) {
    					if(strcmp(record[i].Name, z) == 0) {
    						printf("%-4d %-20s %-10s %.3f %.3f   %d\n", record[i].ID, record[i].Name, record[i].Date, record[i].Min_temp, record[i].Max_temp, record[i].Valid);
    					}
    				}
    				
    				break;
    		case 3 :	printf("Enter Date (in the form xx/xx/xx): \n");
    				scanf("%s",z);
    				clearscreen();
    				printf("Search Result : \n\n");
    				printf("ID   Name                 Date       Min   Max     Valid \n\n");
    				for(i=1;i<=20;i++) {
    					if(strcmp(record[i].Date, z) == 0) {
    						printf("%-4d %-20s %-10s %.3f %.3f   %d\n", record[i].ID, record[i].Name, record[i].Date, record[i].Min_temp, record[i].Max_temp, record[i].Valid);
    					}
    				}
    				break;
    Maybe it will help to know ive got my z variable declared like this:
    Code:
    char *z;
    and that when i compile i get the warning:

    warning: 'z' might be used unitialised in this function

    any ideas on fixing this would be great, and thanks for the help already

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    You seem to lack understand of allocation and pointers:
    http://cpwiki.sf.net/A_pointer_on_pointers
    Also beware of the dangers of scanf. See:
    http://cpwiki.sf.net/Buffer_overrun
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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