Question

SO I'm trying to get this particular program to open a file, put those elements into a struct and then output one of the variables (to see if its working). Unfortunately I can't even start the program because my void main is telling me that it has changed to an int and then says main must return int. I am a C++ novice and as such do not realize that it could be a simple mistake with the main. However, the struct I am not to sure if it is working correctly for the strings. Sample text in the file:

surname bloodtype organ age year(admited) Casby A heart 35 2012 Jorde B kidney 20 2009 etc....

I would be very grateful for any help towards this program as this will allow me to do the rest of the actual program (comparing two of the variables to be ==/displaying the lowest year...

#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();

}
Was it helpful?

Solution

Your first of many problems lies here:

void main ()

Main must return int. Some compilers may let you get away with void, but that is non-standard.

int main() { ... }

Or

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

are the 2 standard signatures for main.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top