0
float fahrenheit;
float celsius;
float formulaf = (fahrenheit - 32.0) * 5/9;
char str[20];
float formulac = celsius * 9/5 + 32.0;
printf("Choose either fahrenheit or celsius; ");
scanf("%*s", &str);
if (strcmp(str, "fahrenheit") ==0)
{
    printf("Enter the temperature in fahrenheit: ");
    scanf("%f", &fahrenheit);
    printf("%.2f fahrenheit is %.2f celsius", fahrenheit, formulaf);
}
else
{
    printf("Enter the temperature in celsius: ");
    scanf("%f", &celsius);
    printf("%.2f celsius is %.2f fahrenheit", celsius, formulac);
}

I'm coding this in xcode, I have created the headers and everything, this is the main part where I am stuck, it gives an error in the following code and it gives breakpoints in some other lines, please help me solve this

scanf("%*s", &s); -------> data argument not used by format string

what does this mean?

JGV
  • 5,037
  • 9
  • 50
  • 94
user5032369
  • 5
  • 2
  • 5

2 Answers2

2

If you read e.g. this scanf reference you will see that the "*" modifier in the format string means the scanned string will not be used.

Also, when scanning for strings, you should not use the address-of operator, as strings already are pointers.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

%*s instructs scanf to scan and discard the sting scanned. The compiler is complaining because the second argument of that scanf(&s) is not used.

You probably wanted to use %s or %19s(tells scanf to scan a maximum of 19 characters + 1 for the NUL-terminator) which prevent buffer overflows.

Also change the second argument of scanf to s. This is done because %s expects a char* while &s is of type char(*)[20]. s gets converted into &s[0], a char*, exactly what %s expects.

So the scanf

scanf("%*s", &str);

should be

scanf("%s", str);

for the reasons explained above.


BTW, Your code exhibits Undefined Behavior because when you use

float formulaf = (fahrenheit - 32.0) * 5/9;

and

float formulac = celsius * 9/5 + 32.0;

farenheit and celsius are uninitialized. Move it after they get initialized,i.e, move it after

scanf("%f", &fahrenheit);

and

scanf("%f", &celsius);

respectively.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • And can you please explain why should i not use &str in my code ? – user5032369 Jun 24 '15 at 07:19
  • @user5032369 , It might have worked for your friend. But undefined behavior is undefined. It might work, might not, or do something weird. It might stop working on adding a simple `printf` somewhere etc etc. Read the third paragraph of my answer to know why you need to use `str` instead of `&str`. Alternatively, browse through similar questions on StackOverflow like [this](http://stackoverflow.com/questions/8921617/c-why-no-for-strings-in-scanf-function) – Spikatrix Jun 24 '15 at 07:26