【发布时间】:2018-02-14 20:21:53
【问题描述】:
在我的 CRA_Account.cpp 文件中,我的 socialInsuranceNumber 在
之后发生了变化(j = 0; j <= max_name_length; j++) {
given_name[j] = givenName[j];
}
发生。 例如:如果我输入 123123123 作为我的 SIN,它将更改为 123123148。之前的 for 循环具有
for (j = 0; j <= max_name_length; j++) {
family_name[j] = familyName[j];
}
不会改变我的 socialInsuranceNumber 值。
我的代码是:
w3_in_lab.cpp
#include <iostream>
#include "CRA_Account.h"
using namespace std;
using namespace sict;
int main() {
const int size = 1000;
char familyName[size];
char givenName[size];
sict::CRA_Account myCRA;
int sin;
bool quit = false;
if (sict::max_name_length + 1 > size) {
cerr << "Increase size to value greater than "
<< sict::max_name_length + 1 << endl;
return 1;
}
cout << "Canada Revenue Agency Account App" << endl
<< "=================================" << endl << endl;
cout << "Please enter your family name: ";
cin >> familyName;
cin.ignore();
cout << "Please enter your given name: ";
cin >> givenName;
cin.ignore();
do {
cout << "Please enter your social insurance number (0 to quit): ";
cin >> sin;
cin.ignore();
if (sin != 0)
{
myCRA.set(familyName, givenName, sin);
if (myCRA.isEmpty()) {
cout << "Invalid input! Try again." << endl;
}
else {
quit = true;
}
}
else {
quit = true;
}
} while (!quit);
cout << "----------------------------------------" << endl;
cout << "Testing the display function" << endl;
cout << "----------------------------------------" << endl;
myCRA.display();
cout << "----------------------------------------" << endl;
return 0;
}
CRA_Account.cpp
#include <iostream>
#include "CRA_Account.h"
#include <cstring>
using namespace std;
using namespace sict;
void CRA_Account::set(const char* familyName, const char* givenName, int sin) {
int j;
if (sin >= min_sin && sin <= max_sin) {
socialInsuranceNumber = sin;
for (j = 0; j <= max_name_length; j++) {
family_name[j] = familyName[j];
}
for (j = 0; j <= max_name_length; j++) {
given_name[j] = givenName[j];
}
}
else {
socialInsuranceNumber = 0;
}
}
bool CRA_Account::isEmpty() const {
bool empty = false;
if (socialInsuranceNumber <= 0) {
empty = true;
}
return empty;
}
void CRA_Account::display() const {
if (isEmpty()) {
cout << "Account object is empty!" << endl;
}
else {
cout << "Family Name: " << family_name << endl;
cout << "Given Name: " << given_name << endl;
cout << "CRA Account: " << socialInsuranceNumber << endl;
}
}
CRA_Account.h
#ifndef CRA_ACCOUNT_H
#define CRA_ACCOUNT_H
namespace sict {
static int max_name_length = 40;
static int min_sin = 100000000;
static int max_sin = 999999999;
class CRA_Account {
public:
void set(const char* familyName, const char* givenName, int sin);
bool isEmpty() const;
void display() const;
private:
char family_name[40];
char given_name[40];
int socialInsuranceNumber = 0;
};
}
#endif
【问题讨论】:
-
使用字符串而不是字符数组。请提供minimal reproducible example。
-
这看起来有问题
j <= max_name_length -
你应该使用
std::string而不是char array,如果你需要使用数组,那么使用strncpy()而不是循环。 -
Debug 和 minimze to relevant code 在发布问题之前请!
-
或者像“in Orbit”这样的名字
标签: c++ arrays loops for-loop int