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' ... –