在c+;中使用std::getline()接受带空格的输入+; 我在C++中编码很新,我在Mac上使用VisualStudio代码。我试图找到正确的代码,以便输入带有空格的字符串,但我尝试的一切都没有成功。我尝试了std::getline(std::cin,name),但是当我运行代码时,到了输入名称的时候,它只是没有显示名称应该在哪里。我不知道VisualStudio代码是否有问题,或者是否有一点点代码我遗漏了。这是我遇到问题的代码 #include <iostream> #include <string> #include <cmath> #include <sstream> #include <string.h> int main (){ std::string name; std::cout << "Please, enter the name of your nation: "; std::getline(std::cin, name); std::cout << "General Williams: " << name << " is a great name!\n"; } #包括 #包括 #包括 #包括 #包括 int main(){ std::字符串名; 我执行了你的代码,它工作了。 可能您无法看到结果,因为您使用.exe运行

在c+;中使用std::getline()接受带空格的输入+; 我在C++中编码很新,我在Mac上使用VisualStudio代码。我试图找到正确的代码,以便输入带有空格的字符串,但我尝试的一切都没有成功。我尝试了std::getline(std::cin,name),但是当我运行代码时,到了输入名称的时候,它只是没有显示名称应该在哪里。我不知道VisualStudio代码是否有问题,或者是否有一点点代码我遗漏了。这是我遇到问题的代码 #include <iostream> #include <string> #include <cmath> #include <sstream> #include <string.h> int main (){ std::string name; std::cout << "Please, enter the name of your nation: "; std::getline(std::cin, name); std::cout << "General Williams: " << name << " is a great name!\n"; } #包括 #包括 #包括 #包括 #包括 int main(){ std::字符串名; 我执行了你的代码,它工作了。 可能您无法看到结果,因为您使用.exe运行,c++,C++,请在代码末尾添加“std::getchar()”,然后重试 int main() { std::string name; std::cout << "Please, enter the name of your nation: "; std::getline(std::cin, name); std::cout << "General Williams: " << name << "

请在代码末尾添加“std::getchar()”,然后重试

int main()
{
        std::string name;

        std::cout << "Please, enter the name of your nation: ";
        std::getline(std::cin, name);
        std::cout << "General Williams: " << name << " is a great name!\n";
        std::getchar();

}
intmain()
{
std::字符串名;

我执行了你的代码,它工作了。 可能您无法看到结果,因为您使用.exe运行

请在代码末尾添加“std::getchar()”,然后重试

int main()
{
        std::string name;

        std::cout << "Please, enter the name of your nation: ";
        std::getline(std::cin, name);
        std::cout << "General Williams: " << name << " is a great name!\n";
        std::getchar();

}
intmain()
{
std::字符串名;

这里显示的代码能正常工作吗(虽然有几个多余的头包含,但它们应该无关紧要。我怀疑您与VSCode的集成问题比其他任何东西都严重。使用clang++从命令提示符编译此文件,从shell运行它,它应该可以工作。您可以解释更多吗,我对编码非常陌生。你说clang++和shell是什么意思SCODES网站将更好地解释IDE如何比我能更好地工作,就像任何数量的WHO站点,包括如何在各种平台(包括MAC)上使用VCSODE开发C和C++代码项目。不管我说什么,代码看起来都是可以接受的。(虽然有几个多余的头包含,但它们应该无关紧要。我怀疑您与VSCode的集成问题比其他任何东西都严重。使用clang++从命令提示符编译此文件,从shell运行它,它应该可以工作。您可以解释更多吗,我对编码非常陌生。你说clang++和shell是什么意思SCODES网站将更好地解释IDE如何比我能更好地工作,就像任何数量的WHO站点,包括如何在各种平台(包括MAC)上使用VCSODE开发C和C++代码项目。