在for循环之后,Int值会自行更改

时间:2018-02-14 20:21:54

标签: c++ arrays loops for-loop int

在我的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

1 个答案:

答案 0 :(得分:0)

在C ++和C数组中,索引是基于0的,这意味着索引在[0,n-1]范围内有效。迭代的常见模式是:

for( size_t i = 0; i < array_size; ++i )
有些人甚至更喜欢写:

for( size_t i = 0; i != array_size; ++i )

所以你的循环:

for (j = 0; j <= max_name_length; j++) {

错误,因为它遍历范围[0,n](包括)并通过访问具有错误索引的数组导致UB。在您的情况下,您复制socialInsuranceNumber所在的内存部分。所以修复你的循环,问题应该消失。

注意:您应该使用std::string而不是char数组,这不仅可以简化代码,还可以减少错误。如果您需要使用char数组,那么您应该使用标准C库中的strncpy()函数,而不是手动循环。