Look up the format of printf(). Use that info to do your conversion.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Use fgets instead , since scanf is not considered safe.
scanf() is perfectly safe when used correctly. The problem is things like %s without a field width where the programmer has no clue that it's a buffer overflow waiting to happen.
If scanf() is so unsafe, then why is the recommended alternative a combination of fgets() and sscanf()? The only major difference between scanf() and sscanf() is the source of characters. Further, if you read those links, you'll find that the "problems" with scanf() have to do with variations of the following:Not understanding how scanf() manages whitespace
Not understanding how the %s specifier works
While I agree that an intuitive interface and semantics are both important, ignorance of scanf() isn't the fault of scanf(). Were I in a perverse mood, I'd argue that fgets() also has problems because beginners are often confused about how newlines are managed.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
can u help me with scanf? cuz its must be printf and scanf..pls
how to convert this in scanf?
cin.getline(boy,50);
A C programmer would prefer fgets() for string input, but since this isn't an immediately obvious question about scanf(), I'll answer it. The closest equivalent to such a getline() call with scanf() is:
scanf("%49[^\n]", boy);
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401