-1

I am currently taking the "CS50 Introduction To Computer Science" course, and I'm still quite new to programming in C. I'm working on a simple agreement project that involves using if statements. However, as a beginner, I'm facing some difficulties with my code.

If there's anyone experienced with C programming who could lend a helping hand, I would be incredibly grateful for your assistance. Below is the code that I've attempted so far:

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

int main(void)
{
    string username = get_string("What would you like your username to be? ");
    string agreement = get_string("Do you agree to the following terms & conditions? ");
    if (!strcmp(agreement, "yes" || agreement, "y" || agreement, "Yes" || agreement, "Y"))
    {
        printf("You can now continue using the service, %s! \n", username);
    }
    else if (!strcmp(agreement, "no" || agreement, "n" || agreement, "Yes" || agreement, "Y"))
    {
        printf("You need to agree to the terms & conditions to use the service\n");
    }
    else
    {
        printf("You need to select a option\n");
    }
}

And here is the error that is thrown when I try to compile the code:

Too many arguments for function call. Expected 2, Have 5

I tried to search similar questions on google and found this result: How to check if variable equal to multiple values But, I couldn't solve my problem. Here is the code that I tried (which did not work):

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

int main(void)
{
    string username = get_string("What would you like your username to be? ");
    string agreement = get_string("Do you agree to the following terms & conditions? ");
    if (str[agreement] == 'yes' || str[agreement] == 'y' || str[agreement] == 'Yes' || str[agreement] == 'Y')
    {
        printf("You can now continue using the service %s! \n", usename);
    }
    else if (str[agreement] == 'no' || str[agreement] == 'n' || str[agreement] == 'No' || str[agreement] == 'N')
    {
        printf("You need to agree to the terms & conditions to use the service\n");
    }
    else
    {
        printf("You need to select a option\n");
    }
}

But I received this error:

Use of undeclared identifier "str"

I'm excided to learn and appreciate any guidance or tips you can provide to help me make progress on this project. Thank you so much for your time and support!

  • 2
    The only way to compare strings in C is to a function such as `strcmp`. To compare multiple strings, you will need to apply the function on each one of the strings. There is no way around it. Well, you could implement a fancy algorithm with prefix trees and such, but you probably don't want to. – Eugene Sh. Jul 20 '23 at 17:03

2 Answers2

2

strcmp takes two aruments. As written in you first example, you are passing a bunch of non-sense into it.

Instead, make multiple strcmp checks, and "or" their results:

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

int main(void)
{
    string username = get_string("What would you like your username to be? ");
    string agreement = get_string("Do you agree to the following terms & conditions? ");
    if (!strcmp(agreement, "yes") || !strcmp(agreement, "y") || !strcmp(agreement, "Yes") || !strcmp(agreement, "Y"))
    {
        printf("You can now continue using the service, %s! \n", username);
    }
    else if (!strcmp(agreement, "no") || !strcmp(agreement, "n") || !strcmp(agreement, "Yes") || !strcmp(agreement, "Y"))
    {
        printf("You need to agree to the terms & conditions to use the service\n");
    }
    else
    {
        printf("You need to select a option\n");
    }
}
ee-4-me
  • 267
  • 1
  • 8
2

So, the error is because of this line:

if (!strcmp(agreement, "yes" || agreement, "y" || agreement, "Yes" || agreement, "Y"))

That call to strcmp is being parsed as

strcmp(agreement, ("yes" || agreement), ("y" || agreement), ("Yes || agreement), "Y")

hence the "too many arguments" error. That should be written as

if (!strcmp(agreement, "yes") ||  // you're comparing the results
    !strcmp(agreement, "y")   ||  // of four separate calls to
    !strcmp(agreement, "Yes") ||  // strcmp
    !strcmp(agreement, "Y") )
{
  ...
}

Since it looks like you only care about the first letter of agreement, you can simplify that a bit:

#include <ctype.h>
...
if ( tolower( agreement[0] ) == 'y' ) // convert the first letter of
{                                     // agreement to lower case and
  ...                                 // compare against 'y'
}
else if ( tolower( agreement[0] ) == 'n' )
{
  ...
}
else
{
  ...
}

This will work if agreement contains any of the strings "Yes", "yes", "Y", "y", "YO, ADRIAN!", "yup", etc.

Be aware, CS50 grossly misrepresents string operations in C. C does not have an actual string data type; that's unique to the CS50 library. It's an alias for the type char * (pointer to char). The get_string function allocates memory to store the string behind the scenes and returns a pointer to that memory, which is what actually gets stored in agreement:

           +---+          +---+
agreement: |   | -------> |'Y'|  
           +---+          +---+
                          |'e'|
                          +---+
                          |'s'|
                          +---+
                          | 0 |
                          +---+ 

In C, a string is a sequence of character values including a zero-valued terminator. The string "yes" is represented as the sequence {'y', 'e', 's', 0}.

Strings are stored in arrays of character type. You can initialize an array with a string in a declaration:

 char greeting[SIZE] = "hello";

or

 char greeting[SIZE] = {'h', 'e', 'l',  'l', 'o', 0};
 

but you cannot assign arrays to each other using the = operator; this won't work:

 greeting = "goodbye";

You either have to use a library function like strcpy:

strcpy( greeting, "goodbye" );

or assign each element individually:

greeting[0] = 'g';
greeting[1] = 'o';
greeting[2] = 'o';
...
greeting[6] = 'e';
greeting[7] = 0;

Similarly, you can't use == to compare array contents, you must use a library function like strcmp or memcmp, or compare elements individually.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Thanks for the detailed explenation but, If I'm not mistaken CS50 is only a 101 / Introduction to CS. So it shouldn't be taken as a C course right? – KORABIMERI Jul 20 '23 at 19:45