Need help fixing an error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Need help fixing an error

  1. #1
    Join Date
    Nov 2012
    Posts
    4

    Resolved Need help fixing an error

    I ran the program and now lines 38, 92, 105, 108, 129, 138-140, 156-160, and 183 have these lime green colored blocks next to them and the error opens this crtexe.cpp file and highlights line number 555 in there:

    Code:
    mainret = main(argc, argv, envp);

    My whole code is:
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    //student Class declaration
    class Student
    {
    	private:
    		string name;
    		int id;
    		int *test;
    		int num;
    		void makeArray();
    	public:
    		Student();
    		Student(int n);
    		Student(string nm, int i, int n);
    		void setName(string nm);
    		void setID(int i);
    		void setScore(int i, int s);
    		string getName() const;
    		int getID() const;
    		void showScore();
    		void display();
    		~Student();			
    };
    
    //makeArray allocate an int array with num elements, 
    //assigns the address of the array to test and assigns 0 to all elements
    void Student::makeArray()
    {
    	int size = Student::num;
    	int *studentArray;
    	studentArray = new int[num];
    	Student::test = studentArray;
    
    	test = 0;
    }
    
    //Student function
    Student::Student()
    {
    	setName("NONE");
    	setID(10);
    	Student::num = 3;
    
    
    }
    
    //Second student function, takes in 2 parameters
    Student::Student(int n)
    {
    	setName("None");
    	setID(10);
    	if(n > 0){
    		Student::num = n;
    	}else{
    		Student::num = 3;
    	}
    	makeArray();
    }
    
    //Third student function, takes in 3 parameters
    Student::Student(string nm, int i, int n)
    {
    	setName(nm);
    	setID(i);
    	if(n > 0){
    		Student::num = n;
    	}else{
    		Student::num = 3;
    	}
    	makeArray();
    }
    
    //sets name to nm
    void Student::setName(string nm)
    {
    	Student::name = nm;
    }
    
    //sets id to i. If i is in range of 10 - 99. If not then it sets id to 10 and prits and error
    //message saying it cannot set the id to i. The error message should include the students name
    //by displaying the return value of get name
    void Student::setID(int i)
    {
    	if(i >= 10 && i <= 99){
    		Student::id = i;
    	}else{
    		Student::id = 10;
    		cout << "Invalid.  Can not set id to " << i << " for " << getName() << endl;
    	}
    }
    
    //only sets the score if index i is a valid index within the bounds of the dynamic array holding the test scores,
    //and if s is a valid score in teh range of 0-100. If it doesn't meet thse conditions, an error message should display
    // saying the test i cantnot be set to s. Error message should include student's name by displaying return value or getName.
    void Student::setScore(int i, int s)
    {
    	if(i < Student::num){
    		if(s >= 0 && s <= 100){
    			test[i] = s;
    		}else{
    			cout << "Invalid.  Can not set test " << i << " to " << s << " for " << getName() << endl;
    		}
    	}else{
    		cout << "Invalid.  Can not set test " << i << " to " << s << " for " << getName() << endl;
    	}
    }
    
    //returns the name
    string Student::getName() const
    {
    	return Student::name;
    }
    
    //returns the id
    int Student::getID() const
    {
    	return Student::id;
    }
    
    //displays the test number and the score
    void Student::showScore()
    {
    	for(int count = 0; count < Student::num; count++)
    	{
    		cout << "Test " << count << "had a score of " << test[count] << endl;
    	}
    }
    
    //get and display the student's information
    void Student::display()
    {
    	cout << "The Name: " << getName();
    	cout << "The ID: " << getID();
    	showScore();
    	cout << endl;
    	cout << endl;
    }
    
    //frees the array that test is pointing to
    Student::~Student()
    {
    	free(test);
    }
    int main()
    {
    	//sets up the 3 students
    	Student a;
    	Student b(4);
    	Student c("Joe", 40, 5);
    
    	//calls the set functions
    	cout << "Calling the set functions";
    	//for student a
    	a.setName("Tom");
    	a.setID(200);
    	a.setID(20);
    	a.setScore(0, 75);
    	a.setScore(1, 85);
    	a.setScore(2, 95);
    
    	//for student b
    	b.setName("John");
    	b.setID(30);
    	b.setScore(0, 70);
    	b.setScore(1, 80);
    	b.setScore(2, 90);
    	b.setScore(3, 100);
    
    	//for student c
    	c.setScore(0, 90);
    	c.setScore(1, 91);
    	c.setScore(2, 92);
    	c.setScore(3, 93);
    	c.setScore(4, 94);
    	c.setScore(5, 95);
    	c.setScore(4, 105);
    	c.setScore(5, 105);
    
    	//the display function
    	a.display();
    	b.display();
    	c.display();
    
    	
    	system("pause");
    	return 0;
    }
    Any help with why i am getting this error and how to fix it would be greatly appreciated. Thanks
    Last edited by fergis911; November 26th, 2012 at 08:38 PM. Reason: Resolved

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,202

    Re: Need help fixing an error

    It took literally less than a second for the debugger to point out that test is an invalid pointer. You need to learn to use your debugger. You can't program without it.

    Look at this code.
    Code:
    	Student::test = studentArray;
    
    	test = 0;
    First, why are you using the scope resolution operator. That's not needed, and even misleading here.
    test = studentArray would be more appropriate, although why you're not just initialing test instead of using studentArray isn't clear.

    Regardless, you allocate it, then immediately set the allocated pointer to zero.
    Last edited by GCDEF; November 26th, 2012 at 07:23 PM.

  3. #3
    Join Date
    Nov 2012
    Posts
    4

    Re: Need help fixing an error

    Well sorry if i did a rookie mistake i am extremely new to programming like maybe less than a year experience. I have no idea what half of these errors even mean, and so i have trouble problem solving. What exactly do i need to change to make it work better. with the makeArray function I am trying to make an array with num elemets, and store it in test. I thought what i did was that exactly but turns out it isn't so what do i need to change?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,202

    Re: Need help fixing an error

    You did make an array with num elements and set test to point to it, but then you set test to 0 right away, so you set a value you can't dereference and you no longer have access to your array.

    Better syntax for makeArray...
    Code:
    void Student::makeArray()
    {
    	test = new int[num];
    }
    That's all you need. Lose all the :: to access your member variables.

  5. #5
    Join Date
    Nov 2012
    Posts
    4

    Re: Need help fixing an error

    So i changed makeArray to be:
    Code:
    void Student::makeArray()
    {
    	int size = Student::num;
    	int *studentArray;
    	studentArray = new int[num];
    	Student::test = studentArray;
    
    	for (int i = 0; i < size; i++)
    		test[i] = 0;
    }
    but still get an error, what am i missing? and what is wrong?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,202

    Re: Need help fixing an error

    Quote Originally Posted by fergis911 View Post
    So i changed makeArray to be:
    Code:
    void Student::makeArray()
    {
    	int size = Student::num;
    	int *studentArray;
    	studentArray = new int[num];
    	Student::test = studentArray;
    
    	for (int i = 0; i < size; i++)
    		test[i] = 0;
    }
    but still get an error, what am i missing? and what is wrong?
    You could read my previous post and follow my advice for starters. Not much incentive to help if you ignore what I say.

  7. #7
    Join Date
    Nov 2012
    Posts
    4

    Re: Need help fixing an error

    Well like i said I am fairly new to programming, and what you said made little to no sense to me, luckily i figured it out on my own. Just because i do something wrong doesn't mean you have to be an *** about it. Maybe you could learn to notice someone didn't understand what you said and try rewording it. Lets hope to god you never go into IT and ACTUALLY have to help people.

    Thanks for helping anyways i guess, i figured it out about 3 hours ago.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Need help fixing an error

    Quote Originally Posted by fergis911 View Post
    Well like i said I am fairly new to programming, and what you said made little to no sense to me, luckily i figured it out on my own.
    So let's see your final results. You may think you fixed the problem, but with the code you had and the little experience you claimed, there is a good chance you really didn't fix the problem but only believed you fixed the problem.

    When writing C++, you need to be 100% sure that the code is correct. There is a concept called undefined behaviour that occurs when the program seems to behave "correctly", but in reality is still wrong and can crash. Your code is not beginner code, as it deals with classes, dynamically allocated memory, etc. When you get to that stage, it is mandatory that you know exactly what you're doing, and as GCDEF stated, use the debugger.

    Unless you are experienced in C++ such as GCDEF and others here, then the claim that you fixed the code can't really be trusted unless you show us how you addressed the problem.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Need help fixing an error

    Quote Originally Posted by fergis911 View Post
    Well like i said I am fairly new to programming, and what you said made little to no sense to me, luckily i figured it out on my own. Just because i do something wrong doesn't mean you have to be an *** about it. Maybe you could learn to notice someone didn't understand what you said and try rewording it. Lets hope to god you never go into IT and ACTUALLY have to help people.
    There is no reason to react like this. Nobody blames you for making mistakes, but we do expect you try to make the most out of the advice you are getting from experienced C++ developers for free. If you don't understand part of that advice, then search the internet to see if you can find an explanation and if you still don't understand, indicate here exactly what you don't understand. If you are not willing to make an effort, you can't expect anyone else to make an effort for you.

    GCDEF has indicated twice (in two different ways) that there is no reason to fully qualify your member variables. So, instead of writing "Student::num" and "Student::test", you can just write "num" and "test" in member functions of the Student class.

    Which book are you using to learn C++? Based on the code you've posted, I would consider getting a better book to learn from. You almost never have to use new[] in C++ nowadays, using std::vector is better. A good book will teach you this from the very beginning.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,202

    Re: Need help fixing an error

    Quote Originally Posted by fergis911 View Post
    Well like i said I am fairly new to programming, and what you said made little to no sense to me, luckily i figured it out on my own. Just because i do something wrong doesn't mean you have to be an *** about it. Maybe you could learn to notice someone didn't understand what you said and try rewording it. Lets hope to god you never go into IT and ACTUALLY have to help people.

    Thanks for helping anyways i guess, i figured it out about 3 hours ago.
    I gave you advice. I even showed you how to write the function without all the extraneous stuff you put in there. You ignored what I said. I don't see how that makes me the bad guy here. If you don't understand what I said, ask for clarification, although honestly, if you didn't understand what I said, and from looking at your code, you should probably take a step back and work through a good tutorial book for a while. And no, that's not being obnoxious. That's just giving you good advice. This isn't a language where guessing gets you very far.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center