PROBLEM WITH SCANF_S

Write your question here.
When I run this short program, it bombs out at the printf statement. See output. How come?

1
2
3
4
5
6
7
8
9
#include <cstdio>

int main(void)
{
	char s[16];
	printf("String please: ");
	scanf_s("%s", s);
	printf("Thanks for the %s!\n", s);
}


String please: Hello
Thanks for the !
Press any key to continue...


line 7 should be:
 
scanf_s("%s", s, 16);


scanf_s takes a 3rd parameter, the size.
Uh...if this is C++, why are you using printf and scanf?

Try
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
    std::string s;
    std::cout << "String please: ";
    std::getline(std::cin, s); // Or std::cin >> s; for one word only
    std::cout << "Thanks for the " << s << "!\n";
}

Otherwise, if this is supposed to be C code, change the #include <cstdio> to #include <stdio.h> and try mutexe's suggestion above.
Thank you mutexe. It works.

long double main,

I am taking a free online course that uses C code. I want to eventually program in C++. I know I will run into problems because of the differences. However, I feel the effort is worth it. My understanding in taking this course is that you want to use library functions as much as possible.

It appears that C++ no longer supports printf and scanf but has similar fucntions like, printf_s and scanf_s. My assumption is that you can still use these functions; otherwise they would be deleted from the library.
Topic archived. No new replies allowed.