Solved

Where Did I Go Wrong?

Posted on 2008-10-10
7
2,459 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
A list of useful business intelligence software.
Viewers will learn how to use the Hootsuite Dashboard.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

747 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

10 Experts available now in Live!

Get 1:1 Help Now