Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
This question I guess very very easy but I don't understand that it works with scanf and Code BLock Ide. But it doesnt work scanf_s and in Visual Studio. Why?

I get exception when I run this code part.

<pre>#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>


int main()
{
	char city[10];
	for (int i = 0; i < 2; i++)
	{
		
		printf("Please enter name of city:\n");
		scanf_s("%s", city);
	}

	for (int i = 0; i < 2; i++)
	{
		printf("%s",city);
	}

	return 0;
}


What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>


int main()
{
	char city[10];
	for (int i = 0; i < 2; i++)
	{
		
		printf("Please enter name of city:\n");
		scanf_s("%s", city);
	}

	for (int i = 0; i < 2; i++)
	{
		printf("%s",city);
	}

	return 0;
}
Posted
Updated 28-Jun-20 4:11am
v2

You are missing the third parameter, which should be the size of the buffer, as described at scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l | Microsoft Docs[^].

The code should be:
C++
scanf_s("%s", city, sizeof(city));
 
Share this answer
 
Most likely, the space you have allocated to city is not big enough to store what you are entering: 10 characters is not a lot, and you are probably exceeding it.
Try this:
int main()
{
	char city[100];
	for (int i = 0; i < 2; i++)
	{

		printf("Please enter name of city:\n");
		scanf_s("%s", city, 10);
	}

	for (int i = 0; i < 2; i++)
	{
		printf("%s", city);
	}

	return 0;
}
It still won't do what you want it to because it will print the last name twice, but it shouldn't crash.
 
Share this answer
 
Comments
goksurahsan 28-Jun-20 10:17am    
Yes it will print the last name twice, but it worked. Is it about character array. Should I use pointer?
OriginalGriff 28-Jun-20 10:29am    
No, it's down to your code "reusing" the same memory space to hold both cities. You can't do that, it overwrites the first city with the last so it prints the same name twice.
It's like a clothes peg - it can hold a shirt, or a pair of pants, but if you use it to peg a shirt on the line to dry you can't "add" a pair of pants to it. When you open the peg to insert the pants the shirt falls to the ground!

Hint: look at creating a two dimensional array and using that instead.
goksurahsan 28-Jun-20 10:58am    
Thanks for answering. I solved it. Thanks again
OriginalGriff 28-Jun-20 11:16am    
You're welcome!
Try with:
C++
scanf_s("%s", &city);

C - Pointers - Tutorialspoint[^]
 
Share this answer
 
Comments
goksurahsan 28-Jun-20 10:11am    
I tried this but it didn't work
OriginalGriff 28-Jun-20 10:25am    
Um ... the name of an array is a pointer to the first element of the array - that's ion the C specification. &city is a pointer to a pointer to a character.
Patrice T 28-Jun-20 10:42am    
I have stopped using C/C++ years ago, but I keep a vivid memory of missing a & with a scan and resulting in HDD MBR being erased :)
OriginalGriff 28-Jun-20 10:57am    
Sounds like a damn good reason for using managed memory languages ... :D

Mind you, I can't talk - back in the day I discovered that FORTRAN's COMMON statement would allow you to change the type of a variable in two different files, and use a CHAR as a multidimensional array of INTs if you wanted. And in Primos, you could modify the OS with that ... :laugh:
Patrice T 28-Jun-20 11:18am    
Agreed, that is why I settled on Clipper years ago, a Xbase family compiler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900