Scanning a floating variable

Jan 8, 2014 at 2:47pm
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?
Jan 8, 2014 at 2:51pm
dude, stop using scanf.
Jan 8, 2014 at 3:06pm
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?
Jan 8, 2014 at 3:16pm
Yes, see my link in Marcial's other post:
http://www.cplusplus.com/forum/beginner/121343/

Jan 8, 2014 at 3:18pm
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?
Jan 8, 2014 at 3:23pm
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++?
Jan 8, 2014 at 3:25pm
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.
Jan 8, 2014 at 3:31pm
If you use C, use printf/scanf


scanf_s
http://msdn.microsoft.com/en-us/library/w40768et.aspx
Jan 8, 2014 at 6:04pm
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 Jan 8, 2014 at 6:12pm
Jan 8, 2014 at 6:13pm
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.
Jan 8, 2014 at 9:13pm
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.
Jan 8, 2014 at 11:52pm
@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.