C++ 此代码中存在运行时错误
我在这个程序中有一个运行时错误,它没有语法错误,但在运行时崩溃。我使用的是C++ 此代码中存在运行时错误,c++,runtime,C++,Runtime,我在这个程序中有一个运行时错误,它没有语法错误,但在运行时崩溃。我使用的是dev-c++4.9.9.2。我试图找出错误,但找不到。如果有人能帮忙,请找出错误并纠正我 #include<iostream.h> void DisplayVUID(); void DisplayReverse(char[], int); void StoreDiagonal(); main() { DisplayVUID(); char a[20] = "mc123456789
dev-c++4.9.9.2
。我试图找出错误,但找不到。如果有人能帮忙,请找出错误并纠正我
#include<iostream.h>
void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();
main()
{
DisplayVUID();
char a[20] = "mc123456789";
DisplayReverse(a, 20 );
StoreDiagonal();
system("pause");
}
void DisplayVUID()
{
int i;
char name[20] = "mc123456789";
cout<<"My VU id is ";
for(i=0;i<20;i++)
{
cout<<name[i];
}
cout<<endl;
}
void DisplayReverse(char a[], int arraysize)
{
int i;
cout<<"MY VU id in Reverse is ";
for(i=arraysize-1;i>=0;i--)
{
cout<<a[i];
}
cout<<endl;
}
void StoreDiagonal()
{
int a[9][9] ;
int i;
int row, col;
for (i = 0; i<9;i++)
{
for(i=0;i<9;i++)
{
a[row][col] = 0;
}
}
a[1][1] = 1;
a[2][2] = 3;
a[3][3] = 0;
a[4][4] = 2;
a[5][5] = 0;
a[6][6] = 2;
a[7][7] = 3;
a[8][8] = 9;
a[9][9] = 8;
for(i = 0 ; i < 9 ; i ++)
{
for( i = 0 ; i < 9 ; i ++)
{
cout<<a[row][col];
}
}
}
#包括
void DisplayVUID();
void DisplayReverse(char[],int);
void()是对角的;
main()
{
DisplayVUID();
字符a[20]=“mc123456789”;
显示反向(a,20);
Store对角线();
系统(“暂停”);
}
void DisplayVUID()
{
int i;
字符名称[20]=“mc123456789”;
库特
删除这一行就可以了。数组索引从0开始,而不是从1开始
我还想指出,在函数DisplayVUID()中
删除这一行就可以了。数组索引从0开始,而不是从1开始
我还想指出,在函数DisplayVUID()中更改iStackoverflow上的情况不是这样的,从下次开始,努力自己做事情,做研究,然后来这里。您的程序中似乎有很多错误,但我尝试删除一些错误,最后,它在我的系统上工作。我还通过评论推荐了一些不错的东西,您可以在程序中查看内存:
编辑:我注意到一些由于数组中未分配空格而未定义的字符串在reverse函数中被打印出来,但我现在已经纠正了它
#include<iostream>
#include<stdlib.h>
using namespace std;// use namespace otherwise cout won't work
void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();
int main()// In C++ always declare a main function like this,its good habit
{
int i=0;
DisplayVUID();
char a[20] = "mc123456789";
while(a[i]!='\0')
i++;// Did this to ensure that cout will print only up to the null character,earlier it was printing some undefined characters along with the data in the array.
DisplayReverse(a, i );
StoreDiagonal();
system("pause");
return 0;//return 0
}
void DisplayVUID()
{
//int i;
char name[20] = "mc123456789";
cout<<"My VU id is ";
//for(i=0;i<20;i++)// no need for this loop at least here
//{
cout<<name;
//}
cout<<endl;
}
void DisplayReverse(char a[], int i)
{
cout<<"MY VU id in Reverse is ";
// for(i=arraysize-1;i>=0;i--)
//{
while(i--)
cout<<a[i];//instead of the for loop traversing for the whole arraysize i have made it go up to only the null terminator this way it doesn't print undefined characters.
//}
cout<<endl;
}
void StoreDiagonal()// i don't understand by the way what this function is here for , its not an error though.
{
int a[9][9] ;
int i,j,c=1;
//int row, col;// you didn't initialize row and column and placed it inside the loop
for (i = 0; i<9;i++)
{
for(j=0;j<9;j++)
{
a[i][j] = 0;
if(i==j)
{
a[i][j]=c;//instead of manually assigning do it like this.
c++;
}
}
}
for(i = 0 ; i < 9 ; i ++)
{
for( j = 0 ; j < 9 ; j ++)
{
cout<<a[i][j];// the elements of the array don't display like a table , this is not an error but to make your output readable do it by your self
}
}
}
#包括
#包括
使用名称空间std;//使用名称空间,否则将无法工作
void DisplayVUID();
void DisplayReverse(char[],int);
void()是对角的;
int()//C++总是声明这样的一个主要函数,它的好习惯
{
int i=0;
DisplayVUID();
字符a[20]=“mc123456789”;
而(a[i]!='\0')
i++;//这样做是为了确保cout最多只能打印空字符,之前它是在打印一些未定义的字符以及数组中的数据。
显示反向(a,i);
Store对角线();
系统(“暂停”);
返回0;//返回0
}
void DisplayVUID()
{
//int i;
字符名称[20]=“mc123456789”;
coutStackoverflow上的情况不是这样的,从下一次开始,努力自己做事情,做研究,然后来这里。你的程序中似乎有很多错误,但我尝试删除一些错误,最后,它在我的系统上工作。我还通过评论推荐了一些不错的东西,你可以在程序中查看:
编辑:我注意到一些由于数组中未分配空格而未定义的字符串在reverse函数中被打印出来,但我现在已经纠正了它
#include<iostream>
#include<stdlib.h>
using namespace std;// use namespace otherwise cout won't work
void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();
int main()// In C++ always declare a main function like this,its good habit
{
int i=0;
DisplayVUID();
char a[20] = "mc123456789";
while(a[i]!='\0')
i++;// Did this to ensure that cout will print only up to the null character,earlier it was printing some undefined characters along with the data in the array.
DisplayReverse(a, i );
StoreDiagonal();
system("pause");
return 0;//return 0
}
void DisplayVUID()
{
//int i;
char name[20] = "mc123456789";
cout<<"My VU id is ";
//for(i=0;i<20;i++)// no need for this loop at least here
//{
cout<<name;
//}
cout<<endl;
}
void DisplayReverse(char a[], int i)
{
cout<<"MY VU id in Reverse is ";
// for(i=arraysize-1;i>=0;i--)
//{
while(i--)
cout<<a[i];//instead of the for loop traversing for the whole arraysize i have made it go up to only the null terminator this way it doesn't print undefined characters.
//}
cout<<endl;
}
void StoreDiagonal()// i don't understand by the way what this function is here for , its not an error though.
{
int a[9][9] ;
int i,j,c=1;
//int row, col;// you didn't initialize row and column and placed it inside the loop
for (i = 0; i<9;i++)
{
for(j=0;j<9;j++)
{
a[i][j] = 0;
if(i==j)
{
a[i][j]=c;//instead of manually assigning do it like this.
c++;
}
}
}
for(i = 0 ; i < 9 ; i ++)
{
for( j = 0 ; j < 9 ; j ++)
{
cout<<a[i][j];// the elements of the array don't display like a table , this is not an error but to make your output readable do it by your self
}
}
}
#包括
#包括
使用名称空间std;//使用名称空间,否则将无法工作
void DisplayVUID();
void DisplayReverse(char[],int);
void()是对角的;
int()//C++总是声明这样的一个主要函数,它的好习惯
{
int i=0;
DisplayVUID();
字符a[20]=“mc123456789”;
而(a[i]!='\0')
i++;//这样做是为了确保cout最多只能打印空字符,之前它是在打印一些未定义的字符以及数组中的数据。
显示反向(a,i);
Store对角线();
系统(“暂停”);
返回0;//返回0
}
void DisplayVUID()
{
//int i;
字符名称[20]=“mc123456789”;
如果您没有使用名称空间标准,请在include语句之后尝试使用名称空间std;
。下次,请先使用调试器,然后给我们打电话。@sirez您没有讨论有关问题的事情,您也没有接受任何答案,如果我说我不希望您接受我的答案,但这是我们投入的时间,那我就是在撒谎回答一个问题,你应该注意讨论它是否有效:)@PHIfounder很抱歉,但当我看到这些评论时,他们非常沮丧,这里没有人愿意帮助,所以我留下了这个问题,直到你在这里发表评论,我才看一看。@PHIfounder:)我总是在来t之前在谷歌上找到东西他的网站,这就是为什么我的问题很少的原因。这是一个旧代码,是我朋友的。我犯了一个错误,我没有在t上工作,只是发布在这里。好吧,我会尽量不再这样做。我是这个网站的新手,几个月前我开始学习编程。所以需要别人的帮助。但是谢谢你的帮助,我会考虑的如果您没有使用名称空间标准,请在include语句之后尝试使用名称空间std;
。下次,请先使用调试器,然后给我们打电话。@Sireiz您没有讨论有关此问题的问题,也没有接受任何答案,如果我说我不希望您这样做,那我就是在撒谎接受我的回答,但这是我们回答问题的时间,你应该注意讨论它是否有效:)@PHIfounder抱歉,但当我看到这些评论时,它们非常令人沮丧,这里没有人愿意帮助,所以我留下了这个问题,直到你在这里发表评论,我才看。@PHIfounder:)在来到这个网站之前,我总是在谷歌上找到一些东西,这就是为什么我的问题很少的原因。这是一个旧代码,是我朋友的。我犯了一个错误,我没有在t上工作,只是发布在这里。我会尽量不再这样做。我是这个网站的新手,几个月前我开始学习编程。所以需要ot的帮助吗但是谢谢你的帮助,我以后会考虑,这里的事情不这样做。+ 1你的代码中的评论,我不支持这样回答(完整的代码)但是由于你添加的评论,这是一个非常有用的答案。坚持下去!@GrijeshChauhan非常感谢!!只需要像你这样的人的支持和指导。实际上,即使我不想写这样的答案,但我认为OP在这里太幼稚了,为什么不在代码本身的特定点上给他一个指导呢。Anywa是的,从现在起我会考虑的。再次谢谢你。:)+1谢谢你的来信