Needing help on best way to use 1 char array for mulitple user inputs, passing the user input data to a class setMethod(), then freeing up or reusing the same char array variable for the next user input statement, rinse and repeat. I know for pointers you can delete the memory address associated with them freeing up space, so I was thinking that would be my best bet?
// 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;
}