Thread: If clause based on a string

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    If clause based on a string

    Hi, I'm trying (for a bit of fun with a friend of mine) to write a program which will poke fun at his team. The problem is I can't get the program to run with an if condition based on a char. I've tried it with a constant and a variable, and whilst in both cases it operates with an int/float it won't with a string (char array).

    Code:
    /*Calcio.c Program to read football team and issue a message
    */
    
    
    #include<stdio.h>
    
    
    int main()
    
    
    char team[80];
    char squadra[7] = "Inter";
    
    
    {
    printf("Che squadra tifi?:");
    scanf("%s",&team);
    
    
    if ( team == squadra )
    {
    printf("Forza Inter, sei un tifoso fantastico.");
    }
    
    
    else
    {
    printf(" %s SERIE B SERIE B SERIE B!!!!!!!!!!!!!" ,team);
    }
    return 0;
    }
    This is probably because its something I haven't covered on my course yet so I'm probably out-of-my-depth.

    (Sorry that some of it is in Italian, he's a Juve fan and I'm an Interista hence the Italian).

    Whilst its just a bit of fun at this stage I think it'll help for me to know with a view to later stages of my studies.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can not use the comparison operator== with C-strings, you need to use the strcmp() function from string.h.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    You can not use the comparison operator== with C-strings, you need to use the strcmp() function from string.h.

    Jim
    Thanks!

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Oooh, Italian. That language is so beautiful. Also, why do people keep using assignments with char[] arrays instead of char* pointers. In my usage, char[] has caused me nothing but trouble.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Only problem I have now is it will only print the first one (i.e. Forza Inter.......)

    How to I get it to react to a wrong answer and print a different message (i.e. Juve SERIE B.....)

    I tried using two while conditions but it doesn't seem to work.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should rarely use the assignment operator= with char pointers either. You should use the strcpy() function instead.

    Jim

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Code:
    if (!strcmp(team,squadra)) //if they match
             printf("Forza inter...");
    else
             printf("Juve SERIE B.....");
    Quote Originally Posted by jimblumberg View Post
    You should rarely use the assignment operator= with char pointers either. You should use the strcpy() function instead.

    Jim
    You know. I never thought of just putting strcpy(charpointer,"Hi"); You have a point
    Last edited by Rodaxoleaux; 10-13-2011 at 11:57 AM.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rodaxoleaux
    Code:
    if (!strcmp(team,squadra)) //if they match
    I recommend:
    Code:
    if (strcmp(team, squadra) == 0)
    The "==" is a visual indicator that we are comparing for equality, whereas the "!strcmp" lends itself to be read as "if not string compare", which can then be misinterpreted as "if strings are not equal".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Eh. I'm used to just using ! to say (if blank == 0). Whichever floats your (or the OP's) boat.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rodaxoleaux
    I'm used to just using ! to say (if blank == 0). Whichever floats your (or the OP's) boat.
    What do you write when you want to compare "less than" instead of "equal to"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Rodaxoleaux View Post
    Also, why do people keep using assignments with char[] arrays instead of char* pointers. In my usage, char[] has caused me nothing but trouble.
    Maybe an area of C you should work on . They're actually called initializers, not assignments (they must be part of the variable definition). You can't assign to an array with = in C. As for char *, they aren't all that helpful if you want to change the contents. String constants should be just that, constant. Most modern implementations store them in an unwritable segment of memory. On many systems, the following would seg fault:
    Code:
    char *victim_name = "Jon Doe";  // points to a string constant, probably stored somewhere in unwritable memory
    if (victim_identified)
        strcpy(victim_name, "Fred Flintstone");  // oops, can't copy into unwritable memory, seg fault
    whereas the following would work
    Code:
    char victim_name[MAX_NAME] = "Jon Doe";
    if (victim_identified)
        strcpy(victim_name, "Fred Flintstone");
    Sure, there are ways around the char * issues, like pointing victim_name to a char array or malloc'ing some memory for it, but that doesn't always make them the best solution.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Code:
    /*Calcio.c Program to read football team and issue a message
    */
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    int main ()
    {
      char squadra[7] = "Inter";
      char team[80];
      
      do {
      
         printf ("Quale squadra tifi? ");
         gets (team);
      
      } 
    
    
    while (strcmp(team, squadra) == 0)
    
    
    {printf ("Forza Inter, sei un tifoso bellissimo!!!"};
    
    
    or
      
    printf ("Serie B!");
    
    
    
    
    
    
      return 0;
    }
    Sorry for being a dope but this is what I'm left with and I'm totally confused.

    I just want to play a joke where it asks what team you support, I input Inter and it puts out a positive message. Whereas, when he puts in Juve it puts out a negative one.

    Maybe its beyond me at this stage.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should be using an if/else statement not a loop.
    Code:
    if(strcmp(team, squadra) == 0)
    {
       printf ("Forza Inter, sei un tifoso bellissimo!!!"
    }
    else
    {
       printf ("Serie B!");
    }
    You may want to study this link: Control structures

    Jim

  14. #14
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by anduril462 View Post
    Maybe an area of C you should work on . They're actually called initializers, not assignments (they must be part of the variable definition). You can't assign to an array with = in C. As for char *, they aren't all that helpful if you want to change the contents. String constants should be just that, constant. Most modern implementations store them in an unwritable segment of memory. On many systems, the following would seg fault:
    Code:
    char *victim_name = "Jon Doe";  // points to a string constant, probably stored somewhere in unwritable memory
    if (victim_identified)
        strcpy(victim_name, "Fred Flintstone");  // oops, can't copy into unwritable memory, seg fault
    whereas the following would work
    Code:
    char victim_name[MAX_NAME] = "Jon Doe";
    if (victim_identified)
        strcpy(victim_name, "Fred Flintstone");
    Sure, there are ways around the char * issues, like pointing victim_name to a char array or malloc'ing some memory for it, but that doesn't always make them the best solution.
    Wow. I understand C less than I thought.

    Quote Originally Posted by laserlight View Post
    What do you write when you want to compare "less than" instead of "equal to"?
    cute.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    You should be using an if/else statement not a loop.
    Code:
    if(strcmp(team, squadra) == 0)
    {
       printf ("Forza Inter, sei un tifoso bellissimo!!!"
    }
    else
    {
       printf ("Serie B!");
    }
    You may want to study this link: Control structures

    Jim
    Thanks!!!! Worked perfectly and joke performed well!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LINQ, Verbose WHERE Clause
    By alphaoide in forum C# Programming
    Replies: 4
    Last Post: 08-04-2010, 09:04 AM
  2. problem with cin.fail() in if/else clause
    By clearcom in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2010, 10:11 PM
  3. Replies: 12
    Last Post: 04-20-2009, 06:44 AM
  4. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM
  5. if clause not working as I expect.
    By luise.valencia in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 08:35 PM