I am getting this error string undeclared while compiling this code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
string names;
printf("What is your name?\n");
scanf("%s", &names);
printf("Your name is %s", names);
return 0;
}
Can someone tell me why. Many thanks
you should include string header:
#include <string>
and don't forget namespace std when using it:
std::string names;
Besides, don't mix C and C++ when you code. Try to use std::cout not printf, cin/getline not scanf.
You need to add
#include <string>
to reference string from the C++ standard library and state that you're using std with
using namespace std;
See here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With