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

The above is perhaps a starter program. Still struck. Number is type int and so I am using %i in the scanf command.

This is the error message:

mario.c:6:29: error: format specifies type 'int *' but the argument has type 'int' [-Werror,- 
Wformat]
scanf ("enter a number %i", number);
                   ~~   ^~~~~~

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 mario.c.
~/pset1/ $ 

@RobertS.Pratt

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

Putting & before the variable name in scanf worked. However, when I run, I do not receive "enter a number" text. Do I need to create/define any more variable that will hold that text? Also, printf do not return the integer that is entered from the keyboard as part of scanf.

Here is the result as I enter 44

~/pset1/ $ ./Mario
44
the number is 0~/pset1/ $

2 Answers 2

1

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 provided. Instead, you need to give it a pointer to where the data for that variable is stored. You do this by adding an ampersand (&) before the name of the variable.

Changing it from scanf("enter a number %i", number); to scanf("enter a number %i", &number); should fix this for you.

Hopefully that makes sense and helps. If it does, you can accept this answer by clicking on the check mark beside it. But if it doesn't, let me know and I will try to help further.


Regarding your additional question with no value being stored in number, this is why I recommended that you take a look at the man pages. ;)

Here is the exact example from the man page for scanf that shows how it should be used:

#include <stdio.h>

int main(void)
{
    int i;
    printf("Input: ");
    scanf("%i", &i);
    printf("Output: %i\n", i);
}

See, scanf does not print anything to the screen, it only scans what was last typed by the user. So, your original usage was always going to run into this issue.

I was trying to provide a strong hint about looking at the man page by putting it at the beginning of my answer with a link; I was a teacher for many years and I try to point people to the resources before providing the answer. But, I could've at least given you direction that you would need to look at the page to avoid another error you'll soon have, so I'll take that as poor direction from me.

Your current solution has your program scanning for input before any happens. Prompt the user for input, then scan it, and you should have everything functioning as you expect.

Let me know if you run into any other issues, but this is all that I see.

1
  • Thanks so much. Will be on my desktop tomorrow and continue. @RobertS.Pratt Commented Aug 6, 2021 at 11:41
0

Remove all the other keyword from scan except placeholder. scanf willn't read white-spaces or any other character unless you explicitly said it to do so. If you want to print out something before scanf prompt for input, you can modify your scanf function a little : scanf("%[^\n]%*c", str); But i'll suggest you, just use it without any of this mess like this scanf("%i", &i);

You must log in to answer this question.

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