无法让我的代码工作 [英] Can not get my code to work

查看:73
本文介绍了无法让我的代码工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,我不知道自己在做什么。我的导师不是很有帮助所以我几乎搞砸了。请帮助。

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.

//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
}



我需要它才能分割它是不工作我不知道还能做什么。



我尝试了什么:



我不知道还能做什么。我正在使用Eclipse。


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.

推荐答案

理查德现在问题是什么,但你可以 - 并且应该 - 在调试器的帮助下自己发现了这个问题。



在函数的第一行放置断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


学习这项技能将为您节省大量的时间,精力和拔毛!
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!


为了学习C / C ++,我推荐这些书,学习调试器是强制性的:



以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

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 ++编程语言 [ ^ ]





你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。
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.


Hello @ member 12805452它听起来像是getTeacherCount应该返回0而不是:P(这是一个JOKE,它不会被0分为0)根据你的设置可以产生有趣的效果。)



我看到逻辑的唯一问题是来自calculateResults的返回0



我被教导它是一个好主意上面的主要原型然后把功能定义放在下面soo我在这里做了。



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.

//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和其他人一样,如果你有一个调试器,你应该掌握它。




O and like what everyone else was saying, if you have a debugger at your disposal you should master it.


这篇关于无法让我的代码工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆