'I am having issues with the get_string function
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.
Solution 1:[1]
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.
Solution 2:[2]
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
Solution 3:[3]
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;
}
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Antti Haapala -- Слава Україні |
| Solution 2 | Seyit |
| Solution 3 | mada |
| Solution 4 | d0ntr13 |
