Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to programing and I have no idea what I am doing. My instructor is not very helpful so I am pretty much screwed. Please help.
C++
//Example program
#include <iostream>
#include <string>

int getStudentCount()
{
	std::cout << "Enter number of students";
	int numStudents;
	std::cin >> numStudents;
	return numStudents;
}

int getTeacherCount()
{
	std::cout <<"Enter number of teacher:";
	int numTeacher;
	std::cin >> numTeacher;
	return numTeacher;
}
int calculateResult(int numStudents, int numTeacher)
{
	int classSize;
	int classSize = numStudents / numTeacher;
	cout << classSize;
	return 0;
}

void printResults(int classSize)
{
	std::cout <<"The average is: " << classSize << std::endl;

}
int main()
{
	int numStudents = getStudentCount();
	int numTeacher = getTeacherCount();
	int classSize = calculateResult(numStudents, numTeacher);
	printResults(classSize);
	int
}

I need it to be able to divide and it is not working. I don't know what else to do.

What I have tried:

I don't know what else to do. I am using Eclipse.
Posted
Updated 20-Oct-16 14:24pm
v3
Comments
[no name] 20-Oct-16 12:05pm    
Is your teacher "not helpful" because you do not know how to ask a question? Or not helpful because you didn't ask them at all? Did you tell them what you think "not working" means? It doesn't mean anything to us. Did you step through your code using the debugger? What did you find?
Member 12805452 20-Oct-16 13:00pm    
I do not know how to do anything you have just mentioned. This is the first programming class that I am taking. The instructor does not reply to any emails and he expects us to know how to do this. There is no lecture or at least a breakdown. We are using the textbook Programming in C/C++ by Diane Zak. So please, I do need help but if you are trying to make me look stupid then I don't want it. Like I said, I am new and have no idea what I am doing. I had to youtube and google everything.
[no name] 20-Oct-16 13:54pm    
If your teacher isn't teaching you, then get a different teacher.
Member 12805452 20-Oct-16 13:56pm    
Well, that would be a solution, but he is the only instructor for this course. I don't have a choice.
[no name] 20-Oct-16 14:01pm    
Well then you are going to have to work harder. Team up with a classmate to support each other (not copy each others work). Learn to use the debugger. Learn how to ask questions in forums (not working is NOT a description of a problem). Read your textbook then read it again. I would be complaining to the school that the teacher is not doing his job. We can't teach you programming in a forum posting.

Richard is spot on with what the problem is, but you could - and should - have spotted this yourself, with the help of the debugger.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Learning this skill will save you a lot of time, effort, and hair-pulling!
 
Share this answer
 
To learn C/C++, I recommand these books, and learning the debugger is mandatory:

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Hello @ member 12805452 it sounds like getTeacherCount should have been returning 0 instead :P (THIS IS A JOKE DON'T DIVIDE BY 0 IN C it can have funny effects depending on your setup.)

The only problem i see with the logic is the return 0 from the calculateResults

I was Taught it is a good idea to prototype above main then put the function definitions below soo i did that here.

C++
//Example program
#include <iostream>
#include <string>
int getStudentCount();
int getTeacherCount();
int calculateResult(int numStudents, int numTeacher);
void printResults(int classSize);

int main()
{
	int numStudents = getStudentCount();
	int numTeacher = getTeacherCount();
	int classSize = calculateResult(numStudents, numTeacher);
	printResults(classSize);
	while(1);//never lets the program end MUHAHAHAHAHAHA 
                 //Ctrl+c to exit a cmd prompt app
	return 0;// without returning an int here the compiler will complain
}
int getStudentCount()
{
	std::cout << "Enter number of students";
	int numStudents;
	std::cin >> numStudents;
	return numStudents;
} 
int getTeacherCount()
{
	std::cout <<"Enter number of teacher:";
	int numTeacher;
	std::cin >> numTeacher;
	return numTeacher;
}
int calculateResult(int numStudents, int numTeacher)
{
	int classSize;
	int classSize = numStudents / numTeacher;
	cout << classSize;  // was this just an attempt at debugging, 
	return classSize;               //or did you actually want to echo this?
}  
void printResults(int classSize)
{
	std::cout <<"The average is: " << classSize << std::endl;
 
}



O and like what everyone else was saying, if you have a debugger at your disposal you should master it.
 
Share this answer
 
v2
Comments
Richard MacCutchan 21-Oct-16 9:15am    
I was Taught it is a good idea to prototype above main then put the function definitions
If you think about it, it is quite a bad idea. Why have a prototype and a definition in the same source module? If you change anything in the definition you have to remember to change the prototype, and vice versa; patently a waste of time.
Philippe Mori 21-Oct-16 16:22pm    
In C++, a function must be declared before it can be called so you don't really have any choice except that you might reorder functions. In this case, moving main() at the end of the file would be enough...

Rule is different for member function as any member are available from the start of the class.

For a simple program that consist of a single file, this is perfectly fine. Until the user make some more ambitious program, there is no need to bother with multiple files.
Rbabs 21-Oct-16 17:27pm    
I believe the idea behind it is two-fold.
First off visually you want the entry point to be easily located IE near the top. Second you want the compiler to know about functions that may be used without having to process them at the time, until they are actually needed so you won't allocate space unnecessarily if a function is not actually called.

Remember this language was made when memory was at a premium so the end goal was not efficiency and ease of use but more on saving the precious few resources you have.
Richard MacCutchan 22-Oct-16 3:57am    
It does not matter whether a function is called or not, if it is in the source code it will be compiled and it will be allocated space.
enhzflep 22-Oct-16 5:05am    
Allocated space in the intermediary object-file? Sure. Allocated space in the final executable? Not nearly so straight-forward.
Perhaps an important distinction to someone new to the black-art.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900