Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Int值在for循环后自行更改_C++_Arrays_Loops_For Loop_Int - Fatal编程技术网

C++ Int值在for循环后自行更改

C++ Int值在for循环后自行更改,c++,arrays,loops,for-loop,int,C++,Arrays,Loops,For Loop,Int,在我的CRA_Account.cpp文件中,我的socialInsuranceNumber在for之后更改 (j = 0; j <= max_name_length; j++) { given_name[j] = givenName[j]; } 在C++和C数组中索引为0,这意味着索引在[0,n-1 ]范围内是有效的。要迭代的常见模式是: for( size_t i = 0; i < array_size; ++i ) 所以

在我的CRA_Account.cpp文件中,我的socialInsuranceNumber在for之后更改

(j = 0; j <= max_name_length; j++) {
                given_name[j] = givenName[j];
            }

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

for( size_t i = 0; i < array_size; ++i )
所以你的循环:

for (j = 0; j <= max_name_length; j++) {
错误,因为它在[0,n]范围内进行迭代,并通过使用错误索引访问数组而导致UB。在本例中,您复制socialInsuranceNumber所在的部分内存。所以,修复您的循环,问题就会消失


注意:您应该使用std::string而不是char数组,这不仅可以简化代码,而且还可以减少出错的可能性。如果需要使用字符数组,则应使用标准C库中的strncpy函数,而不是手动循环。

使用字符串而不是字符数组。请提供一个。这看起来有点不确定。您应该使用std::string而不是char数组,如果需要使用数组,则使用strncpy而不是循环。在发布问题之前,请!或者像Orbit这样的名字,我们真的要一次又一次地回答简单的打字错误以及扩展的概念问题吗?如果你找到了dupe,我会删除我的答案,我发誓这可能太基本了,找不到比这更好的dupe了。。。但是关于我最初的评论:你妨碍了roomba,我真的不想进一步讨论。@Andrew Sure strncpy需要另一个参数来指定目标缓冲区的最大大小。
#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;

        }
    }
#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
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++) {