works in the book but not in my compiler

This is a discussion on works in the book but not in my compiler within the C Programming forums, part of the General Programming Boards category; I am learning to program and this program is suppost to work in the book but it will not find ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28

    works in the book but not in my compiler

    I am learning to program and this program is suppost to work in the book but it will not find anything. I'm stumped
    Thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
        char tracks[][80] = {
         "I left my Heart at Harvard Medical School",
         "Newark, Newark, You suck balls",
         "Dancing with a dork",
         "From Here to maternity",
         "The Girl from Iwo Jima",
         };
    
    
         
    void find_track(char search_for[])
         {
              int i;
              for(i = 0; i<5; i++){
                    if (strstr(tracks[i], search_for))
                    printf("Track %i: '%s'\n",i,tracks[i]);
                    
                    else
                    printf("Nothing found\n");
                    }
         }
    
    
    int main()
    {
        char search_for[80];
        
    
    
        printf("Search for: ");
        fgets(search_for, 80, stdin);
        printf("search_for = '%s'\n", search_for); 
        find_track(search_for);
        
        system("pause");  
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    How does it not work? For example, what are the error messages? What compiler (including version) are you using?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,375
    I had to change system(pause) to getchar()

    Also, you need to remove the '\n' from the input string after fgets

    I changed the indentation style to Allman, but your way is fine.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char tracks[][80] =
    {
        "I left my Heart at Harvard Medical School",
        "Newark, Newark, You suck balls",
        "Dancing with a dork",
        "From Here to maternity",
        "The Girl from Iwo Jima",
    };
    
    void find_track(char search_for[])
    {
        int i;
        for(i = 0; i<5; i++)
        {
            if (strstr(tracks[i], search_for))
            {
                printf("Track %i: '%s'\n",i,tracks[i]);
                break;
            }
            else if (4 == i)  // So the "nothing found" is only printed when everything is searched.
            {
                printf("Nothing found\n");
            }
        }
    }
    
    
    int main(void)
    {
        char search_for[80];
        char *ch;
    
        printf("Search for: ");
        fflush(stdout); // Because string doesn't end in \n
    
        fgets(search_for, 80, stdin);
        if ((ch = strchr(search_for, '\n')) != NULL)
        {
            *ch = '\0';
        }
    
        printf("search_for = '%s'\n", search_for);
        find_track(search_for);
    
        //system("pause");
        puts("Press enter to continue...");
        getchar();
    
        return 0;
    }
    [edit]
    laserlight has a really good way of removing the \n from the end of a fgets string - I just can't remember it at the moment...
    [/edit]
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Click_here View Post
    Code:
            else if (4 == i)  // So the "nothing found" is only printed when everything is searched.
            {
                printf("Nothing found\n");
            }
    Yuck, double yuck in fact!

    Just do "if (i == 5)" after the loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,375
    Quote Originally Posted by iMalc View Post
    Yuck, double yuck in fact!

    Just do "if (i == 5)" after the loop.
    There is nothing wrong with a bit of Yoda script!
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clang++ works but g++ doesn't -- Maybe a compiler bug?
    By RichSelian in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2011, 08:04 PM
  2. Replies: 9
    Last Post: 06-10-2010, 08:49 PM
  3. Replies: 4
    Last Post: 04-29-2010, 08:02 PM
  4. Compiler Issues (Works on older not newer)
    By ss3x in forum C++ Programming
    Replies: 5
    Last Post: 09-04-2002, 09:37 PM
  5. compiler book?
    By sanchezero in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 09:22 PM

Tags for this Thread


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