The problem is with your scanf and the fact that you are telling it it will read an int but you give it a char. ASCII values may be represented by integers, but scanf can distinguish them. It checks for each digit to see if it's in the range 48-57 which is '0'-'9' in ASCII. So when it reads a digit that is not in that range ('q' for example), it puts it back ...
Per the manual page of scanf()
A sequence of white-space characters (space, tab, newline, etc.; see
isspace(3)). This directive matches any amount of white space,
including none, in the input.
when something like
is executed, scanf() expects to read an int followed by any amount of white-spaces (e.g., space, tab, newline, ...
First in the struct definition, you are the declaring a pointer to a char (char*), so if you don't want to use GetString(), when you try to store something that is not a pointer on any of this elements most probably you will get a segmentation fault.
To fix this an option is to declare the char* like this:
As you will discover later on during the course, get_int() is part of the cs50 library "cs50.h" which is not used in the real world, except in the course. To ease the apprenticeship, David and his team created simplified function to let students understand more rapidly the dev logic.
You have all the details of this library here : https://cs50....
When in doubt or you are sure you have done something correct but still receive errors, you should always check the the CS50 Man Pages to see what it says.
In this case, it says that scanf needs a format string and zero or more arguments that tell it where to store input (read pointers). So, scanf can do nothing with the numbers variable that you have ...
I think you got it. scanf needs a memory address so it knows where to write to.
Whenever you add & you get the memory address of what follows. But in case of strings, what we pass as a string already is a pointer (because the variable is a pointer to the first character of the actual string, with the characters in your example stored in malloced memory ...
It can be done, and in fact, will be done as part of some of the last psets. But since this is a teaching environment, and one of the things this class teaches is how to find out how research questions like these, I'll point you in the right direction. ;-)
Like all programming problems, it's best broken down into parts. In this case, you want to:
Read in a ...
The source code for the GNU C standard library implementation of vprintf (which is called by printf) should indicate that it is not a simple operation with a single answer, however, understanding printf works will reveal a lot about the intricacies of languages in general.
In the theoretical sense, the formatting parameters used by printf and scanf, and ...
Use GetInt() from the cs50 library!
GetInt(), as its name suggests, waits for an int input from the user and returns it.
You may use like that:
// prompt the user for an int
printf("Please give me an int:");
int i = GetInt();
printf("Thanks for the %d", i);