1

Working through the CS50 pages, I decided to do this locally on my Linux Mint computer (gcc --version gets me: gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4).

At the start, I did not realize that cs50.c and cs50.h were available, so I wrote my own. As I progressed through the lessons, there is a code segment in week4 (http://docs.cs50.net/2016/fall/notes/4/week4.html) that is seemingly failing to work. In particular:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    printf("s: ");
    char *s = get_string();

    printf("t: ");
    char *t = get_string();

    if (s != NULL && t != NULL)
    {
        if (strcmp(s, t) == 0)
        {
            printf("same\n");
        }
        else
        {
            printf("different\n");
        } 
    }
}

$ ./string0 
s: abc 
t: abc 
same

$ ./string0  
s: abc 
t: dfgh 
same


$ ./string0  
s: a 
t: sdfjkl; 
same

In the code above, strcmp seems to return 0 regardless of the values of s and t. Well, initially, I thought this was a problem with my implementation of cs50.c and cs50.h, but after searching I realized I can download them! (http://mirror.cs50.net/library50/c/library50-c-6.zip). I downloaded that file and unzipped it, installed them into my libraries and now I am using the "official" cs50.c and cs50.h. Same problem.

$ ./string0 
s: abc 
t: abc 
same

$ ./string0  
s: abc 
t: dfgh 
same

And: 
$ ./string0  
s: a 
t: sdfjkl; 
same

Hrm, interesting. Let's try to just force the strings (and live dangerously doing it...)

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    printf("s: ");
    char s[64];
    fgets(s, sizeof(s), stdin);

    printf("t: ");
    char t[64];
    fgets(t, sizeof(t), stdin);

    if (s != NULL && t != NULL)
    {
        if (strcmp(s, t) == 0)
        {
            printf("same\n");
        }
        else
        {
            printf("different\n");
        }
    }
}


$ ./string0  
s: abc 
t: abc 
same

$ ./string0 
s: abc 
t: dfgh 
different 

$ ./string0  
s: a 
t: 
sdfjkl;
different

Is this a problem with my computer (I tried it on two machines)? I made sure I was using the cs50 library files by just compiling them on the command line with the program (replacing <> with "" and adding them to the compile), but it give the same result. I thought this might be an issue with getstring, but then all kinds of people would have issues. I find it odd that on the CS50 GetString and my get_string implementation I See the same issue. Any ideas?

UPDATE: after creating the library and installing it and the headers on yet another machine, I did not observe this problem. It seems there was some kind of mistake or error made on the first machine that I translated over to the second machine. I went back to investigate and it seems I may have mixed the headers between the files (e.g. my old one and the cs50 one). This issue was resolved by re-installing.

3
  • To respond to both comments: 'char *s = get_string();' is verbatim from the site: (docs.cs50.net/2016/fall/notes/4/week4.html). Interestingly, if I change to 'char t = get_string();', I get the same result. To respond to @CodeforTheStars, strcmp takes in two char pointers according to the man page.
    – number9
    Feb 13 '17 at 13:30
  • So far, none of these suggestions work. I am not using an IDE, I am doing this in a Linux terminal in bash. @CodeForTheStars: From man: NAME strcmp, strncmp - compare two strings SYNOPSIS #include <string.h> int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n);
    – number9
    Feb 13 '17 at 18:33
  • Apologies about the misunderstanding for terminal type. However, string works. Review here: stackoverflow.com/questions/9834067/…. As said by the answer, you can change char* but not const char*. Both still work in strcmp, however, including string. I implemented my own program using const char*,char*, and string in strcmp and it works. Feb 13 '17 at 18:46
0

Change char *s and char *t to string s and string t. Even though the program you gave worked when I ran it, changing char *s to string s and the same for t will most likely solve your problem.

As for your concerns about strcmp, according to the man page for strcmp:

NAME: strcmp, strncmp - compare two strings
The  strcmp()  function  compares  the two strings s1 and s2.  It returns an integer less than, equal to, or greater than zero if s1 is found,
   respectively, to be less than, to match, or be greater than s2.

It could be your computer or terminal, so I suggest re-starting, updating, and refreshing both your terminal and your computer.

I don't have a clear answer has to why it doesn't work, because it works just fine on my terminal/computer. I suggest changing it to string s = ... and string t = ... and then seeing if that works. Or, you could change it to string's equivalent, char*. Either one should work. Even though strcmp says that const char* would work, I don't advise using it in the case that you want to add on to the program and change the variables in the future.

2
  • 1
    string is an alias for char* (pointer to char), so I don't get your point.
    – Blauelf
    Feb 15 '17 at 14:46
  • I am honestly not sure what was going on either. I replicated this setup on my other linux machine, and it gave me the correct output. I then moved to yet another linix machine, and it gave me the correct output. I am assuming I did something wrong with the first machine. On the original machine, after re-installing the cs50 headers and code into /usr/local{include, lib} it works. I will mark this as solved.
    – number9
    Feb 19 '17 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .