C++ 将输入字符串转换为字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20428395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert input string into a char array
提问by Syngularity
My problem is that, I have a string that I get from keyboard and want to save it in to a char array. After I get the array I want to turn it in to a number or something.
我的问题是,我有一个从键盘获取的字符串,并想将它保存到一个字符数组中。在我得到数组后,我想把它变成一个数字或其他东西。
I tried many thing but it's doesn't work. This is my best solution so far:
我尝试了很多东西,但它不起作用。到目前为止,这是我最好的解决方案:
string input_string;
char char_string[20];
cout << "type in some input text:$" << endl;
cin >> input_string;
strcpy(char_string, input_string.c_str());
for (int i = 0; i < 20 ; i++)
{
switch(char_string[i])
{
case 'a' : cout << "a" << endl; break;
case 'b' : cout << "b" << endl; break;
case 'c' : cout << "c" << endl; break;
case 'd' : cout << "d" << endl; break;
case 'e' : cout << "e" << endl; break;
case 'f' : cout << "f" << endl; break;
...
but when I run this code I get something thy i don't expect. If the input is random random
但是当我运行这段代码时,我得到了一些我不期望的东西。如果输入是random random
I get this:
我明白了:
r
a
n
d
o
m
contain uavialba char
contain uavialba char
contain uavialba char
contain uavialba char
contain uavialba char
contain uavialba char
contain uavialba char
contain uavialba char
contain uavialba char
v
contain uavialba char
z
contain uavialba char
contain uavialba char
But i want this:
但我想要这个:
r
a
n
d
o
m
r
a
n
d
o
m
回答by john
The problem is here
问题就在这里
for (int i = 0; i < 20 ; i++)
You alwaysoutput 20 characters. You should stop outputting when you get to the end of the string.
你总是输出 20 个字符。当您到达字符串的末尾时,您应该停止输出。
for (int i = 0; char_string[i] != 'cin >> input_string;
' ; i++)
C style strings have a '\0'
character at the end, you should test for this.
C 样式字符串'\0'
的末尾有一个字符,您应该对此进行测试。
Seems the other error is that you read a word instead of a line. Instead of this
似乎另一个错误是你读了一个词而不是一行。而不是这个
getline(cin, input_string);
try this
尝试这个
cout << char_string[i] << endl;
getline
reads a whole line, >>
reads a single word.
getline
读一整行,>>
读一个词。
BTW there's an easier way to write the code inside the for loop. You don't need switch, just do this instead
顺便说一句,有一种更简单的方法可以在 for 循环中编写代码。你不需要开关,只需这样做
for(char c : input_string)
{
switch(c)
{
case 'a' : cout << "a" << endl; break;
case 'b' : cout << "b" << endl; break;
...
回答by David
You don't need char_string
at all. Just pretend input_string
is an array of char
s (it doescontain one, after all). For example, input_string[7]
will give you a char
.
你根本不需要char_string
。假装input_string
是一个char
s数组(毕竟它确实包含一个)。例如,input_string[7]
会给你一个char
.
//g++ -std=c++0x sample.cpp
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
string input_string;
cout << "type in some input text:$" << endl;
cin >> input_string;
for(auto c : input_string)
cout << c << endl;
for_each(input_string.begin(), input_string.end(), [](char c){ cout << c << endl; });
return 0;
}
回答by BLUEPIXY
It is preferable to write to not depend on the size and're using std::string.
最好写入不依赖于大小并使用std::string。
##代码##