![]() |
#2
ytiantian2013-05-21 20:34
|

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
int main(){
struct Student{
string Name;
string stuNum;
string stuObj;
};
string str1;
string str2;
string str3;
Student *p1 = new Student;
p1.Name = "姓名:" ; //报错 用Student.Name也试过
p1.stuNum = "学号: " ; //报错
p1.stuObj = "科目: " ; //报错
cout << "请输入您的姓名:" <<endl;
cin >>str1;
cout << "请输入您的学号:" <<endl;
cin >>str2;
cout << "请输入您的科目:" <<endl;
cin >>str3;
Student.Name = Student.Name + str1;
Student.stuNum = Student.stuNum + str2;
Student.stuObj = Student.stuObj + str3;
ofstream outfile("Myworld.txt",ios::binary);
outfile.write(str1.c_str(),100);
if(outfile.good())
{
cout << "打开文件成功!"
<<endl;
}
outfile.close();
system("pause");
}
这个结构体保存学员的姓名、学号、和科目,暂时只要这些。先给结构体的Name赋值“姓名:”是为了跟输入的str1方便连接,最后要输出的文本的现实为“姓名:张三”之类,那个结构体已经分配空间 但还是没办法解决,刚学C++两个月、求一起进步的朋友 扣扣247804877 #include<string>
#include<fstream>
#include<vector>
using namespace std;
int main(){
struct Student{
string Name;
string stuNum;
string stuObj;
};
string str1;
string str2;
string str3;
Student *p1 = new Student;
p1.Name = "姓名:" ; //报错 用Student.Name也试过
p1.stuNum = "学号: " ; //报错
p1.stuObj = "科目: " ; //报错
cout << "请输入您的姓名:" <<endl;
cin >>str1;
cout << "请输入您的学号:" <<endl;
cin >>str2;
cout << "请输入您的科目:" <<endl;
cin >>str3;
Student.Name = Student.Name + str1;
Student.stuNum = Student.stuNum + str2;
Student.stuObj = Student.stuObj + str3;
ofstream outfile("Myworld.txt",ios::binary);
outfile.write(str1.c_str(),100);
if(outfile.good())
{
cout << "打开文件成功!"
<<endl;
}
outfile.close();
system("pause");
}