麻城女孩交友:帮我看看这个C++代码为什么会一直循环.我明明写入了"while(z!=3)才循环也就是说等于3就中止循环"..

来源:百度文库 编辑:高考问答 时间:2024/05/04 12:44:21
//file name:d.cpp
#include <iostream>
using namespace std;
void x();
int y();
int main()//声明函数.
{
int z=0;
while(z!=3)
{
x();
int z;//定义变量.
z=y();
switch(z)
{
case 1:
cout<<"processing receivable"<<endl;
break;
case 2:
cout<<"processing payables"<<endl;
break;
case 3:
cout<<"quitting"<<endl;
break;
default:
cout<<"\alnvalid selection"<<endl;
}
}
return 0;
}
void x()
{
cout<<"---menu---"<<endl;
cout<<"1=receivables"<<endl;
cout<<"2=payables"<<endl;
cout<<"3=ouit"<<endl;
}
int y()
{
int a;
cout<<"enter selection:";
cin>>a;
return a;
}

int z=0;
while(z!=3)
{
x();
int z;//定义变量.

在循环体内外多定义了一个z变量
导致循环体内部的z成了局部变量