I have just recently started working on scanf and printf in general, and in my research and general screwing around I have run into the problem in my code multiple times so I decided to make a test program and have successfully replicated the error.
#include <string>
#include <string.h>
#include <stdio.h>
int main(){
std::string name;
std::string name2;
printf("Print donald or larry: ");
scanf("%s",name);
if(strcmp(name.c_str(), "donald") == 1){
printf("You entered donald");
goto stop;
}else{
printf("You entered larry");
goto stop;
}
stop:
return 0;
}
when I attempt to compile the code it throws me an error coming from line 10
error:
error: cannot pass objects of non-trivially-copyable type 'std::string {aka
class std::basic_string<char>}' through '...'|
source:
scanf("%s", name);
I have tried both name.c_str and &name and they both don't work, name.c_str makes the if statement bug out and not work but &name just crashes altogether, although I have found that &<variable> only works with integers so far.
What am i doing wrong?
if(strcmp(name.c_str(), "donald") == 1){Nope.coutandcin.string. If C++,stringis fine, but no more scanf:std:cin >> name;