gpt4 book ai didi

c++ - 将字符数组用作多种用途的通用变量

转载 作者:行者123 更新时间:2023-11-30 03:17:39 25 4
gpt4 key购买 nike

需要有关将 1 个字符数组用于多个用户输入的最佳方法的帮助,将用户输入数据传递给类 setMethod(),然后为下一个用户输入语句释放或重用相同的字符数组变量,冲洗并重复.我知道对于指针,您可以删除与它们关联的内存地址以释放空间,所以我认为这是我最好的选择?

// 9Application01.cpp
// Zachary James Mcclurg

#include "pch.h"
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;
const int MAXINPUT = 80;

class Customer
{
private:
long customerNumber;
char custName[NAME_SIZE];
char streetAddress_1[STREET_SIZE];


public:
bool setNum(long num);
bool setName(char namesize[]);
bool setAddress1(char address1[]);


long getNum() const;
char* getName();
char* getAdd1();
};


bool Customer::setNum(long num)
{
if (num > 99999 || num < 0) {
cout << "Invalid input. Re-enter ";
return false;
}
customerNumber = num;
return true;
}

bool Customer::setName(char namesize[MAXINPUT])
{
if (strlen(namesize) >= NAME_SIZE) {
cout << "Invalid input. Re-enter ";
return false;
}

strcpy_s(custName, namesize);
return true;
}

bool Customer::setAddress1(char address1[MAXINPUT])
{
if (strlen(address1) >= NAME_SIZE) {
cout << "Invalid input. Re-enter ";
return false;
}

strcpy_s(streetAddress_1, address1);
return true;
}



// Getter Methods

long Customer::getNum() const
{
return customerNumber;
}

char* Customer::getName()
{
return custName;
}

char* Customer::getAdd1()
{
return streetAddress_1;
}


int main()
{
Customer custom;

char dataLine[MAXINPUT];
long numLine;


cout << "Name: ";
cin.getline(dataLine, MAXINPUT);
custom.setName(dataLine);

cout << "Customer ID: ";
cin >> numLine;
custom.setNum(numLine);

cout << "Primary Address: ";
cin.getline(dataLine, MAXINPUT);
custom.setAddress1(dataLine);


cout << "Your Name is: " << custom.getName() << endl;
cout << "Your Customer ID is: " << custom.getNum() << endl;
cout << "Your Primary Address is: " << custom.getAdd1() << endl;

}

最佳答案

同一个字符数组可能被多次读取。只要不是动态分配就不需要清理:

#include <iostream>

int main()
{
char cstr[50];
while(std::cin.getline(cstr, 50))
{
std::cout << "I read \"" << cstr << "\"" << std::endl;
}

return 0;
}

如果数组是动态分配的(如果字符串大到足以导致堆栈溢出就应该是动态分配的),您将不得不删除[]一旦完成:

#include <iostream>

int main()
{
auto cstr = new char[50];
while(std::cin.getline(cstr, 50))
{
std::cout << "I read \"" << cstr << "\"" << std::endl;
}

delete[] cstr;
return 0;
}

但是这段代码很危险。您可能会忘记清理或中间某处的异常可能会导致内存泄漏。您应该考虑 std::string 以获得最大的异常安全性,并且它也更易于使用。 std::string 通常保存一个指向堆上动态分配内存的指针,并且它还负责在字符串超出范围时清理它。这意味着您不必担心管理内存/堆栈溢出等问题。这是一个例子:

#include <iostream>
#include <string>

int main()
{
auto line = std::string(); // guaranteed to make no copies with C++17
while(std::getline(std::cin, line))
{
std::cout << "I read \"" << line << "\"" << std::endl;
}

// No need to clean up manually!
return 0;
}

关于c++ - 将字符数组用作多种用途的通用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com