0

This is the code written:

#include <stdio.h>
#include <cs50.h>
int main(void)
{
string name = get_string ("what is your name\n");
scanf("%s", &name);
printf("%s",name);
}

On typing help50 make hello command, I get this suggestion:

hello.c:6:17: error: format specifies type 'char *' but the argument has type 'string *' (aka 'char **') [-Werror,-Wformat]
scanf("%s", &name);
       ~~   ^~~~~

Be sure to use the correct format code (e.g., %i for integers, %f for floating-point values, %s for strings, etc.) in your format string on line 6 of hello.c. ~/pset1/hello/ $

Do I need to remove string type and instead use char type. If so, why?

#include <stdio.h>
#include <cs50.h>
int main (void)
{
int number;
scanf ("enter a number %i", &number);
printf ("the number is %i", number);
}

Here is the result as I enter 44

~/pset1/ $ ./Mario
44
the number is 0~/pset1/ $ 
1
  • Why have you edited your answer to include the code for your other question? While it related here, there is no context given for this new code block that is not related to the code/question that comes before it. Combining questions like this can lead to less than helpful answers due to confusion. I'd recommended leaving a comment below Cliff's answer with a link to the other question and ask him to take a look, if you would like him to. Aug 6 at 12:33
1
+50

The cs50 function get_string() takes care of writing to name. It both prints out the message "What is your name" and puts the user input into name. The scanf line is unnecessary and can be removed.

These cs50 var types and functions were created to streamline getting data while you're learning to code, so you don't have to figure out scanf and other functions right now.

However, the error is because of the type 'string' being used on the next line! scanf doesn't like any vars of type string. The code would need to actually use 'char *' instead, but you'll learn about that later. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • In the hints while solving the Hello lab problem, it is mentioned: Recall that you can use get_string as follows, storing its return value in a variable called name of type string. cs50.harvard.edu/x/2021/labs/1/hello Jul 25 at 5:53
  • exactly! The call to get string is doing exactly that. It's the next line that's causing the error. Just delete it! I just reread my answer and I did a poor job of explaining the error was with scanf and not getstring. My bad. I've edited it to make more sense.
    – Cliff B
    Jul 25 at 9:57
  • Thanks! Removing scanf led to the solution. Jul 26 at 8:00

You must log in to answer this question.

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