C++ Cin 输入到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39898918/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:51:48  来源:igfitidea点击:

C++ Cin input to array

c++arrayscin

提问by Dylan Galea

I am a beginner in c++ and I want to enter a string as character by character into an array , so that I can implement a reverse function .. However unlike C when the enter is hit a '\n' is not insterted in the stream.. how can I stop data from being entered ?

我是 C++ 的初学者,我想将一个字符串作为一个字符一个字符地输入到一个数组中,这样我就可以实现一个反向函数..但是与 C 不同,当输入时,'\n' 没有插入到流中.. 我怎样才能停止输入数据?

my code is :

我的代码是:

#include<iostream>
#include<array>
#define SIZE 100
using namespace std;

char *reverse(char *s)
{
    array<char, SIZE>b;
    int c=0;
    for(int i =(SIZE-1);i>=0;i--){
        b[i] = s[c];
        c++;
    }

    return s;
} 

int main()
{
    cout<<"Please insert a string"<<endl;
    char a[SIZE];
    int i=0;
    do{
        cin>>a[i];
        i++;
    }while(a[i-1]!= '
std::string str;
while (true)
{
    char ch;
    std::cin >> ch;
    if (ch == '\n')
    {
        break;  // End loop
    }

    str += ch;  // Append character to string
}
'); reverse(a); return 0; }

回答by Some programmer dude

When you read character by character, it really reads characters, and newline is considered a white-space character.

当您逐个字符读取时,它真正读取的是characters,而换行符被视为空白字符

Also the array will never be terminated as a C-style string, that's not how reading characters work. That means your loop condition is wrong.

此外,数组永远不会作为 C 样式字符串终止,这不是读取字符的工作方式。这意味着您的循环条件是错误的。

To begin with I suggest you start using std::stringfor your strings. You can still read character by character. To continue you need to actually check what characters you read, and end reading once you read a newline.

首先我建议你开始使用std::string你的字符串。您仍然可以逐个字符地阅读。要继续,您需要实际检查您阅读的字符,并在阅读换行符后结束阅读。

Lastly, your reversefunction does not work. First of all the loop itself is wrong, secondly you return the pointer to the original string, not the "reversed" array.

最后,您的reverse功能不起作用。首先循环本身是错误的,其次返回指向原始字符串的指针,而不是“反向”数组。



To help you with the reading it could be done something like

为了帮助您阅读,可以这样做

#include <string>
#include <algorithm>
#include <iostream>

int main(){
    std::string str;
    std::cout << "Enter a string: ";
    std::getline(std::cin, str);

    std::reverse(str.begin(), str.end());

    std::cout << str << std::endl;

    return 0;
}

Do note that not much of this is really needed as shown in the answer by Stack Danny. Even my code above could be simplified while still reading one character at a time.

请注意,如Stack Danny 的回答所示,真正需要的并不多。即使我上面的代码也可以简化,同时仍然一次读取一个字符。

回答by Stack Danny

Since you tagged your question as C++(and not C) why not actually solve it with the modern C++ headers (that do exactly what you want, are tested, save and work really fast (rather than own functions))?

既然您将您的问题标记为C++(而不是C),为什么不实际使用现代 C++ 标头来解决它(这些标头完全符合您的要求,经过测试、保存和工作速度非常快(而不是自己的函数))?

Enter a string: Hello Test 4321
1234 tseT olleH

output:

输出:

##代码##