3

I just finally got the cs50 library working after a lot of trial on my Windows Vscode. Now, the problem is that the get_string function would not work as used below:

int main(void)
{
    string s = get_string("Enter string: ");

    // ensure string was read
    if (s == NULL)
    {
        return 1;
    }

    string next = get_string("You just entered %s. Enter a new string: ", s);

    if (next == NULL)
    {
        return 1;
    }

    printf("Your last string was %s\n", s);
}

When I write

string name = get_string("Give me a name:");

I get the error

In file included from hello.c:1:0:
cs50.c:78:8: note: expected ‘char **’ but argument is of type ‘char *’
 string get_string(va_list *args, const string format, ...)
        ^~~~~~~~~~
hello.c:10:16: error: too few arguments to function ‘get_string’
  string name = get_string("Give me a name:");
                ^~~~~~~~~~
In file included from hello.c:1:0:
cs50.c:78:8: note: declared here
 string get_string(va_list *args, const string format, ...)

Here is my code. I am basically testing the get_string function not necessary needed in the function.

#include "cs50.c"
#include "cs50.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{

 char str[20] = "#";
 string name = get_string("Give me a name:");
 printf("What height of pyramid \n");
 int user;
 if(scanf("%d", &user))
 {
     for (int i =0; i< 8; i++)
  {
      if(user <= 0 || user > 8 )
      {
          printf("Height: %d \n", user);
          printf("Provide value between 1 and 8 \n");
          scanf("%d", &user);

      }


  }
    printf("\n");
    int i;


    for (i = 1; i <= user; i++) { 
        for(int k = user; k > i; k--){
            putchar(' ');
        }
        int j;
        for (j = 0; j < i; j++) {

            putchar('#');

        }
        putchar('\n');

    }
  return EXIT_FAILURE;
 }

}

I expect to write

string s = get_string("Enter string: ");

and get a prompt in the terminal when running the code.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Darotudeen
  • 1,914
  • 4
  • 21
  • 36

7 Answers7

2

In fact by trial and error did not bring you quite to the correct solution. The problem is rather simple. The get_string you're supposed to use is a macro from cs50.h. The cs50.c removes this macro definition and defines another function by name get_string (yes, it is awful). The end result is that you cannot #include <cs50.c> to make the code work even in a single-file application.

What you need to do is to only

#include <cs50.h>

and add cs50.c as another translation unit in your project, i.e. if your main program is prog.c you will add cs50.c as a file in the same folder.

2

I was able to get through this by including cs50.h and cs50.c from libcs50-8.0.3 which is the v8.0.3 that conform with what I want.

Everything is fine now.

Darotudeen
  • 1,914
  • 4
  • 21
  • 36
  • Here is the link to the zip file for v8.0.3: https://github.com/cs50/libcs50/archive/refs/tags/v8.0.3.zip and the general link for v8.0.3: https://github.com/cs50/libcs50/releases/tag/v8.0.3 – Himaloy Mondal Jul 08 '21 at 05:45
0

If you added cs50 library and even you get this error, I can give an advice like this:

For linux terminal;

clang -o file_name file_name.c -lcs50

source: Harvard College CS50

Seyit
  • 1
0

Copy and paste this code in your source code file:

My Own CS50 Get_String Function (Shortened Implementation)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#define SIZE_MAX 50

char *get_string(const char *format, ...)
{
    if (format != NULL)
    {
        va_list ap;

        va_start(ap, format);

        vprintf(format, ap);

        va_end(ap);
    }

    char *buffer = NULL;
    size_t capacity = 0;
    size_t size = 0;
    unsigned read = 0;
    int c;

    while ((c = fgetc(stdin)) != '\r' && c != '\n' && c != EOF)
    { 
        read++;

        if (read > capacity && read < SIZE_MAX )
            capacity++;
            
        else
        {
            free(buffer);
            return NULL;
        }

        char *temp = realloc(buffer, capacity);
        if (temp == NULL)
        {
            free(buffer);   
            return NULL;
        }
        buffer = temp;
        buffer[size++] = c;
    }
    
    if (size == 0 || c == '\n')
        return NULL;

    if (size == SIZE_MAX)
    {
        free(buffer);
        return NULL;
    }

    char *s = realloc(buffer, size + sizeof(char));
    if (s == NULL)
    {
        free(buffer);
        return NULL;
    }
    s[size] = '\0';

    return s;
}
mada
  • 1,646
  • 1
  • 15
0

This works for me:

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

int main(void)
{
    char c1 = ' ', *c2, **c3;
    c2 = &c1;
    c3 = &c2;
    string s = get_string(c3, "Some text: ");
    printf("%s\n", s);
}
Eliud L
  • 1
  • 1
0

I noticed that get_string checks for NULL in the cs50.c file so setting the first argument to NULL fixed it for me.

string name = get_string(NULL, "Give me a name:");
user16217248
  • 3,119
  • 19
  • 19
  • 37
-1

I got a workaround to this problem! Calling the string 's' variable within the 'get_string' function fixes this problem.

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

int main(void)
{
    printf("Enter a string: ");
    string s = get_string(" ", s);
    printf("%s", s);

Note: Am not aware if this approach results into bugs down the line. It works fine for me.

d0ntr13
  • 1
  • 1
  • Hi, welcome to SO. I am not sure you understood the question. If you read the other answers then you can see that there is a problem with duplicate names and a mix of a macro and a function with the same name. Further more your code uses the uninitialized variables as an argument to a function which is a bad practice. This answer unfortunately does not add information helpful for solving the problem. Consider deleting it – Sebastian Cabot Mar 05 '22 at 22:22