Solved

Where Did I Go Wrong?

Posted on 2008-10-10
7
2,465 Views
Last Modified: 2013-12-14
The below is the warning I get when I try to debug inputs to my program:
warning C4996: 'scanf' was declared deprecated

It refers to this string:  scanf(" %c%c%c", &first, &second, &third);

I cannot figure out what is wrong.  Can anyone help?
0
Comment
Question by:Noo2this
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:StrongBad_Rules
ID: 22692679
Could you put the whole code on here or at least the declarations?
0
 

Author Comment

by:Noo2this
ID: 22692689

int

main(void)

{

char first, second, third; /* input - first 3 letters of your name */

int yard_length, house_length; /* input - yard and house length */

int yard_width, house_width; /* input - yard and house width */

int grass_area; /* output - what you are working with */

int time_required; /* output - time required to cut the grass */
 

/* Get and display the customer's name. */

printf("Type in first 3 letters of your name and press return> ");

scanf(" %c%c%c", &first, &second, &third);

printf("Hello %c%c%c, let's see how big your yard is.\n", first, second, third);

	

Open in new window

0
 
LVL 8

Expert Comment

by:Thejaka
ID: 22692854
This is a microsoft CRT related warning. You can use

#pragma warning(disable:4996)

to disable the warning, then

#pragma warning(default:4996)

to reset it. Use before, after main method, respectively.

Alternately, try a later Microsoft CRT, or an alternate CRT.

Also, you can use scanf_s instead:
http://msdn.microsoft.com/en-us/library/w40768et(VS.80).aspx
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:Noo2this
ID: 22693717
Thejaka,
I used scanf_s and it gave me access violations.  I'm new to this visual studio so where exactly do I put the #pragma warning(disable:4996) in my program?
0
 
LVL 8

Expert Comment

by:Thejaka
ID: 22693953
The access violations may have been caused by your compile/link options. Try dynamically linking to CRT by selecting "Multithreaded DLL" build option and linking with msvcp, msvcrt libraries.

For the other, try like this:

#pragma warning(disable:4996)

int main(void)

{

  // Some code...

}

#pragma warning(default:4996)

Open in new window

0
 
LVL 8

Accepted Solution

by:
Thejaka earned 30 total points
ID: 22693974
Regarding the access violation:

In case you didn't check the msdn link, note the following excerpt:

==========

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows:

char s[10];

scanf_s("%9s", s, 10);

The buffer size includes the terminating null. A width specification field may be used to ensure that the token read in will fit into the buffer. If no width specification field is used, and the token read is too big to fit in the buffer, nothing will be written to that buffer.

==========

Your line of code could be written as follows:

scanf_s(" %c%c%c", &first, 1, &second, 1, &third, 1);

Open in new window

0
 

Author Closing Comment

by:Noo2this
ID: 31505250
Thejaka, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let’s list some of the technologies that enable smooth teleworking. 
If your app took Google’s lash recently, here are the 5 most likely reasons.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now