C++字符串变量声明

发布于 2024-10-13 02:40:22 字数 1044 浏览 4 评论 0原文

我在声明字符串变量时遇到一些问题。代码和错误在这里: http://pastebin.com/TEQCxpZd 关于我做错了什么的任何想法?另外,请保持平台独立。谢谢!

#include <stdio.h>
#include <string>
using namespace std;

int main()
{
    string input; //Declare variable holding a string

    input = scanf; //Get input and assign it to variable
    printf(input); //Print text
    return 0;
}


Getting this from GCC:

main.cpp: In function ‘int main()’:
main.cpp:53:10: error: invalid conversion from ‘int (*)(const char*, ...)’ to ‘char’
main.cpp:53:10: error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’
main.cpp:54:14: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’

I'm having some trouble declaring a string variable. Code and the errors are here: http://pastebin.com/TEQCxpZd Any thoughts on what I'm doing wrong? Also, please keep it platform independent. Thanks!

#include <stdio.h>
#include <string>
using namespace std;

int main()
{
    string input; //Declare variable holding a string

    input = scanf; //Get input and assign it to variable
    printf(input); //Print text
    return 0;
}


Getting this from GCC:

main.cpp: In function ‘int main()’:
main.cpp:53:10: error: invalid conversion from ‘int (*)(const char*, ...)’ to ‘char’
main.cpp:53:10: error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’
main.cpp:54:14: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住你手 2024-10-20 02:40:22

您正在混合 c++ 和 c I/O。在 C++ 中是这样的:

#include <string>
#include <iostream>

int main(void)
{
   std::string input;
   std::cin >> input;
   std::cout << input;
   return 0;
 }

You are mixing c++ and c I/O. In C++ this is,

#include <string>
#include <iostream>

int main(void)
{
   std::string input;
   std::cin >> input;
   std::cout << input;
   return 0;
 }
虚拟世界 2024-10-20 02:40:22

我理解这个问题是:如何在 C++ 中进行字符串声明?
下面是一个简短的程序来演示:

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    string your_name;
    cout << "Enter your name: ";
    cin >> your_name;
    cout << "Hi, " << your_name << "!\n";
    return 0;
}

因此,在程序的开头包含 cstdlib。实际上,这意味着输入 string 而不是 std::string,输入 cout 而不是 std::cout 等等。字符串变量本身(在示例中,字符串变量是 your_name)用 string 声明。

假设您已使用文件名“str_example.cpp”保存了程序
在命令行(Linux 中)编译程序:

g++ -o str_example str_example.cpp

这将创建一个名为 str_example(无文件扩展名)的可执行目标文件。
最后,假设您与程序位于同一目录中,运行它:

./str_example

g++ 的手册页很丰富,但默认情况下不包含在内。要使用 aptitude 包管理器安装 g++ 文档:

sudo apt-get install gcc-7-doc

请注意,“7”指的是版本 7;撰写本文时的当前版本。希望有帮助。

I understand the question to be: How do you make a string declaration in C++?
Here's a short program to demonstrate:

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    string your_name;
    cout << "Enter your name: ";
    cin >> your_name;
    cout << "Hi, " << your_name << "!\n";
    return 0;
}

So, include cstdlib at the start of your program. In practical terms, this means typing string instead of std::string, cout instead of std::cout and so on. The string variable itself (in the example, the string variable is your_name) is declared with string.

Let's say you've saved the program with the filename, 'str_example.cpp'
To compile the program at the command line (in Linux):

g++ -o str_example str_example.cpp

This creates an executable object file called str_example (no file extension).
And finally, assuming you're in the same directory as the program, to run it:

./str_example

The man page for g++ is extensive but not included by default. To install g++ documentation using the aptitude package manager:

sudo apt-get install gcc-7-doc

Note that the '7' refers to version 7; the current version at the time of writing. Hope that helps.

扬花落满肩 2024-10-20 02:40:22

无法将“std::string”转换为“const”
char*' 对于参数 '1' 到 'int
printf(const char*, ...)'

input = scanf; //Get input and assign it to variable

您试图将 scanf函数指针分配给一个字符串变量。你不能这样做,这就是你收到第一个错误的原因。正确的语法是。

char buffer[BIG_ENOUGH_SIZE];
scanf("%*s", sizeof(buffer) - 1, buffer);
input = buffer;

但这是一种非常 C 风格的做事方式。在 C++ 中读取输入的惯用方法是使用 std::cin >>>按照内森的建议输入

无法将“std::string”转换为“const”
char*' 对于参数 '1' 到 'int
printf(const char*, ...)'

printf(input); //Print text

printf 采用 const char* 作为其第一个参数,而不是 std::string。您可以使用 .c_str() 转换为 C 样式字符串。但从不将用户输入作为第一个参数传递给printf;用户可以通过在字符串中放入 % 来做一些令人讨厌的事情。如果您坚持使用 C 样式输出,则正确的语法是:

printf("%s", input.c_str());

但 C++ 样式的替代方案是 std::cout <<输入;

cannot convert ‘std::string’ to ‘const
char*’ for argument ‘1’ to ‘int
printf(const char*, ...)’

input = scanf; //Get input and assign it to variable

You're trying to assign the function pointer to scanf to a string variable. You can't do that, which is why you're getting the first error. The proper syntax would be.

char buffer[BIG_ENOUGH_SIZE];
scanf("%*s", sizeof(buffer) - 1, buffer);
input = buffer;

But that's a very C-style way of doing things. The idiomatic way to read input in C++ is with std::cin >> input as Nathan suggested.

cannot convert ‘std::string’ to ‘const
char*’ for argument ‘1’ to ‘int
printf(const char*, ...)’

printf(input); //Print text

printf takes a const char* as its first argument, not a std::string. You can use .c_str() to convert to a C-style string. But never pass user input as the first argument to printf; the user can do nasty stuff by putting %'s in the string. If you insist on C-style output, the correct syntax is:

printf("%s", input.c_str());

But the C++-style alternative is std::cout << input;.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文