Well, I need to write a program that uses enumerated datatypes, structs, and plenty of functions. Below is my current code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


void getInput(string input);
void outputStudent(string student);
void outputTeacher(string teacher);
void outputStaff(string staff);
void sendtoOutputStudents(string students);

struct personDataType
{
    enum personType{student=0, teacher=1, staff=2};
    string firstname, lastname, street, city, state;
    int housenumber, zipcode;
};

int main()
{

    return 0;
}

void getInput(string input)
{
    ifstream fin;

    fin.open("program6.txt");

    if(fin.fail())
    {
        cerr << "Unable to find input file, program closing\n";
        exit (2);
    }
}

void outputStudent(string student)
{
    getInput(input);
    ofstream fout;
    fout.open("student.txt");
    if(fout.fail())
    {
        cerr << "Unable to open outputfile, program closing\n";
        exit (3);
    }

    while(fin >> aNumber >> persontype.firstname >> persontype.lastname >> persontype.housenumber >> persontype.street >> persontype.city >> persontype.state >> persontype.zipcode)
    {
        if(aNumber==student)
        {
            sendtoOutputStudents(students)
            fout << setw(10) << persontype.firstname << setw(10) << persontype.lastname << setw(10) << persontype.housenumber << setw(10) << persontype.street << setw(10) << persontype.city << setw(10) << persontype.state << setw(10) << persontype.zipcode)
        }
    }
} 

void sendtoOutputStudents(string students)
{
    fout << "Student\n" << setw(10) << "First Name" << setw(10) << "Last Name" << setw(10) << "House Number" <<  setw(10) << "Street" << setw(10) << "City" <<  setw(10) << "State" <<  setw(10) << "Zipcode" << endl;
}

Obviously, I'm not calling the struct right. Also, the function "getInput" doesn't work, at all. What am I doing wrong here? This is an assignment so doing it any other way would cost points.

Ok, there are plenty of errors. This is not going even to compile.

I'd suggest you to rewrite it, adding a single function at a time.

I think it would be meaningless to point out all the errors at once, so let's just say for now that:

1) you don't create any instance of your struct personDataType, ever. I don't know how you thought to use it, but this is not the right way ^^"

2) main is empty, so how do you know that getInput doesn't work? Indeed you're right - that won't work - but as suggested before I recommend you to write your program step by step and test each functionality you add before passing to the next level.

3) in you input function, fin goes out of scope when exiting and thus you can't use it in outputStudent - and this is just an example

4) are the arguments of your functions provided by the assignment? I think there's no point in passing unused string all around the code

5) as a start, you could begin here:

int main(void) {
     personDataType myperson;
     // try to input - output myperson's data, at first hard coding them, then letting the user input them and finally taking them from a file
     return EXIT_SUCCESS;
}

after you have done this, you can go to the next step ;)

Post your attempts and we will give you more specific advices :)

Ok, there are plenty of errors. This is not going even to compile.

I'd suggest you to rewrite it, adding a single function at a time.

I think it would be meaningless to point out all the errors at once, so let's just say for now that:

1) you don't create any instance of your struct personDataType, ever. I don't know how you thought to use it, but this is not the right way ^^"

2) main is empty, so how do you know that getInput doesn't work? Indeed you're right - that won't work - but as suggested before I recommend you to write your program step by step and test each functionality you add before passing to the next level.

3) in you input function, fin goes out of scope when exiting and thus you can't use it in outputStudent - and this is just an example

4) are the arguments of your functions provided by the assignment? I think there's no point in passing unused string all around the code

5) as a start, you could begin here:

int main(void) {
     personDataType myperson;
     // try to input - output myperson's data, at first hard coding them, then letting the user input them and finally taking them from a file
     return EXIT_SUCCESS;
}

after you have done this, you can go to the next step ;)

Post your attempts and we will give you more specific advices :)

Well then, how do I use "enum"? In the assignment, he explicitly states: "In this program, you need to declare a person datatype. This will include a first name, last name, house number, street, city, state and zip code. It will also include an enumerated datatype. The enumerated datatype will consist of enumerations for teachers, students and staff."

If I'm using the struct wrong, then how do I use it?

This means, to me, that the struct "personDataType" HAS to use an "enum" in it. If not, then what the heck is he saying?

Placing a struct into "int main" just causes more errors than I can count. It crashed Visual Studio the first time I did it.

I know get input doesn't work because every time I try to compile the program, I get an error asking for "fin" to be a variable. WHY doesn't "getInput" work?

And I have been writing the program step-by-step. First the struct, then the functions. I haven't added anything to "int main()" yet because nothing works, so why add it when I know it won't work?

Because it's good strategy to make simpler things work before adding stuff.

If you write a chunk of code and it doesn't compile then it's nonsense writing more lines before you get the first compiling AND working! You will find yourself completely overwhelmed by the errors, to the point that it'll be easier to restart from scratch than to fix the errors.

The first thing to do is to create your struct and then keep on using it in a working program (that is: put an instance of that struct in main and play around with it) until you feel confident with the base handling of the struct.

I didn't mean to put personDataType myperson; in YOUR main, I was suggesting to restart and put in the source file only the struct definition and such a main. Then, you'd better experiment data handling of your struct before you even think of input file(s).

Here's a start:

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

enum personType { student = 0, teacher = 1, staff = 2 };

struct personDataType {
	personType mytype;
	string firstname, lastname, street, city, state;
	int housenumber, zipcode;
};

int main(void) {
	personDataType myperson;
	myperson.mytype = student;
	myperson.firstname = "Ellen";
	myperson.lastname = "Allien";
	myperson.housenumber = 4390;
	cout << endl << "My first person is " << myperson.firstname << " " << myperson.lastname 
	<< " and her house number is " << myperson.housenumber << endl;
	return EXIT_SUCCESS;
}

You can see how I assign values to some member variables of myperson and how I output them.
Try something like this, experiment and try to let the user input all the data about him/her and then print it out.

After you have done this, go to the next step and start adding functions, ONE AT A TIME, and test them before moving further :)

Comments
Your telling me something I just admitted to doing, writing the damn thing one step at a time. It just so happens, I reached "getInput", and THATS where it stopped working. Everything else prior to that works fine. I went over this, several times.

Okay... apparently I'm not making sense when writing my reply's. So I'll do this. I've written a code here:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void outputStudents(string students);
void outputTeachers(string teachers);
void outputStaff(string staff);

int main()
{
    string students, teachers, staff;

outputStudents(students);
outputTeachers(teachers);
outputStaff(staff);

    return 0;
}

void outputStudents(string students)
{

    string firstname;
    string lastname;
    string street;
    string city;
    string state;
    int housenumber, zipcode, anumber;

    ifstream fin;
    ofstream fout;

    fin.open("program6.txt");
    fout.open("student.txt");

    if(fin.fail())
    {
        cerr << "Unable to open input file, this is outputstudents\n";
        exit(2);
    }

    if(fout.fail())
    {
        cerr << "Unable to open output file\n";
        exit(3);
    }
    
    cout << "Your up to the while loop at students\n";

    while (fin >> anumber >> firstname >> lastname >> housenumber >> street >> city >> state >> zipcode)
    {
        if (anumber==0)
        {
            fout << firstname << setw(10) << lastname << setw(10) << housenumber << setw(10) << street << setw(10) << city << setw(10) << state << setw(10) << zipcode << endl;
        }

    }

    fin.close();
    fout.close();
    return;
}

void outputTeachers(string teachers)
{

    string firstname;
    string lastname;
    string street;
    string city;
    string state;
    int housenumber, zipcode, anumber;

    ifstream fin;
    ofstream fout;

    fin.open("program6.txt");
    fout.open("teacher.txt");

    if(fin.fail())
    {
        cerr << "Unable to open input file\n";
        exit(2);
    }

    if(fout.fail())
    {
        cerr << "Unable to open output file\n";
        exit(3);
    }

    cout << "Your up to the while loop at teacers\n";

    while(fin >> anumber >> firstname >> lastname >> housenumber >> street >> city >> state >> zipcode)
     {
        if (anumber==1)
        {
            fout << firstname << setw(10) << lastname << setw(10) << housenumber << setw(10) << street << setw(10) << city << setw(10) << state << setw(10) << zipcode << endl;
        }
    }
    cout << "The while loop at teachers is working\n";

    fin.close();
    fout.close();
    return;
}

void outputStaff(string staff)
{

    string firstname;
    string lastname;
    string street;
    string city;
    string state;
    int housenumber, zipcode, anumber;

    ifstream fin;
    ofstream fout;

    fin.open("program6.txt");
    fout.open("staff.txt");

    if(fin.fail())
    {
        cerr << "Unable to open input file\n";
        exit(2);
    }

    if(fout.fail())
    {
        cerr << "Unable to open output file\n";
        exit(3);
    }

    cout << "The while loop at staff starts\n";

    while(fin >> anumber >> firstname >> lastname >> housenumber >> street >> city >> state >> zipcode)
    {
        if (anumber==2)
        {
            fout << firstname << setw(10) << lastname << setw(10) << housenumber << setw(10) << street << setw(10) << city << setw(10) << state << setw(10) << zipcode << endl;
        }
 
    }

    fin.close();
    fout.close();
    return;
}

THIS CODE WORKS.

As you can see, the above code works. It was written, step-by-step. I know that in order to make a working program, you write it, step-by-step. Writing it "step-by-step" isn't the problem. If it was, I wouldn't have said I had written it, "step-by-step", in my last post.

Now, what was wrong with the LAST code, the one I wrote in the first post, was that "getInput", was not working. That, and where to put "enum", was the problem. My writing a code, step-by-step, WASN'T the problem. I never said it was. Nor has it been in my writing.

Now, what I need to do, is change the working code you see in this post, so that it uses "enum", a struct (the one I've written in the first post), and a "function" (that is, a code OUTSIDE of "int main") that can be called into another function.

Telling me to "test it after each line", is getting redundant. I know how to debug, I don't know how to write a function that will open an input stream, and then call said function into another function. Nor did I know how to use an "enum", in fact I'm still hazy on it. How, HOW, do you use an when calling data from an input file? Note, I'm using the enum and the struct with an input file, to send data to an output file.

If were still stuck on the "well you need to write it step-by-step" phase, then I guess I'm just not typing in proper English and should wing this thing.

void getInput(string input)
{
    ifstream fin;

    fin.open("program6.txt");

    if(fin.fail())
    {
        cerr << "Unable to find input file, program closing\n";
        exit (2);
    }
}

fin dies where the function ends. In your outputStudent function you call getInput(input); and after you use fin inside outputStudent, that does not know fin (that's why it asks you to declare it). If you have to write a function for the input, then put all the input routine inside that function!

Also, what's "input"? a string? You pass to all your function useless arguments (that you haven't even declared anywhere). What are you trying to do?

And then again, there's no point in creating a struct if you don't use it to store data. Instead of putting data in persontype.somestuff, that does not make sense, you should create an instance of your struct, or pass it as an argument, and input all the data in that specific instance's member variables, like I was trying to show you.

This article has been dead for over six months. Start a new discussion instead.