Scanning a floating variable

I've made a very silly testing program, because for some reason
I can't conclude this most simple operation. Despite the value
I assign to t, it's always returning

s=-0.750000

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

int main () {
   double t, s;
   printf ("Enter a real number: \n");
   scanf("%f", &t);
   s=t-0.75;
   printf ("s is %f \n", s);
   system ("PAUSE");
   return 0;

}


Can you please help me?
dude, stop using scanf.
it's scanf("%lf",&t); because it is double
if it was float you would use scanf("%f",&t);

@mutexe
dude, stop using scanf.

could you tell us why?
Yes, see my link in Marcial's other post:
http://www.cplusplus.com/forum/beginner/121343/

I've tried using cin and cout and it returned:

'cout' undeclared (first use this function)
'cin' undeclared (first use this function)

Now I'm using %lf and it's working. Thanks, JewelCpp. But I'm still having problems with characters, why does scanf work pretty well
with numbers, but not with characters? What can I possibily doing wrong?
you need #include<iostream> to use cin and cout, and call them like this:
std::cin
std::cout

are you trying to learn c or c++?
With cout/cin you should #include <iostream> .
And, yes, you should really use cout/cin.
If you use C, use printf/scanf.
But you're using C++, use cout/cin.
If you use C, use printf/scanf


scanf_s
http://msdn.microsoft.com/en-us/library/w40768et.aspx
mutexe, excuse me if it's a too basic question, but how exactly do I "call" them? Shall I simply write

std::cin
std::cout

?
If so, where shall I put this? Before the int main()? Because it isn't working, it gives

expected constructor, destructor, or type conversion before "std"
expected `,' or `;' before "std"

Also I couldn't find an answer here: http://www.cplusplus.com/doc/tutorial/basic_io/
Last edited on
This is all explained in the tutorial and reference information on this site:

http://www.cplusplus.com/doc/tutorial/basic_io/

It should also be adequately covered by whatever textbook you're using to learn from.
Immediate benefit to using std::cin over scanf, you don't have specify what type of primitive you're handling. std::cin can even input std::string without specifying anything. Correct me if I'm wrong, but I don't believe std::string is even a primitive.
@mutexe: That's a windows extension.
There's no scanf_s in the C library (Search in the Reference pages in the forum).
Topic archived. No new replies allowed.