赤壁外传1.9.8:Vc++疑问:'temp' : undeclared identifier

来源:百度文库 编辑:中科新闻网 时间:2024/04/28 00:11:19
我的程序:
#include<iostream.h>
const int ARRAYMAX = 5;
void main()
{
int a[ARRAYMAX] = {20, 40, -50, 7, 13};
for(int i=0;i<ARRAYMAX;i++){
for(int j=i+1;j<ARRAYMAX;j++){
if(a[i]<a[j])
int temp = a[j]; a[j] = a[i]; a[i] = temp;
}
}
cout<<a[i]<<" ";
}
}
出现的错误:
D:\study\C++程序\12\Ex_ArraySort\array.cpp(9) : error C2065: 'temp' : undeclared identifier

你的if后面少了一个{,

看我修改后的程序:
#include<iostream>
using namespace std;
const int ARRAYMAX = 5;
void main()
{
int a[ARRAYMAX] = {20, 40, -50, 7, 13};
for(int i=0;i<ARRAYMAX;i++)
{
for(int j=i+1;j<ARRAYMAX;j++)
{
if(a[i]<a[j])
{
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
cout<<a[i]<<" ";
}
}

少了大括号。
#include<iostream.h>
const int ARRAYMAX = 5;
void main()
{
int a[ARRAYMAX] = {20, 40, -50, 7, 13};
//int temp;
for(int i=0;i<ARRAYMAX;i++)
{
for(int j=i+1;j<ARRAYMAX;j++)
{
if(a[i]<a[j])
{ int temp = a[j];
a[j] = a[i];
a[i] = temp; }
}
}
cout<<a[i]<<" ";

}