fails to compile

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1309 Views - Last Post: 23 August 2009 - 07:12 AM Rate Topic: -----

#1 hellhound  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 07-March 09

fails to compile

Post icon  Posted 22 August 2009 - 04:47 PM

hi i have half written out this code but when i go to compile it it crashs on me. i have look but i have no idea whats causing it.

//McBeth, Samuel, 07056141, Assignment 2 159.234

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string comp = "-1";

class student {
	friend class course;
	private: 
		struct students {
			string firstname;
			string lastname;
			string ID;
			char fees;
		};
		students list [2000];
	
	public:
		student() {};
		~student() {};
		void readstudents();
		
};

void student::readstudents() {
	ifstream studentfile ("students.txt");
	if(!studentfile) {
		cout << "unable to open students.txt" << endl;
		exit(1);
	}
	float num;
	int i=0;
	string temp;
	studentfile >> num;
	if(num > 2000) {
		cout << "to many students. exiting" << endl;
		exit(0);
	}
	getline (studentfile, temp);
	for(i=0; i<num; i++) {
		getline(studentfile, list[i].firstname);
		getline(studentfile, list[i].lastname);
		getline(studentfile, list[i].ID);
		studentfile >> list[i].fees;
		getline(studentfile, temp);
	}
	i++;
	list[i].ID=comp;
	studentfile.close();
}

class course{
	private:
		struct studentlist {
			string ID;
		};
		struct courses {
			string courseID;
			string title;
			int credits;
			studentlist students[2000];
		};
		int num;
		courses clist[200];
		
	public:
		course() {};
		~course() {};
		void readCourses();
		void notEnrolled();
		void noStudents();
		void ghostStudents();
};

void course::readCourses() {
	string temp;
	ifstream coursefile ("courses.txt");
	if(!coursefile) {
		cout << "unable to open course.txt" << endl;
		exit(1);
	}
	courses >> num;
	if(num > 200) {
		cout << "to many courses. Exiting" << endl;
		exit(0);
	}
	getline (coursefile,temp);
	for(int i=0; i<num; i++) {
		getline(coursefile,clist[i].courseID);
		getline(coursefile,clist[i].title);
		coursefile >> clist[i].credits;
		getline(coursefile,temp);
		int j=0;
		while(true) {
			getline(coursefile,clist[i].students[j].ID;
			if(clist[i].students[j].ID.compare(comp) == 0) {
				break;
			}
			j++;
		}
	}
	coursefile.close();
}

//void course::notEnrolled () {
//	int i=0;
//	int k=0;
//	int j;
//	while(list[i].ID.compare(comp) != 0) {
//		for(j=0; j<num; j++) {
//			k=0;
//			while(list



Is This A Good Question/Topic? 0
  • +

Replies To: fails to compile

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: fails to compile

Posted 22 August 2009 - 04:51 PM

You can't crash on compile. You can fail to compile, due to syntax errors. If it compiles, but then fails during run-time, then you say the program crashed. Which is it?

Also, why does the student class have a struct called students? That just seems like bad design.

This post has been edited by KYA: 22 August 2009 - 04:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: fails to compile

Posted 22 August 2009 - 04:55 PM

If your code is not right, a compile will print out errors. Please post the errors.
Was This Post Helpful? 0
  • +
  • -

#4 hellhound  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 07-March 09

Re: fails to compile

Posted 22 August 2009 - 05:00 PM

well when i try to compile it it does not finish compiling. instead i get a windows error :cc1plus.exe has stopped working. and then get a exit code 1 when i click on exit.
Was This Post Helpful? 0
  • +
  • -

#5 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: fails to compile

Posted 22 August 2009 - 05:31 PM

What compiler (and operating system) are you using? I just compiled it with VC++ 2008 Express and the compiler itself didn't crash. But the compiler did produce a couple errors you need to fix such as this one: courses >> num; courses is the name of a structure, and that line is just not possible.

What compiler (and operating system) are you using? I just compiled it with VC++ 2008 Express and the compiler itself didn't crash. But the compiler did produce a couple errors you need to fix such as this one: courses >> num; courses is the name of a structure, and that line is just not possible.
Was This Post Helpful? 0
  • +
  • -

#6 hellhound  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 07-March 09

Re: fails to compile

Posted 22 August 2009 - 05:50 PM

ok im using gcc and vista 64bit business. and i changed that line it was ment to be coursefile >> num;
Was This Post Helpful? 0
  • +
  • -

#7 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: fails to compile

Posted 22 August 2009 - 05:54 PM

So, is your problem solved ??
Was This Post Helpful? 0
  • +
  • -

#8 hellhound  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 07-March 09

Re: fails to compile

Posted 22 August 2009 - 06:00 PM

op sorry no that same thing still occurs. and i cant use another program because thats the only one avalible at uni. and i cnat use the ones on my computer cus my harddrive gave out the other day
Was This Post Helpful? 0
  • +
  • -

#9 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: fails to compile

Posted 22 August 2009 - 07:18 PM

The problem is in the course structure -- you have apparently overflowed the compiler's memory with that huge 2,000 element array of structures. Replace that array with a vector, which will expand as necessary to fit the data.
Was This Post Helpful? 0
  • +
  • -

#10 hellhound  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 07-March 09

Re: fails to compile

Posted 22 August 2009 - 07:32 PM

yea the problem is that but our assignment states we have to use it as an array in the form. so im kind of stuck on that matter.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: fails to compile

Posted 22 August 2009 - 07:37 PM

Dynamically allocate/deallocate rather then putting it on the program's stack.
Was This Post Helpful? 0
  • +
  • -

#12 hellhound  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 07-March 09

Re: fails to compile

Posted 22 August 2009 - 11:32 PM

ok thanks guys the only way i can see this working is with a vector so im going to go that way.
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: fails to compile

Posted 23 August 2009 - 01:53 AM

The side issue of the 2000 element array is a complete red herring that you can safely ignore. It's a big array but nothing that can't be handled.

When you say you try to compile this what are you actually trying to compile?

If you try to compile what you posted it can't work because there is no main().

Are you trying to just compile that chunk or are you compiling that in context of other code that will make an actual program?

It looks to me like you might be just trying to compile a random chunk of code which will always fail. Programs compile, code chunks don't.
Was This Post Helpful? 0
  • +
  • -

#14 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: fails to compile

Posted 23 August 2009 - 05:53 AM

View Postjanotte, on 23 Aug, 2009 - 12:53 AM, said:

The side issue of the 2000 element array is a complete red herring that you can safely ignore. It's a big array but nothing that can't be handled.

When you say you try to compile this what are you actually trying to compile?

If you try to compile what you posted it can't work because there is no main().

Are you trying to just compile that chunk or are you compiling that in context of other code that will make an actual program?


Its not his code. I get the same problem on cygwin even after adding a main().

hellhound: You don't have to use vectors -- just make the array a pointer, allocate it with new operator in the constructor then delete[] it in the descructor.

This post has been edited by Ancient Dragon: 23 August 2009 - 05:55 AM

Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: fails to compile

Posted 23 August 2009 - 06:28 AM

View PostAncient Dragon, on 23 Aug, 2009 - 04:53 AM, said:

Its not his code. I get the same problem on cygwin even after adding a main().


What are you trying to say here?

In what way is it "not his code"?
What does that mean?

What is the problem you get on cygwin?
In what way is it same as something in the conversation and what is the thing it is the same as?

Are you saying this (hellhound's code with a main() added and a couple of obvious errors corrected) doesn't compile for you?
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main() {
    // nothing here
    return 0;
}


string comp = "-1";

class student {
    friend class course;
    private: 
        struct students {
            string firstname;
            string lastname;
            string ID;
            char fees;
        };
        students list [2000];
    
    public:
        student() {};
        ~student() {};
        void readstudents();
        
};

void student::readstudents() {
    ifstream studentfile ("students.txt");
    if(!studentfile) {
        cout << "unable to open students.txt" << endl;
        exit(1);
    }
    float num;
    int i=0;
    string temp;
    studentfile >> num;
    if(num > 2000) {
        cout << "to many students. exiting" << endl;
        exit(0);
    }
    getline (studentfile, temp);
    for(i=0; i<num; i++) {
        getline(studentfile, list[i].firstname);
        getline(studentfile, list[i].lastname);
        getline(studentfile, list[i].ID);
        studentfile >> list[i].fees;
        getline(studentfile, temp);
    }
    i++;
    list[i].ID=comp;
    studentfile.close();
}

class course{
    private:
        struct studentlist {
            string ID;
        };
        struct courses {
            string courseID;
            string title;
            int credits;
            studentlist students[2000];
        };
        int num;
        courses clist[200];
        
    public:
        course() {};
        ~course() {};
        void readCourses();
        void notEnrolled();
        void noStudents();
        void ghostStudents();
};

void course::readCourses() {
    string temp;
    ifstream coursefile ("courses.txt");
    if(!coursefile) {
        cout << "unable to open course.txt" << endl;
        exit(1);
    }
    coursefile >> num;
    if(num > 200) {
        cout << "to many courses. Exiting" << endl;
        exit(0);
    }
    getline (coursefile,temp);
    for(int i=0; i<num; i++) {
        getline(coursefile,clist[i].courseID);
        getline(coursefile,clist[i].title);
        coursefile >> clist[i].credits;
        getline(coursefile,temp);
        int j=0;
        while(true) {
            getline(coursefile,clist[i].students[j].ID);
            if(clist[i].students[j].ID.compare(comp) == 0) {
                break;
            }
            j++;
        }
    }
    coursefile.close();
}



Or are you trying to say something completely different?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2