Solved

Where Did I Go Wrong?

Posted on 2008-10-10
7
2,469 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ios - android compatible database app 3 92
Error E2158 compiling with Delphi XE10 Seattle 2 98
Check version 13 74
Where is Outlook in Paperport 14? 8 56
A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

813 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

11 Experts available now in Live!

Get 1:1 Help Now