C++ 数组索引问题

C++ 数组索引问题,c++,C++,我认为下面的代码存在数组索引问题,它在Orwell Dev CPP中编译得很好,但当我运行它时,我会看到输出的上下箭头,我认为这表明其中一个条件语句存在某种问题。我期待所有的数字输出 #include <iostream> #include <stdio.h> #include <string.h> using namespace std ; /* run this program using the console pauser or add your

我认为下面的代码存在数组索引问题,它在Orwell Dev CPP中编译得很好,但当我运行它时,我会看到输出的上下箭头,我认为这表明其中一个条件语句存在某种问题。我期待所有的数字输出

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std ;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

   char name[2] ;

   cout << "Type aa ab ba or bb" << endl ;

   fgets(name , 2, stdin) ;

   cout << "The first letter of the name is " << name[0] << endl ;
   cout << "The second letter of the name is " << name[1] << endl ;

   int name_length ;

   name_length = strlen(name) -1 ;

   cout << "The length of the name is " << name_length << endl;

   char Kaballah_Chaldean_Main[2][2] ;

   /* Set the Main Chaldean Kaballah */

   Kaballah_Chaldean_Main[1][1] = 'A' ;
   Kaballah_Chaldean_Main[2][1] = 'B' ;

   Kaballah_Chaldean_Main[1][2] = 1 ;
   Kaballah_Chaldean_Main[2][2] = 2 ;

   unsigned int x = 0 ;
   unsigned int Chaldean_Letter_Index ;

   cout << "Name_Lenght is " << name_length  << "Chaldean_Letter_Index "<< Chaldean_Letter_Index << "x " << x << endl ;

   for ( x = 0 ; name_length  >= x; x = x + 1 ) {

      cout << "x " << x << "Name Length is " << name_length << endl ;

      for (Chaldean_Letter_Index = 1; Chaldean_Letter_Index <= 2 ; Chaldean_Letter_Index = Chaldean_Letter_Index+ 1) {

         cout << "Chaldean Letter Index" << Chaldean_Letter_Index << endl ;

         cout << "x " << x  <<"Name letter " << name[x] << endl ;
      }
   }
   return 0;
}
#包括
#包括
#包括
使用名称空间std;
/*使用控制台暂停器运行此程序,或添加您自己的getch、system(“暂停”)或输入循环*/
int main(int argc,字符**argv){
字符名[2];

coutarr[2][2]数组将具有

arr[0][0] arr[0][1]
arr[1][0] arr[1][1]
以下内容超出索引范围

Kaballah_Chaldean_Main[2][1] = 'B' ;
 Kaballah_Chaldean_Main[1][2] = 1 ;
 Kaballah_Chaldean_Main[2][2] = 2 ;

你的数组索引大部分都是错误的。你的下标在Kask> KabalaHAL CaldAdAn Mux/Cudit中是基于1的,但是C++使用的是基于0的数组。你应该使用<代码> KabalaHaCaldAdAn Maul[0 ] [0 ] < /Cuff>等,以避免超出数组的边界。< /P>


此外,Chalden字母索引应以0开始,以1结束。

问题1

fgets(name , 2, stdin);
最多读取一个字符到
名称
。有关详细信息,请参阅

如果要读取2个字符,请使用:

char name[3] ;

cout << "Type aa ab ba or bb" << endl ;

fgets(name , 3, stdin) ;
更合理的做法是:

name_length = strlen(name);
问题3

你有:

char Kaballah_Chaldean_Main[2][2] ;

/* Set the Main Chaldean Kaballah */

Kaballah_Chaldean_Main[1][1] = 'A' ; 
Kaballah_Chaldean_Main[2][1] = 'B' ;

Kaballah_Chaldean_Main[1][2] = 1 ;
Kaballah_Chaldean_Main[2][2] = 2 ;
这些是错误的索引。您正在修改超出有效限制的内存。
Kaballah\u Chaldean\u Main
的有效索引范围是
[0][0]
-
[1][1]
。这些行应该是:

Kaballah_Chaldean_Main[0][0] = 'A' ; 
Kaballah_Chaldean_Main[1][0] = 'B' ;

Kaballah_Chaldean_Main[0][1] = 1 ;
Kaballah_Chaldean_Main[1][1] = 2 ;
问题4

您有以下变量:

unsigned int Chaldean_Letter_Index ;
它在下一行中使用之前未初始化。可能应该将其初始化为0

unsigned int Chaldean_Letter_Index = 0;
unsigned int Chaldean_Letter_Index = 0;