0
Here's my Code:
#include<stdio.h>
#include<cs50.h>

int main (void)
    {
       string a= get_string("Enter a name\n");
       string b= get_string("Enter Class\n");

       //Prints out the answer

       string c= get_string("Search: \n");
       if (c==a)
       printf(" Name: %s,    Class: %s", a,b);
       else
       printf("void");
    }

When I enter everything, (even though the code is right, The terminal prints out void, but every time, c==a. Here's my termial:

~/ $ ./database2
Enter a name
David J Malan
Enter Class
X
Search: 
David J Malan
void~/ $ 
1

You can't compare strings using == in most languages, as that will compare their location (or possibly their hash, I think, depending on the language). This is all if I remember the specifics correctly right now. Either way, c will never "equal" a. You will need to use something like strcmp from string.h to achieve your desired behavior.

4
  • Thanks, So, can you just show me how to do it? Just 2 lines of code... Jan 29 at 13:44
  • 1
    I could, but you should look it up in the course's documentation, especially since I gave you everything needed for that. You will often find yourself in documentation as a programmer and this is good experience/practice. I also could not provide a better example or explanation as succinctly. :) manual.cs50.io Jan 29 at 13:52
  • You need to learn how to find needed functions in the libraries and the docs and how to use them. "Give a man a fish and you feed him for a day. Teach him to fish and you feed him for a lifetime." Good response, @Robert S. Pratt
    – Cliff B
    Jan 29 at 23:47
  • Okay, Thanks for the advise, @RobertS.Pratt S. Pratt Jan 30 at 13:11

You must log in to answer this question.

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