Thread: Phonebook search without cs50.h library ?

  1. #1
    Registered User
    Join Date
    Dec 2023
    Posts
    13

    Phonebook search without cs50.h library ?

    Hi,

    Iam using different tutorials and sadly the lecture from Harvard on YT always uses a custom made "cs50.h" library which in the end just complicate things because I have to figure then things out myself, or is everyone using it ? I guess not so I obviously dont want to rely on it.

    Well here is a simple phonebook search, at least Iam not getting any errors anymore but the code also doesnt print out anything, any help would be appreciated.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct
    {
        char name[20];
        char phone[20];
    }
    Phonebook;
    
    int main()
    {
    
    Phonebook people[2];
    
    Phonebook kevin;
    strcpy(kevin.name, "Kevin Schmidt");
    strcpy(kevin.phone, "827123");
    
    Phonebook maik;
    strcpy(maik.name, "Maik Mueller");
    strcpy(maik.phone, "67322188");
    
    char find[20];
    printf("Name to find ?: ");
    scanf("%s", find);
    
    for(int i = 0; i < 2; i++)
    {
        if(strcmp(people[i].name, find) == 0)
        printf("Found %s\n", people[i].phone);
    }
    return 0;
    }
    But maybe the whole thing is wrong I have no clue, thanks in advance as always :).
    Last edited by crusher152; 01-23-2024 at 11:28 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > strcpy(kevin.name, "Kevin Schmidt");
    You want to use people[0].name etc to initialise both elements of your array.

    > or is everyone using it ?
    Nobody outside of whatever course that's for even knows or cares what's in it.

    If you can manage to do what you want with only the standard libraries, you're in good shape.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    cs50.c (and its header file cs50.h) consists of 6 functions for entering a character, string, int, long, float, or double, with a prompt and some error (e.g. range) checking. It is not important.

    Your code has another problem besides not putting anything in the people array.
    Using the %s format with scanf will stop reading input at the first space, so if the input was Kevin Schmidt it would only read Kevin. The format %[^\n] will read up to the end of the input line (it basically says to keep reading characters until it encounters a newline character).
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Dec 2023
    Posts
    13
    Thanks for your time and help but I cant figure it out, it just wont work. If I initiliaze with "people[0]" etc. which I also tried before then the variables "Maik, Kevin" are unused.

    I also dont understand what you mean by "%[^\n]", that will give an error. And if I put "%s[^\n]" it will do the same as before (at least I think so).

    I guess I cant see the forest full of trees, maybe I should move on to something else for a while :/.
    Last edited by crusher152; 01-24-2024 at 02:50 AM.

  5. #5
    Registered User
    Join Date
    Dec 2023
    Posts
    7
    hi, i am also newbie in C but, you can just assign people[0]=kevin and 1 to maik
    and i just used search to find scanf(“%[^\n]s”,&str)

  6. #6
    Registered User
    Join Date
    Dec 2023
    Posts
    13
    Quote Originally Posted by EmVee View Post
    hi, i am also newbie in C but, you can just assign people[0]=kevin and 1 to maik
    and i just used search to find scanf(“%[^\n]s”,&str)
    But you then didnt specify ".name" and ".phone", doesnt work for me. Cant you just write down the snippet of code and show it ? That would be so much easier and quicker because somehow I dont get it :/. Ive tried already yesterday for 2 hours and with no result, its already very annoying.

  7. #7
    Registered User
    Join Date
    Dec 2023
    Posts
    7
    Quote Originally Posted by crusher152 View Post
    But you then didnt specify ".name" and ".phone", doesnt work for me. Cant you just write down the snippet of code and show it ? That would be so much easier and quicker because somehow I dont get it :/. Ive tried already yesterday for 2 hours and with no result, its already very annoying.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct
    {
        char name[20];
        char phone[20];
    }
    Phonebook;
    
    int main()
    {
    
    Phonebook people[2];
    
    Phonebook kevin;
    strcpy(kevin.name, "Kevin Schmidt");
    strcpy(kevin.phone, "827123");
    
    Phonebook maik;
    strcpy(maik.name, "Maik Mueller");
    strcpy(maik.phone, "67322188");
    
    people[0]=kevin;
    people[1]=maik;
    
    char find[20];
    printf("Name to find ?: ");
    scanf("%[^\n]s", find);
    
    for(int i = 0; i < 2; i++)
    {
        if(strcmp(people[i].name, find) == 0)
        printf("Found %s\n", people[i].phone);
    }
    return 0;
    }
    it works

  8. #8
    Registered User
    Join Date
    Dec 2023
    Posts
    13
    I thought I have to replace my "strcpy ..." code with the "people..." code but still it wont print out anything in the end. If it works for you then maybe my IDE or compiler is faulty ? I just get a blank line in the end even tho the names exist.

    My IDE also puts the "%" from the "scanf" function in red, no clue why.

    Guess I will reinstall everything and see if it fixes it. Thanks for the help.
    Last edited by crusher152; 01-24-2024 at 09:21 AM.

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    @EmVee, the s after %[^\n] is incorrect. Also, you don't need the kevin and maik objects. You can assign directly to people.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct
    {
        char name[20];
        char phone[20];
    }
    Phonebook;
    
    int main()
    {
        Phonebook people[2];
    
        strcpy(people[0].name, "Kevin Schmidt");
        strcpy(people[0].phone, "827123");
    
        strcpy(people[1].name, "Maik Mueller");
        strcpy(people[1].phone, "67322188");
    
        char find[20];
        printf("Name to find: ");
        scanf("%[^\n]", find);
    
        for (int i = 0; i < 2; i++)
        {
            if (strcmp(people[i].name, find) == 0)
                printf("Found %s\n", people[i].phone);
        }
    
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Dec 2023
    Posts
    7
    i know i just repaired his code, it is not my code, he wanted to send it to him so i tested his code with smallest amount of changes to make it work

    Quote Originally Posted by john.c View Post
    @EmVee, the s after %[^\n] is incorrect. Also, you don't need the kevin and maik objects. You can assign directly to people.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct
    {
        char name[20];
        char phone[20];
    }
    Phonebook;
    
    int main()
    {
        Phonebook people[2];
    
        strcpy(people[0].name, "Kevin Schmidt");
        strcpy(people[0].phone, "827123");
    
        strcpy(people[1].name, "Maik Mueller");
        strcpy(people[1].phone, "67322188");
    
        char find[20];
        printf("Name to find: ");
        scanf("%[^\n]", find);
    
        for (int i = 0; i < 2; i++)
        {
            if (strcmp(people[i].name, find) == 0)
                printf("Found %s\n", people[i].phone);
        }
    
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Dec 2023
    Posts
    7
    yes i tested it before i sent it to you
    Quote Originally Posted by crusher152 View Post
    Thanks for your time and help but I cant figure it out, it just wont work. If I initiliaze with "people[0]" etc. which I also tried before then the variables "Maik, Kevin" are unused.

    I also dont understand what you mean by "%[^\n]", that will give an error. And if I put "%s[^\n]" it will do the same as before (at least I think so).

    I guess I cant see the forest full of trees, maybe I should move on to something else for a while :/.

  12. #12
    Registered User
    Join Date
    Dec 2023
    Posts
    13
    Ive reinstalled everything and now it works, I dont know what the issue was but again thanks alot !.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CS50 Caesar - Checking a string for digits
    By sclem in forum C Programming
    Replies: 3
    Last Post: 05-02-2020, 01:03 AM
  2. Harvard CS50 Online Help -- Mario Pyramid
    By mcavanaugh8 in forum C Programming
    Replies: 14
    Last Post: 02-01-2016, 02:40 PM
  3. Phonebook in C
    By alkurik in forum C Programming
    Replies: 4
    Last Post: 10-16-2014, 02:59 PM
  4. Code for CS50 functions?
    By VEN0M in forum C Programming
    Replies: 19
    Last Post: 07-25-2012, 11:53 AM
  5. Plz help me to include cs50.h and cs50.c into Code::Block
    By Huncowboy in forum C Programming
    Replies: 4
    Last Post: 03-09-2010, 10:05 AM

Tags for this Thread