问题
问答题
请编写一个函数display(),该函数要求用户先输入一字符串,然后在屏幕上再输出该字符串(假设该字符串长度小于 100)。注意:部分源程序已存在文件test35_2.cpp中,
请勿修改主函数main和其他函数中的任何内容,仅在函数display()的花括号中填写若干语句。
如输入abc,输出结果如下:
please input string:
abe
abc
Press any key to continue
文件 test35_2.cpp 的内容如下:
#include <iostream.h>
#include <conio.h>
void display()
void main( )
cout<<"please input string:" << end1;
display ( );
答案
参考答案:
void display()
{
char str[100] ch;
int i=0;
while ((ch=getche()) !=’\r’)
{
str[i]=ch;
i++;
}
str[i]=’\0’;
cout<<end1<<str<<end1;
}
解析: 本题考查的是考生对于基本的数据输入输出和字符数组的掌握。C++以空字符(’\0’)作为一个字符串的结束标志,另外本题中未涉及的常用字符串函数也是需要掌握的。