学校安全教育宣传栏:C++函数调用问题(基础)

来源:百度文库 编辑:中科新闻网 时间:2024/04/29 05:56:01
各位高手,本人初学C++,有一个关于函数调用的问题,想请教各位。
我在menu()函数中直接调用input()函数,input()中的cin.get()就有用,可是从menu()中用if判断后调用input()函数,input()中的cin.get()就失效了,为什么啊,高手请指点!谢谢啦!
以下是源代码:
#include<iostream.h>
char a[50],*p=a;
int judge()
{
if(*(p)!='\0') return 1;
else return 0;
}
void input()
{
cout<<"\nPlease input a string:\n";
cout<<"\t";
cin.get();
}
void print()
{
int j;
j=judge();
if(j==0)
{
cout<<"\nError!No data in menory!\n";
return;
}
cout<<"\nThe string which you have inputted is:\n";
cout<<"\t"<<p<<endl;
}
void calculate()
{
int j;
j=judge();
if(j==0)
{
cout<<"\nError!No data in menory!\n";
return;
}
int l=0;
while(*(p+l)!='\0')
l++;
cout<<"\nThe string's length is:\n";
cout<<"\t"<<l<<endl;
}
void change()
{
int j;
j=judge();
if(j==0)
{
cout<<"\nError!No data in menory!\n";
return;
}
int n;
char x;
cout<<"\nPlease input the element's number which you want to change:\n";
cout<<">>>>>";
cin>>n;
cout<<"\nPlease input the element's value:\n";
cout<<">>>>>";
cin>>x;
*(p+n-1)=x;
}
void print_reverse()
{
int j;
j=judge();
if(j==0)
{
cout<<"\nError!No data in menory!\n";
return;
}
int l=0,i;
while(*(p+l)!='\0')
l++;
cout<<"\t";
for(i=l;i>=0;i--)
cout<<*(p+i);
cout<<endl;
}
int menu()
{
int x=0;
cout<<"\nPlease choose a function:";
cout<<"\n\t1. Input an array";
cout<<"\n\t2. Print the array";
cout<<"\n\t3. Calculate the array's length";
cout<<"\n\t4. Change one element in the array";
cout<<"\n\t5. Print the array in reverse order";
cout<<"\n\t6. Exit programme\n";
cout<<">>>>>";
cin>>x;
if(x==1) input();
else if(x==2) print();
else if(x==3) calculate();
else if(x==4) change();
else if(x==5) print_reverse();
else if(x==6) return 6;
else return 0;
return x;
}
void main()
{
int a=0;
cout<<"\n\tA Piont of char Show Programme\n";
while(a!=6)
{
do
{
a=menu();
}while(a==0);
}
}
可是我们书上都是写的#include<iostream.h>啊,为什么是#include<iostream>啊?

//头文件一定要改成这样(这是ANSI/ISO标准):(有了using name space,有几个头文件是不要.h后缀的,但有的需要:))
//---------------------------
#include<iostream>
#include<conio.h>
using namespace std;
//----------------------------
cin.get()改成
getch();
//另外发现你的input里没有cin之类的东东,
//getch()只能暂停一下