Thread: Help me fix my mess!!!!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    19

    Help me fix my mess!!!!

    Ok, I created a mess, and I am in 36 complier errors. Now, I fixed most of what I know how to fix but some of the errors I don't know how to fix and I am taking a different approach to this problem.

    The program is supposed to ask a student what is name and grades are, then it is supposed to get grades from a student and write them to a file using dynamic memory allocation. I will post the code first and if anyone needs to see the billions of errors I can post the log file.

    OS: XP Tablet PC Edition.
    Complier: Visual C++ 2005 express

    Code:
    #include <iostream> 
    #include <cmath> 
    #include <iostream>
    
    using namespace std;
    
    class Student 
    {
    public:
    	void calculate();
    	void writeToFile();
    	char name[20]; 
    	int age, grade;
    	int numtests;
    	int i = 0;
    	int *iPtr;
    	};
    
    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.writeToFile();
    	return 0;
    }
    	
    	
    	//Calculate grades
    	void calculate() 
    	{
    		Student st;
    		cout << "Enter the number of tests you are entering: ";
    		cin >> st.numtests;
    		int * st.iPtr = new int[st.numtests];
    		for (int st.i=0; st.i<st.numtests; st.i++)
    		{
    			cout << "Enter test score #" << st.i + 1 << " : ";
    			cin >> st.iPtr[i];
    		}
    	}
    
    	
    	//WritetoFile function
    	int writeToFile()
    	{
    		Student stu;
    		ifstream a_file;
    		cout << "Now writing data to file.....";
    		ofstream a_file("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 (int stu.i = 0; stu.i < stu.numtests; stu.i++;)
    			{
    				a_file << "Test score #" << stu.i + 1 << " is " << stu.iPtr[stu.i] << endl;
    				cout << "Data finished writing to file.";
    				delete [] stu.iPtr;
    			}
    		}
    	}

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    This will now compile. I did not run it to see if it actually works as it should. A big tip: compile ALOT more often!

    Code:
    #include <iostream> 
    #include <cmath> 
    #include <fstream>
    
    using namespace std;
    
    class Student 
    {
    public:
    	void calculate();
    	int writeToFile();
    	char name[20]; 
    	int age, grade;
    	int numtests;
    	int i;
    	int *iPtr;
    	};
    
    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.writeToFile();
    	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];
    		}
    	}
    
    	
    	//WritetoFile function
    	int Student::writeToFile()
    	{
    		Student stu;
    		cout << "Now writing data to file.....";
    		fstream a_file("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;
    				cout << "Data finished writing to file.";
    				delete [] stu.iPtr;
    			}
    		}
    	}

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Usually when there are a billion errors, they are results of previous errors, so the first 2-3 errors would be the only real errors, and you should have posted them at least.

    void calculate() should be void Student::calculate(), and int writeToFile() should be int Student::writeToFile() and I wont correct more than that unless I see the exact errors.
    Warning: Have doubt in anything I post.

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

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by MadCow257
    A big tip: compile ALOT more often!
    No, no, no. That's horrible practice. You should know exactly what your code will do when you compile without compiling it. Guessing and checking is one of the most basic mistakes a new programmer can make. Compile when you need to, but save, save, save all the time. You should always have backup copies and old versions you can go back to if you need.
    Sent from my iPadŽ

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I really hope you're joking, Sly...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    About what? People that compile every time they make a new statement?

    Not at all. If you need to compile your code and run it to know what it's going to do then you should be studying up better on what you're doing. You should have a very, very sturdy foundation before you should ever need to compile. In the case of the program above, there would be no need to compile it until it's finished. It's short, it's very direct to what it's doing. There should be no suprises when he does compile it and just reading through the code should be enough to know what happens when it's compiled.

    Oh and as for that program... one thing bugs me...

    Code:
    for (st.i=0; st.i<st.numtests; st.i++)
    I really don't think it's nessassary for the control variable in the for loop to be a member of the class.
    Last edited by SlyMaelstrom; 01-30-2006 at 09:32 PM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Thanks for the reply and everything works except that the file isn't created.


    On a side note, how do you tell where you want to file to be placed?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You specify the path name when you create it. Otherwise it's placed in the current directory which is the directory the program is run from.
    Sent from my iPadŽ

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by SlyMaelstrom
    About what? People that compile every time they make a new statement?

    Not at all. If you need to compile your code and run it to know what it's going to do then you should be studying up better on what you're doing.

    I disagree. Compiling often is good practice. In C++ there are several gotchas when it comes to syntax. Get someone to remove a semi-colon at the end of a class declaration and see how long it takes you to find! If you compile frequently, you can be fairly sure that the error was introduced in the last few lines you typed.


    Quote Originally Posted by SlyMaelstrom
    You should have a very, very sturdy foundation
    agreed. and the only way to get that is too have a syntactically correct program, which only the compiler can gaurentee.

    before you should ever need to compile.
    why? the compiler is a tool. use it.
    "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?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If you need to compile your code and run it to know what it's going to do then you should be studying up better on what you're doing.

    Actually, if you compile and run the code often you have already learned a valuable lesson. You don't compile and run to know what the code will do (some beginners might, but that doesn't mean that the practice itself is bad). You compile and run to verify that your most recent changes do what you expect and don't screw anything else up.

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    You compile and run to verify that your most recent changes do what you expect and don't screw anything else up.
    Thats exactly why I compile and run often. Once you get a base down that compiles and works, you probably don't want to leave compiling off too long so know what recent changes might have screwed something up.

    Not everything you program has a guide/chapter/tutorial, like making a stack in college, so you might not know exactly what you're doing, or you might find changes/improvements/etc. that may possibly screw things up.

    Sure, guessing might be bad practice (a common example would be pointers), and the person should probably go read up on it more, but thats if they dont go varify, and thats not the fault of compiling often. It more just a beginner thing to not check on what they are unsure of.

    Compiling often would also give you a lead on something you aren't sure on, so you can go read up on it, versus leaving it and working with that (say vector) problem which you would be using and having to change a lot of code later because of. (As well as a slur of errors to go along with it).
    Warning: Have doubt in anything I post.

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

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Daved
    You compile and run to verify that your most recent changes do what you expect and don't screw anything else up.
    That's what I disagree with, though. To me, compiling a program every few lines is like speaking in a second language, but asking someone if you said it right, every few sentences. It's not bad when you're learning, but if you're intending to learn the language, you should learn it by knowing it and not by checking yourself constantly. You should have the confidence in what your doing enough to do it without checking your knowledge.

    As for syntax errors. Maybe it's just the compilers I use, but they've all given a line number when I get an error, so finding a missing semi-colon or something similar hasn't really been a problem. In fact, I've never had an issue with finding a syntax error thus far in my career. Keep in mind I've never written a program consisting of more than lets say 2000 lines and 4 or 5 classes.
    Sent from my iPadŽ

  13. #13
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by SlyMaelstrom
    That's what I disagree with, though. To me, compiling a program every few lines is like speaking in a second language, but asking someone if you said it right, every few sentences. It's not bad when you're learning, but if you're intending to learn the language, you should learn it by knowing it and not by checking yourself constantly. You should have the confidence in what your doing enough to do it without checking your knowledge.
    Even while keeping in mind that not everyone has a well setout C++ beginners book or college course? Many people out there simply reading tutorials wont get indepth information and knowledge, and therefor possibly no confidence.

    Either way the problems are going to be corrected though, so checking to make sure its not screwed up would be advantageous and eventually the person should grow confidence.

    I could see this with this for basic C++ programs, possibly a stack/heap/anagram/logic problem/text game/etc program. I know I barely compile at all (usually until the program is done) with those. Of course when you get to adding onto the initial design I find the advantage of checking often better than risking it on confidence.

    However when you're getting into 3-5 classes when working with openGL/flash/win32, IMO it'd be more of an advantage to check than to be so confident. For example I change a lot of lines to encapsulate some openGL code and which crashed after I finished and to this day I don't know which modification I made screwed it up. I've got a 4 class, 1100 line, project for a windows based website in actionscript and I'm not confident in the least. I did dramastically reprogram it and add 200 lines without compiling, but after that I had a lot of bugs to weed out, and needed to compile often. I probably have different feelings towards it because I seriously never know how Flash is going to react next.
    Warning: Have doubt in anything I post.

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

  14. #14
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    good point Dae.

    say I design an application. I start work on it and am confident that I know what every single piece of code does. the application ends up to be 30 lines. I hit compile and get 30 errors (a little co-incidental, but you get the point). now I have to fix these errors, which will usually involve changing some statements/expressions on each line. it would have been easier for me to compile the first 5 or 6 lines when I am finished them then discover the errors. that way I am not left with bunches of errors, and can fix them as they occur, not all at once. the benefit of fixing them while they occur is that I can also fix my design plan at the same time to suit the changes.

    point being is that you should not be compiling your code after every single statement, and if that is the case maybe you are in over your head and need to slow down. but compiling something that you are unsure about is not wrong, because chances are the compiler will give you a good explanation about exactly where and why the error occured, which is useful info for fixing it. I'd rather fix the error and then modify my design plan than end up with a mess of code that involves a total re-design.

    you might think that compiling oftenly is a waste of time, when if you are not a pro C++ programmer it could actually save you time, in the event that you spend hours coding a 30 line application with 30 errors
    Last edited by Bleech; 01-31-2006 at 01:04 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You should have the confidence in what your doing enough to do it without checking your knowledge.

    Again, that's not the reason for doing it. It is not about checking your knowledge. It is about being productive and using the most effective techniques to get the job done. Whether it is a single file program with only a main() function, or a project with 5000+ files, compiling often is a tool to help you find and fix errors faster. This doesn't mean you must take it to extremes. If I write a simple test program to help answer a question here, I won't compile it until I have written the entire thing. But I compile all the time when adding code to the application I work on.

    Besides, you said that the advice given by MadCow257 to Starr was horrible practice. If somebody writes a full program and compiles for the first time to get 36 errors, they are certainly not ready to code without compiling often.


    >> Thanks for the reply and everything works except that the file isn't created. On a side note, how do you tell where you want to file to be placed?

    You might consider starting a new thread with your code and question. The code ofstream a_file("grades.txt"); opens a file called grades.txt in the current directory, which is usually the same directory where the executable file is. You can use a full path to put it in a different directory (e.g. "C:\\grades.txt").

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, 02: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, 07:12 AM