Help me fix my mess!!!!

This is a discussion on Help me fix my mess!!!! within the C++ Programming forums, part of the General Programming Boards category; I don't know if you remember the early MS Office spellcheck that didn't highlight misspelled words. It just had the ...

  1. #31
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    I don't know if you remember the early MS Office spellcheck that didn't highlight misspelled words. It just had the ability to run a program and it would check your errors. Did you ever use it?

    How often did you run it? Every line? I know that I would only run it when I was finished, and that's no matter how many errors I knew I would have. It's a time on task matter. I keep my mind on writing the code, then I go back and fix errors. If I make an update to code, I make the update, then I compile to see if it all went well.

    To me it doesn't matter how new you are, you should never be doing code that generates more errors than you can handle. If you can't get through a "Hello World" program with little error then perhaps you shouldn't go further than that until you can. This is my whole point. People do something, and make mistakes, they do it again, and make less mistakes but still a decent amount. An amount that they just brush off as small mistakes and then they move on. They continue the same method piling on mistakes until they reach a program and get overwhelmed. I don't like seeing people in this situation and it doesn't have to happen this way.

    Please don't compare what I'm saying to anything but a learning programmer compiling a lot. I'm not talking about updating and maintaining a project that's in use and needs to be coded perfect at all updates. If you want to tell me you should compile all the time with that, then I say fine. You're right. This is different though, I'm talking about a learning process, not an efficiency or precision thing.
    Sent from my iPadŽ

  2. #32
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Obviously, many new programmers must complete homework assignments, so sticking to code that matches their abilities doesn't work for them. This is a common problem. One example might be this case, where the OP had 36 compiler errors after attempting to code the entire solution.

    One piece of advice that would have helped the OP and would help others in a similar situation is to compile more often. Write less code before you compile it and make sure that your thinking is correct. Note that you don't compile your code more often to find out what the code you wrote does, but rather to verify that it does what you think it does.

    >> No, no, no. That's horrible practice.
    All else aside, that comment implied that a programmer should code an entire solution before compiling. IMO (and the opinion of most everybody else), that is completely wrong.

    We are all human, and humans make mistakes. The benefits of finding and fixing mistakes earlier rather than later far outweigh any disadvantages of new coders relying on compiling and running without understanding their code.

  3. #33
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Well, if it helps I don't take any class. I teach myself.


    Now I am getting a wierd error when I try to run a program, I added a new function to obtain the averages of the grades entered. But I get a warning on build and an error when trying to run it. It says that I am using an uninitialized variable and it says the variable that I am using that isn't initialized is the instance of the Student class I made. It is starting to irritate me a bit.

  4. #34
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    post the code.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #35
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Ok, here is the error: "warning C4700: uninitialized local variable 'stude' used"

    And here is the code (You guys are so great! )

    Code:
    #include <iostream> 
    #include <cmath> 
    #include <fstream>
    
    using namespace std;
    
    class Student 
    {
    public:
    	void calculate();
    	int writeToFile();
    	void average();
    	char name[20]; 
    	int age, grade;
    	int numtests;
    	int i;
    	int *iPtr;
    	float avg;
    };
    
    int main()
    {
    	Student stud; 
    	//Ask for students name
    	cout << "Enter the student's name: ";
    	cin.get(stud.name, 20, '\n');  
    	cout << "Enter the student's age and grade seperated by a space: "; 
    	cin >> stud.age >> stud.grade; 
    	stud.calculate();
    	stud.average(); 
    	stud.writeToFile();
    	cout <<"\n";
    	return 0;
    }
    
    
    //Calculate grades
    void Student::calculate() 
    {
    	Student st;
    	cout << "Enter the number of tests you are entering: ";
    	cin >> st.numtests;
    	st.iPtr = new int[st.numtests];
    	for (st.i=0; st.i<st.numtests; st.i++)
    	{
    	cout << "Enter test score #" << st.i + 1 << " : ";
    	cin >> st.iPtr[st.i];
    	}
    }
    
    //Calculate Averages 
    void Student::average()
    {
    	Student stude;
    	cout << "Calculating the average of your percentages.....\n";
    	for (stude.i=0; stude.i < stude.numtests; stude.i++) //The warning points to this line
    	{
    		stude.avg += stude.iPtr[stude.i];
    	}
    	stude.avg /= stude.numtests;
    	cout << "The agervage of the percentiles = " << stude.avg << endl;
    }
    
    
    //WritetoFile function
    int Student::writeToFile()
    {
    	Student stu;
    	cout << "Now writing data to file.....";
    	fstream a_file("C:\\grades.txt");
    	if (a_file.is_open() )
    	{
    		cout << "The file opened incorrectly, program terminating.";
    		return 0;
    	}
    	else
    	{
    		a_file << "The student's name is: " << stu.name << endl;
    		a_file << "The student's grade is: " << stu.grade << endl;
    		for (stu.i = 0; stu.i < stu.numtests; stu.i++)
    		{
    			a_file << "Test score #" << stu.i + 1 << " is " << stu.iPtr[stu.i] << endl;
    			delete [] stu.iPtr;
    		}
    		a_file << "The average of the test grades is: " << stu.avg << endl;
    		a_file.close(); 
    		cout << "Data finished writing and file closed......";
    	}
    	return 0; 
    }

  6. #36
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Its because the initialization of numtests in calculate() is to a local object, which gets deleted at the end of that function. So that object you've created in average() has a fresh numtests (not the one from calculate, unless you passed it by reference). However, since you are doing all of this inside Student's methods you can use Student's variables without declaring an object of Student. Thats the whole point of encapsulation.

    So change it to something somewhat like below.

    Code:
    //Calculate grades
    void Student::calculate() 
    {
    	cout << "Enter the number of tests you are entering: ";
    	cin >> numtests;
    	iPtr = new int[numtests];
    	for (i=0; i<numtests; i++)
    	{
    	cout << "Enter test score #" << i + 1 << " : ";
    	cin >> iPtr[i];
    	}
    }
    
    //Calculate Averages 
    void Student::average()
    {
    	cout << "Calculating the average of your percentages.....\n";
    	for (i=0; i < numtests; i++)
    	{
    		avg += iPtr[i];
    	}
    	avg /= numtests;
    	cout << "The agervage of the percentiles = " << avg << endl;
    }
    
    
    //WritetoFile function
    int Student::writeToFile()
    {
    
    	cout << "Now writing data to file.....";
    	fstream a_file("C:\\grades.txt");
    	if (a_file.is_open() )
    	{
    		cout << "The file opened incorrectly, program terminating.";
    		return 0;
    	}
    	else
    	{
    		a_file << "The student's name is: " << name << endl;
    		a_file << "The student's grade is: " << grade << endl;
    		for (i = 0; i < numtests; i++)
    		{
    			a_file << "Test score #" << i + 1 << " is " << iPtr[i] << endl;
    			delete [] iPtr;
    		}
    		a_file << "The average of the test grades is: " << stu.avg << endl;
    		a_file.close(); 
    		cout << "Data finished writing and file closed......";
    	}
    	return 0; 
    }
    Some other advice would be:

    - Like said before, I'd lose the i member variable, and simply create a local on in the functions.

    - Think about making the member variables private, so only the methods access them, and make methods to retrieve the variables if necessary (getter/setters).

    - You might want to make the member variables prefix m_ so that you can easily tell the difference between local ones.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 03:38 AM
  3. Replies: 1
    Last Post: 12-01-2007, 01:06 AM
  4. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 02:48 PM
  5. Follow Iraq, North Korea trying to mess the World.
    By KingoftheWorld in forum A Brief History of Cprogramming.com
    Replies: 81
    Last Post: 01-14-2003, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21