我在使用 cin.getline() 从用户那里获取 3 个字符串并使用指针数组来存储它们时遇到问题

I am having trouble getting 3 strings from the user using cin.getline() and using a pointer array to store them

本文关键字:数组 指针 串并 存储 字符串 问题 遇到 字符 getline cin 获取      更新时间:2023-10-16

我在我的cin.getline上抛出了一个异常。我是 c++ 的新手,如果代码真的很糟糕,我很抱歉。

GetInput 函数应执行以下操作: 此函数接受三行用户输入的文本,并将输入的行存储为三个单独的字符串。 使用指针数组存储字符串。 该函数应要求用户输入三行数据。 该函数会将信息存储在指针数组中。 (cin.getline()). 应该在 GetInput 内部分配,并且每个字符串的大小应该完全正确。

int main() {
char* str[2];
GetInput(str); 
}
void GetInput(char* ptr[]) {
for (int i = 0; i < 3; i++) {
cout << "Enter a string: ";
cin.getline(ptr[i], strlen(ptr[i]), 'n');
}
}

不幸的是,作业需要指针,因为这在使用std::vector<std::string>很容易。如果确实需要使用指针,请使用智能指针,例如std::unique_ptr。当它们被销毁(超出范围)时,它们将delete/delete[]它们拥有的原始指针,从而有助于防止内存泄漏。因此,使用数组和指针,这是可以完成的一种方式。

#include <iostream>
#include <array>    // std::array
#include <cstring>  // std::memcpy
#include <memory>   // std::unique_ptr
constexpr size_t max_line_length = 256;
// convenience alias
using CStrPtr = std::unique_ptr<char[]>;
void GetInput(std::array<CStrPtr, 3>& ptrs) {
char buf[max_line_length]; // temporary buffer when reading a line
// loop through the 3 pointers in the array
for(CStrPtr& cptr : ptrs) {
std::cout << "Enter a string: ";
std::cin.getline(buf, max_line_length);
size_t len = std::strlen(buf) + 1; // +1 to make place for the string terminator, ''
// create a new pointer to a char[] with space for the string
// and assign it to the pointer in the array
cptr = std::make_unique<char[]>(len);
// copy the string in buf into the space the raw pointer held by cptr now points to
std::memcpy(cptr.get(), buf, len);
}
}
int main() {
std::array<CStrPtr, 3> strs;
GetInput(strs);
for(const CStrPtr& cptr : strs) {
std::cout << cptr.get() << "n";
}
}

如果不允许使用标准智能指针,可以创建自己的智能指针或简单的字符串类。这是上面的一个版本,但有一个简单的字符串类:

#include <iostream>
#include <array>     // std::array
#include <cstring>   // std::strlen
#include <algorithm> // std::copy
constexpr size_t max_line_length = 256;
class cstring {
char* m_mem; // the precious pointer
public:
cstring() : // default constructor
m_mem(new char[1]) // make place for the string terminator
{
m_mem[0] = ''; // the string terminator
}
cstring(char const* buf) : // converting constructor
m_mem{}
{
// allocate memory
size_t len = std::strlen(buf) + 1;
m_mem = new char[len];
// and copy buf to m_mem
std::copy(buf, buf+len, m_mem);
}
cstring(const cstring& o) : // copy ctor
cstring(o.m_mem) // delegate to converting ctor
{}
cstring(cstring&& o) : // move ctor
// copy the pointer from o and set o:s pointer to nullptr
m_mem(std::exchange(o.m_mem, nullptr))
{}
cstring& operator=(const cstring& o) { // copy assignment
*this = cstring(o); // using copy ctor + move assignment
return *this;
}
cstring& operator=(cstring&& o) { // move assignment
// swap pointers: let o destroy our old pointer for us
std::swap(m_mem, o.m_mem);
return *this;
}
~cstring() { delete[] m_mem; } // destructor
// user-defined conversions
operator char const* () const { return m_mem; }
operator char* () { return m_mem; }
};
void GetInput(std::array<cstring, 3>& ptrs) {
char buf[max_line_length]; // temporary buffer when reading a line
// loop through the 3 pointers in the array
for(cstring& cptr : ptrs) {
std::cout << "Enter a string: ";
std::cin.getline(buf, max_line_length);
// create a new cstring and assign it to the cstring in the array
cptr = cstring(buf);
}
}
int main() {
std::array<cstring, 3> strs;
GetInput(strs);
for(const cstring& cptr : strs) {
// cptr will here use the user-defined conversion to "char const*"
// for which there's a standard operator<< defined
std::cout << cptr << "n";
}
}
#include <iostream>
#include <string>
using namespace std;
void getLine(string ptr[]) {
for (int index = 0; index < 3; ++index) {
string line;
getline(cin, line);
ptr[index] = line;
}
}
int main() {
string ptr[3];
getLine(ptr);
for (int index = 0; index < 3; ++index) {
cout << ptr[index] << endl;
}
}

你的代码有几个错误。首先,您没有为阵列分配足够的空间。长度应该是数组的长度,而不是最后一个元素的索引。所以如果是 3 个字符串,那就是 [3]。

接下来,我不会使用 char *,而是使用字符串类。请参阅我的代码。

第三,你的strlen的东西 - 嗯,你有一个指向内存中随机位置的指针数组,或者可能是空指针数组。无论哪种方式,都是错误的。

你可以做这样的事情:

char myStrings[3][1000];

然后在你的 getline 中,使用 1000 作为最大长度(也许是 999 - 我不以你所做的形式使用 getline)。这将分配 3 个字符数组,每个数组 1000 字节。

但是平坦的 strlen 东西是行不通的,因为你不会从任何字符串开始