编写一个只允许整数输入的程序。 [英] Write a program which allows integer input only.

查看:65
本文介绍了编写一个只允许整数输入的程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们编写了一个只显示整数输入的程序。如果我们输入一些字符,那么就不应该在屏幕上显示。



请解释getch()函数如何当我们使用ch = getch();时,什么值存储在ch中。

当我们将ch声明为int时,即使我们使用了printf语句中的整数值也会如何打印%c格式说明符?



我尝试过:



 #include< stdio.h> 
main()
{
int x = 0;
x = getIntegerOnly();
printf(\ n您已输入%d,x);
getch();

}
int getIntegerOnly()
{
int num = 0;
int ch;
for(;;)
{ch = getch();
if(ch> = 48&& ch< = 57)

{

printf(%c,ch);
num = num * 10 +(ch-48);
}
if(ch == 13)
break;
}
返回num;
}

解决方案

如果您查看文档,您会看到 getch 从用户获取单个字符:在C-C编程中使用getch(),getche()和getchar() [ ^ ]



因此,如果用户按下A键,您将获得a或A,具体取决于他的SHIFT和/或CAPS状态。

如果他按下数字键,您将获得该键作为一个字符:'0'到'9'

注意,它与整数中的九个值不同:

  int  a =  9 ; 
char b = ' 9';

a b 不包含相同的值!

如果你想转换,你需要将字符更改为数字:

  int  a =  9 ; 
char b = ' 9';
int c =( int )(b - ' 0');

现在 a c 将包含相同的值!



不要使用像48或57这样的幻数 - 它不在当你这样做时,你的意思很明显:

 if(ch> ='0'&& ch< ='9')

更清晰,更容易阅读!


引用:

请解释getch()功能正常。



我知道这是一个愚蠢的想法,但你应该考虑阅读文档。

Quote:

当我们使用ch = getch()时,ch中存储了什么值;



你不明白你的代码做了什么。



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

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

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

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



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


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

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



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



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


We have write a program that displays only integer input.If we input some character then it should not display on screen.

Please explain how getch() function is working.What value gets stored in ch when we use ch=getch();.
When we declare ch as int then how does in the printf statement the value of integer gets printed even if we use %c format specifier?

What I have tried:

#include <stdio.h>
main()
{
    int x=0;
    x=getIntegerOnly();
    printf("\nYou have entered %d",x);
    getch();

}
int getIntegerOnly()
{
    int num=0;
    int ch;
    for(;;)
    {    ch=getch();
        if(ch>=48 && ch<=57)

        {

           printf("%c",ch);
            num=num*10+(ch-48);
          }
           if(ch==13)
            break;
    }
    return num;
}

解决方案

If you look in the documentation, you will see that getch fetches a single character from the user: Use of getch(),getche() and getchar() in C - C Programming[^]

So if the user presses the A key, you will get either 'a' or 'A' depending on his SHIFT and / or CAPS status.
If he presses a numeric key, you will get that key as a character: '0' to '9'
Note that isn't the same value as nine in an integer:

int a = 9;
char b = '9';

a and b do not contain the same value!
If you want to convert, you need to change the character to a number:

int a = 9;
char b = '9';
int c = (int) (b - '0');

Now a and c will contain the same value!

Don't use "magic numbers" like 48, or 57 - it's not at all obvious what you mean when you do that:

if( ch >= '0' && ch <= '9')

is a lot clearer and easier to read!


Quote:

Please explain how getch() function is working.


I know it is a silly idea, but you should think about reading the documentation.

Quote:

What value gets stored in ch when we use ch=getch();


you don't understand what your code is doing.

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.

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天全站免登陆