2013-10-09 155 views
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(); 

} 
+0

將'main'更改爲'int'並返回一個'int' ... –

回答

0

你的第一個的許多問題就出在這裏:

void main() 

主要必須返回int。有些編譯器可能會讓您脫離void,但這是非標準的。

int main() { ... } 

或者

int main(int argc, char** argv) { ... } 

main 2個標準簽名。

相關問題