0
因此,我试图让这个特定的程序打开一个文件,将这些元素放入一个结构体中,然后输出其中一个变量(以查看它的工作情况)。不幸的是,我甚至无法启动程序,因为我的void main告诉我它已经改变为int,然后说main必须返回int。我是一名C++新手,因此没有意识到它可能是主要的一个简单的错误。但是,结构我不确定它是否正确地为字符串工作。文件中的示例文本:从一个文件中获取数据并将其放入一个结构中
姓血型的器官年龄一年(承认当年) Casby一个心脏35 2012 Jorde乙肾20 2009 等....
我将是朝着这个任何帮助,非常感谢计划,因为这将让我做实际的程序(比较两个变量的其余部分是== /显示最低的一年...
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <iomanip.h>
using namespace std;
ifstream patientin;
struct Patient {
string surname;
char Btype;
string organ;
int age, year;
};
void open(){
patientin.open("patient.txt");
if (patientin == NULL){
cout <<"\nCan't open the file. Restart." << endl;
exit(1);
}
}
void close(){
patientin.close();
}
void getFileInfo(){
const int Max = 4;
int i = 0;
Patient records[Max];
while (i <= Max){
patientin >> records[i].surname;
patientin >> records[i].Btype;
patientin >> records[i].organ;
patientin >> records[i].age;
patientin >> records[i].year;
}
cout << records[0].surname << endl;
}
void main(){
open();
getFileInfo();
close();
}
将'main'更改为'int'并返回一个'int' ... –