Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when compiling code

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

like image 918
Zackey Haslow Avatar asked Jul 19 '26 21:07

Zackey Haslow


2 Answers

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.

like image 182
taocp Avatar answered Jul 21 '26 13:07

taocp


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.

like image 44
TheDarkKnight Avatar answered Jul 21 '26 11:07

TheDarkKnight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!