This question already has answers here:
Closed 5 years ago.
How to do scanf for single char in C [duplicate]
(11个答案)
我是c语言编程的新手
我写了一个程序。但当我在进入我的选择程序之前运行程序时就结束了。
这是截图。
https://onedrive.live.com/download?resid=E63780628DD96583!3161&authkey=!APl3ReO7T4XIO_s&v=3&ithint=photo,jpg
#include<stdio.h>
#include<conio.h>
main()
{
    char ch;
    int num1, num2, a, m, s;
    float d;
    printf("\nEnter The First Number: ");
    scanf("%d", &num1);
    printf("\nEnter The Second Number: ");
    scanf("%d", &num2);
    a=num1+num2;
    m=num1*num2;
    s=num1-num2;
    d=(float)(num1/num2);
    printf("\nEnter Your Choice: ");
    scanf("%c", &ch);
    switch(ch)
        {
            case 'A': printf("\nThe Addition Of The Number Is= %d", a);
                break;
            case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
                break;
            case 'S': printf("\The Subsraction Of THe Numbers Is= %d", s);
                break;
            case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
                break;
            default : printf("\nInvalid Entry");
                break;
        }
    getch();
    return 0;
}

我错在哪里了???

最佳答案

%c中的scanf之前添加一个空格将解决此问题。这是因为在输入前两个整数的值之后scanf不会使用\n字符。由于enter键(\n)也是一个字符,因此它会被scanf("%c",&ch);使用,从而执行默认大小写。在%c之前的空格将丢弃所有空格和空格。

关于c - 程序在输入之前结束(输入大小写)(重复),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25822622/

10-16 03:33