虽然没有错误消息,但如何在代码中找到问题? ! [英] How can I find the problem in my code although there is no error message? !

查看:81
本文介绍了虽然没有错误消息,但如何在代码中找到问题? !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我是C ++初学者,我有一个解决的任务是:

写一个程序,选择1到100之间的数字,然后让用户猜出数字是什么。程序应告诉用户他们的猜测是否过高,过低或恰到好处。



我写了源代码,它没有错误,但不准确。例如,(有时候程序会回复一条消息,说明你的猜测太高了,尽管用户输入的数字少于目标,反之亦然。)



我这样做不知道代码中的确切问题是什么。请指导我。



您的时间和精力表示高度赞赏,谢谢。



什么我试过了:



Hello,

I am a beginner in C++ and i have an assignment to solve which is:
"Write a program that picks a number between 1 and 100, and then lets the user guess what the number is. The program should tell the user if their guess is too high, too low, or just right."

I write the source code and it works without errors but its not accurate. for example, (Some times the program gives back a message that your guess is too high although the user enters number less than the target and vice versa.)

I do not know what is the exact problem in the code. Kindly guide me.

Your time and effort are highly appreciated, Thanks.

What I have tried:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int randomnumber ( int low, int high )
{
    return rand() % ( high - low ) + low;
}

int randomnumber();
int low;
int high;
int Entry;
int i;
int target = randomnumber( 1, 100 );

int main()
{

    srand(time(0));

    while (1)
 {

    cout << "Enter any number between 1 and 100: ";
    cin >> Entry;

    for ( i = 0; i <= 100; i++ );
    {
       cout << randomnumber( 1, 100 ) << endl;
    }

    if ( Entry < target )
    {
        cout << "Sorry, Your guess is too low. Try again!";
    }

    else if ( Entry > target )
    {
        cout << "Sorry, Your guess is too high. Try again!";
    }

    else
    {
        cout << "Well done, You guessed just right.";
        break;
    }

 }

}

推荐答案

避免使用全局变量;特别是当它们用函数返回值初始化时。在您的情况下,基于 rand()的值被分配到目标之前 srand()被调用。因此, target 在大多数系统上的每个程序执行时都具有相同的值。



另外,为什么在用户输入其值后,您打印另一个随机值吗?

您可能会将其与用户输入进行比较。但该打印值不是目标值。另请注意末尾的分号语句,这使得循环无效。



所以只需重写为:

Avoid using global variables; especially when they are initialised with function return values. In your case a rand() based value is assigned to target before srand() is called. As a result, target will have the same value at each program execution on most systems.

Also, why are you printing another random value after the user has entered his value?
You probably compare that with the user input in mind. But that printed value is not the target value. Note also the semicolon at the end of the for statement which makes the loop useless.

So just rewrite to:
int randomnumber ( int low, int high )
{
    return rand() % ( high - low ) + low;
}

int main()
{
    srand(time(0));
    int target = randomnumber( 1, 100 );

    while (1)
    {
        int entry;
        cout << "Enter any number between 1 and 100: ";
        cin >> Entry;
        if ( Entry < target )
            cout << "Sorry, Your guess is too low. Try again!";
        else if ( Entry > target )
            cout << "Sorry, Your guess is too high. Try again!";
        else
        {
            cout << "Well done, You guessed just right.";
            break;
        }
    }
}


只是为了添加Jochen所说的:编译没有错误并不代表你的代码是对! :笑:



将开发过程想象成编写电子邮件:成功编译意味着您使用正确的语言编写电子邮件 - 例如英语,而不是德语 - 并不是说电子邮件中包含了你要发送的信息。



现在你进入第二阶段的发展(实际上它是第四或第五阶段,但你会后来进入早期阶段):测试和调试。



首先看看它做了什么,以及它与你想要的有何不同。这很重要,因为它可以为您提供有关其原因的信息。例如,如果程序旨在让用户输入一个数字并将其翻倍并打印答案,那么如果输入/输出是这样的:

Just to add to what Jochen said: compiling without errors does not mean your code is right! :laugh:

Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

然后很明显问题出在将它加倍的位 - 它不会将自身加到自身上,或者将它乘以2,它会将它自身相乘并返回输入的平方。

所以,你可以查看代码和很明显,它在某处:

Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:

int Double(int value)
   {
   return value * value;
   }



一旦你知道可能出现的问题,就开始使用调试器找出原因。在你的线上设一个断点:


Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:

cout << "Enter any number between 1 and 100: ";



并运行你的应用程序。在执行代码之前,请考虑代码中的每一行应该做什么,并将其与使用Step over按钮依次执行每一行时实际执行的操作进行比较。它符合您的期望吗?如果是这样,请转到下一行。

如果没有,为什么不呢?它有何不同?



这是一项非常值得开发的技能,因为它可以帮助你在现实世界和发展中。和所有技能一样,它只能通过使用来改进!


and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!


Quote:

我编写源代码和它没有错误,但不准确。例如,(有时程序会回复一条消息,说明你的猜测太高了,尽管用户输入的数字少于目标,反之亦然。)

I write the source code and it works without errors but its not accurate. for example, (Some times the program gives back a message that your guess is too high although the user enters number less than the target and vice versa.)



编写一些无需编译的代码错误并不意味着代码正在做预期的事情。

猫在天空飞得很高。是正确的英语语法,但是没有意义。

有一个工具可以帮助你理解代码中的错误,它是调试器



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



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



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

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Writing some code that compile without error does not mean that the code is doing what is expected.
"The cat flies high in the sky." is correct English grammar, but is non sense.
There is a tool that will help you to understand what is wrong in your code, its the debugger.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于虽然没有错误消息,但如何在代码中找到问题? !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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