简体   繁体   English

将字符串用户输入读取到任意大小的数组中

[英]Reading string user input into an array of any size

so I'm currently trying to read user input into a char array, but every single example I've looked at defines the size of the array upon its initialization. 因此,我目前正在尝试将用户输入读取到char数组中,但是我查看的每个示例都在初始化时定义了数组的大小。 What I'm looking for, essentially, is a way to read user input (perhaps with getline, as I would want to read user input as a string) and store it in an array. 本质上,我正在寻找的是一种读取用户输入的方法(也许使用getline,因为我想将用户输入读取为字符串)并将其存储在数组中。

Let's say a user inputs this into the program: 假设用户将其输入到程序中:

This is a string

I would want the array size to be able to fit that string, and place the null terminator after the "g". 我希望数组大小能够适合该字符串,并将空终止符放在“ g”之后。 Then, another user could put a string of any size that they so desired into the program, but I would basically want my program to always make the array size just enough to contain what was read in from input. 然后,另一个用户可以将他们想要的任意大小的字符串放入程序中,但是我基本上希望我的程序始终使数组大小恰好足以容纳从输入中读取的内容。

I haven't been able to get this working and it's been a couple of hours of browsing endless pages, so any help would be appreciated! 我一直无法正常工作,浏览无休止的页面已经花费了几个小时,因此我们将不胜感激! Thanks. 谢谢。

As Tony Delroy said on his comment (I can't comment yet), you should be using std::string . 正如Tony Delroy在评论中说的那样(我无法评论),您应该使用std :: string

If you really need an char array, as parameter to a function for example, you can use the function c_str() to get the content of the std::string as a const char* array or if you need a char* array, you can copy the content of the array given by c_str() to a dynamically allocated array, using 如果确实需要char数组,例如作为函数的参数,则可以使用函数c_str()来获取std :: string的内容作为const char *数组,或者如果您需要char *数组,则可以可以使用以下命令将c_str()给定的数组的内容复制到动态分配的数组中:

char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());

As an addend, you need to include the header cstring in order to use the function strcpy and need to use delete[] cstr to delete the char* when you're not going to use it anymore 作为附加,您需要包括标头cstring才能使用strcpy函数,并且在不再使用char *时需要使用delete [] cstr来删除char *

#include <iostream>
#include <cstring>
using namespace std;

// string argument as std::string
void foo(string str) {
    // function body
}

// argument as const char*
void bar(const char* str) {
    // function body
}

// argument as char*
void baz(char* str) {
    // function body
}

int main() {
    string str;

    getline(cin, str);

    foo(str);
    bar(str.c_str());

    char* cstr = new char[str.length() + 1];
    strcpy(cstr, str.c_str());
    baz(cstr);

    delete[] cstr;
    return 0;
}

you should use std::string for that. 您应该使用std :: string。 the null terminator has no use in std::string, because you can just use: 空终止符在std :: string中没有用,因为您可以使用:

string.size() 

to get the size of the user input. 获取用户输入的大小。

if want to traverse a string like a char array one by one it should look like something like this: 如果要一遍遍遍像char数组这样的字符串,它应该看起来像这样:

std::string input;
std::getline(std::cin, input);

for (int i = 0; i < input.size() ; i++)
{
   std::cout << input[i];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM