Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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++ C++;,为什么数组的元素会自行改变?_C++ - Fatal编程技术网

C++ C++;,为什么数组的元素会自行改变?

C++ C++;,为什么数组的元素会自行改变?,c++,C++,随机排列号码。 例如,范围是4 这是我的代码输出 Enter total number: 4 Number Guessing 3 1 4 2 Enter 4 digits(1-4) separated by a space ---------------------------------------- Round 1 Enter Guess: 3 1 4 2 O O O O ----------------------------------------

随机排列号码。 例如,范围是4 这是我的代码输出

Enter total number: 4
Number Guessing
3 1 4 2 Enter 4 digits(1-4) separated by a space
----------------------------------------
Round 1
Enter Guess:    3 1 4 2
                O O O O 
----------------------------------------
Congratulation! You win in 1 steps
RUN FINISHED; exit value 0; real time: 5s; user: 0ms; system: 0ms
是的,没关系。但是如果你改变它超过4。它会有问题的

Enter total number: 5
Number Guessing
3 1 4 5 2 Enter 5 digits(1-5) separated by a space
----------------------------------------
Round 1
Enter Guess:    3 1 4 5 2
                O O O O X 
----------------------------------------
3 1 4 5 1330597711 Enter 5 digits(1-5) separated by a space
----------------------------------------
Round 2
Enter Guess:
第五个元素从2变为1330597711???我是初学者

这是我的代码!!! 你能帮我解决这个问题吗?我想知道

#include <iostream>
#include<ctime>
using namespace std;
int main() {
    void shuffle(int* const arr, int size);
    int iSize,iCount=1;
    int* iCode = new int[iSize];
    char* iGuess= new char[iSize];
    cout<<"Enter total number: ";
    cin>>iSize;

    for(int i=0;i<iSize;i++){
        iCode[i]=i+1;
        iGuess[i]='O';
    }

    shuffle(iCode,iSize);
    cout<<"Number Guessing"<<endl;


    while(iSize>=4&&iSize<=20){
        for(int i=0;i<iSize;i++){
                cout<<iCode[i]<<" ";
            }   //print the code
        int n=1;
        for(int i=0;i<iSize;i++){
            iGuess[i]='O';
        }   //initialize char array;
        cout<<"Enter "<<iSize<<" digits(1-"<<iSize<<") separated by a space"<<endl;
        cout<<"----------------------------------------"<<endl;
        cout<<"Round "<<iCount<<endl;
        cout<<"Enter Guess:    ";
        for(int i=0;i<iSize;i++){
            int temp;
            cin>>temp;
            if(temp!=iCode[i]){
                iGuess[i]='X'; n=0;}
        }
        cout<<"                ";
        for(int i=0;i<iSize;i++){
        cout<<iGuess[i];cout<<" ";}
        cout<<endl;
        cout<<"----------------------------------------"<<endl;
        if(n==1){
            cout<<"Congratulation! You win in "<<iCount<<" steps";
            break;
        }
        iCount+=1;
        }
        delete[] iCode,iGuess;
}
void shuffle(int* const arr, int size){
    srand((unsigned)time(NULL));

    for(int i=0;i<(size-1);i++){
        int r=i+(rand()%(size-i));
        int temp=arr[i];
        arr[i]=arr[r];
        arr[r]=temp;
    }
}
#包括
#包括
使用名称空间std;
int main(){
无效洗牌(整数*常量arr,整数大小);
int iSize,iCount=1;
int*iCode=新的int[iSize];
char*igess=新字符[iSize];
时装化;

对于(inti=0;i这段代码有错误,语句正常,但顺序错误

int iSize,iCount=1;
int* iCode = new int[iSize];
char* iGuess= new char[iSize];
cout<<"Enter total number: ";
cin>>iSize;
在程序中,事情是按照你说的顺序发生的。在使用变量之前,你应该确保变量有一个值

这也是错误的

delete[] iCode,iGuess;
应该是

int iSize,iCount=1;
cout<<"Enter total number: ";
cin>>iSize;
int* iCode = new int[iSize];
char* iGuess= new char[iSize];
delete[] iCode;
delete[] iGuess;

这段代码有错误,语句正常,但顺序错误

int iSize,iCount=1;
int* iCode = new int[iSize];
char* iGuess= new char[iSize];
cout<<"Enter total number: ";
cin>>iSize;
在程序中,事情是按照你说的顺序发生的。在使用变量之前,你应该确保变量有一个值

这也是错误的

delete[] iCode,iGuess;
应该是

int iSize,iCount=1;
cout<<"Enter total number: ";
cin>>iSize;
int* iCode = new int[iSize];
char* iGuess= new char[iSize];
delete[] iCode;
delete[] iGuess;

忘记
new
delete
,使用
std::vector
。忘记
new
delete
,使用
std::vector