请告诉程序中的错误? [英] Tell the error in the program please?

查看:77
本文介绍了请告诉程序中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序来输入数字,直到用户想要,最后它应显示输入的正数,负数和零数。

在我的程序中,编译器不要求y / n只是告诉正数,负数和零数。是不是scanf没有被执行或者有一些预定义的选择值?



我尝试过:



Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.
In my program the compiler is not asking for y/n and just telling the number of positive, negative and zeroes. Is it that scanf is not being executed or there is some predefined value of choice?

What I have tried:

#include <stdio.h>

int main()
{
    int number, positive = 0, negative = 0, zero = 0;
    char choice;

    do
    {
        printf("Enter a number :");
        scanf("%d", &number);

        if (number > 0)
        {
            positive++;
        }
        else if (number < 0)
        {
            negative++;
        }
        else
        {
            zero++;
        }

        printf("Do you want to Continue(y/n)? ");
        scanf("%c", &choice);

    }while (choice == 'y');


    printf("\nPositive Numbers :%d\nNegative Numbers :%d\nZero Numbers :%d",
        positive, negative, zero);


    return 0;
}

推荐答案

在您询问是否要继续之前添加以下行:

Add the following line before you ask if they want to continue thus:
// clear the input buffer
fseek(stdin, 0, SEEK_END);

printf("Do you want to Continue(y/n)? ");
scanf("%c", &choice);


Quote:

在我的程序中,编译器不要求y / n

In my program the compiler is not asking for y/n



编译器不会询问用户输入,因为它不是它的工作。

编译器编译你的源代码,这意味着它检查源代码是否正确C代码句法。如果正确,它会生成一个可执行文件,可执行文件是执行源代码中的可执行文件的可执行文件。这并不意味着可执行文件会按照您的预期执行。

猫在天空飞得很高。是正确的英语语法,但没有任何意义,因为猫不会飞。

好​​消息是调试器允许你观察可执行文件中发生的事情,它是一个很好的学习工具。 br />


你的代码没有你想象的那样,或者你不明白为什么!



那里是一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

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

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你显示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



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



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

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

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


The compiler don't ask user input because it is not its job.
The compiler compile your source code, it means that it check that source code is correct C code syntax. if correct, it generate an executable, the executable is the one that do what is in your source code. This does not mean the the executable will do what you expect.
"The cat fly high in the sky." is correct English grammar, but means nothing because cats don't fly.
The good news is that the debugger allow you to watch what is going on in the executable, it is a great learning tool.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于请告诉程序中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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