【问题标题】:Taking data from a file and putting it into a struct从文件中获取数据并将其放入结构中
【发布时间】:2013-10-09 01:49:54
【问题描述】:

所以我试图让这个特定的程序打开一个文件,将这些元素放入一个结构中,然后输出其中一个变量(看看它是否工作)。不幸的是,我什至无法启动程序,因为我的 void main 告诉我它已更改为 int,然后说 main 必须返回 int。我是 C++ 新手,因此没有意识到这可能是 main 的一个简单错误。但是,我不确定该结构是否适用于字符串。文件中的示例文本:

姓氏血型器官年龄年份(承认) 卡斯比一颗心 35 2012 Jorde B 肾 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...

标签: c++ data-structures file-io struct main


【解决方案1】:

您的许多问题中的第一个在这里:

void main ()

Main 必须返回 int。一些编译器可能会让你摆脱 void,但这是非标准的。

int main() { ... }

或者

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

main 的两个标准签名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2017-06-25
    • 2016-03-12
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多