% meaning

what does it mean %d?
scanf("%d",&num[i]);
im gonna write this with cin>>&num[i];
but it doesnt work
See "format specifier" in:
http://www.cplusplus.com/reference/cstdio/scanf/

it doesnt work

What do you mean by "not work"? Is it perhaps a failure to compile, with the compiler telling that there is an error? If so, what does the compiler say, exactly?
thanks
i have few codes and im gonna change printf and scanf with cout and cin
for example
printf("enter fiver numbers: ");
cout<<"enter five numbers";

can you tell me scanf("%d",&num[i]); with cin.....?
Last edited on
%d is a format specifier for printf and scanf. It tells scanf to read a decimal integer and store it in the value pointed to by the first argument passed to it. You can read more about format specifiers for scanf at http://www.cplusplus.com/reference/cstdio/scanf/.

In C++, cin is overloaded to take an int reference as its argument. This means it can modify the variable passed to it. The compiler will figure out you passed an int to the stream extraction operator (operator>>) and it will call the right function of cin automatically. This value reads a decimal integer from the input and stores it in its argument.

So to convert scanf to cin, you should discard the format specifier string (its no longer required), and pass a reference instead of a pointer (discard the & operator). And example:

1
2
3
4
5
//Using scanf, we have a format specifier (%d) and pass a pointer to num[i] (operator&)
scanf("%d",&num[i]);

//Using cin, we do not have a format specifier and we pass a reference to num[i] (no operator required)
cin >> num[i];
Last edited on
thanks got it
Topic archived. No new replies allowed.