问题
问答题
运行程序,从键盘上输入“This is a C++prog!”后输出到一个文件中,并显示在屏幕上,显示结果:
This is a C++prog!。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
char str[20];
ofstream outf("D:\tem.dat",ios::trunc);
cin>>str;
outf<<str;
outf.close();
int i=0;
while(str[i]!=’\0’)
cout<<str[i];
i++;
cout<<endl;
答案
参考答案:cin>>str;使用cin进行输入时空格也是作为结束符,所以str数组不能接收完整的字符串。应改为cin.getline(str,20);。