2013-10-09 158 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个标准签名。

相关问题