如果要继续,如何检查输入不是字母/符号和循环 [英] How to I check input is not alphabet/symbol and loop if want to continue

查看:94
本文介绍了如果要继续,如何检查输入不是字母/符号和循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求:

1.要求用户为数学方程式输入x,y,z的值

2.检查值是否不是字母 - 询问是否要重新输入

3.如果所有输入都是数字则计算答案

3.当用户希望继续下一次输入时循环



问题:

运行它,发现可以输入无限而不执行其他代码。当键入x,y,z的值时



1.当输入为整数时,可以键入无限数量

输入实数X :

输入实数Y:

输入实数Z:

1

1

1

1



2.当字母代码中的密钥保持循环直到printf()继续

输入实数X:

输入实数Y:

输入实数Z:

输入的值不是数字

继续(是/否)? y

输入实数X:

输入实数Y:

输入实数Z:

输入值不是数字

继续(是/否)?





3.之后尝试输入整数然后字母然后整数以某种方式它可以运行到最后



输入实数X:

输入实数Y:

输入实数Z:

输入值不是数字

继续(是/否)? 1

1

1

1

1

1

f

输入值不是数字

继续(是/否)? 1

1

1

X = 1.0000; Y = 1.0000; Z = 1.0000

f(x,y): - 8.0000; g(x,z):0.0000; h(x,y,z): - inf

你想继续Y / N:



我有什么试过:



Requirement:
1. ask user to input value for x,y,z for mathematic equation
2. check the value is not alphabet- ask if want to re input
3. calculate the answer if all the input is numeric
3. loop when user wish to continue for the next input

Problem:
Run it and found out can input infinite without execute other code. when key in value for x,y,z

1. can key in infinite number when the input is integer
Enter real number X:
Enter real number Y:
Enter real number Z:
1
1
1
1

2. when key in alphabet code keep looping until printf() continue
Enter real number X:
Enter real number Y:
Enter real number Z:
Entered value is not numeric
Continue (Y/N)? y
Enter real number X:
Enter real number Y:
Enter real number Z:
Entered value is not numeric
Continue (Y/N)?


3. after that tried to input integer then alphabet then integer somehow it can run to the end

Enter real number X:
Enter real number Y:
Enter real number Z:
Entered value is not numeric
Continue (Y/N)? 1
1
1
1
1
1
f
Entered value is not numeric
Continue (Y/N)? 1
1
1
X=1.0000 ; Y=1.0000 ; Z=1.0000
f(x,y): -8.0000; g(x,z): 0.0000; h(x,y,z): -inf
Do you want to continue Y/N:

What I have tried:

do
    {
        printf("Enter real number X: \n");
        //scanf("%f",&X);
         
        printf("Enter real number Y: \n");
        //scanf("%f",&Y);
         
        printf("Enter real number Z: \n");
        //scanf("%f",&Z);
         
         while(1)
        {
            if (scanf("%f%f%f", &X,&Y,&Z) != 3)
            {
            printf("Entered value is not numeric\n");
            printf("Continue (Y/N)? ");
            scanf(" %c", &choice);
            }
            if ((choice != 'Y') && (choice != 'y')) break;
        }
         
            if(scanf("%f%f%f", &X, &Y, &Z) == 3)
            {
             
   //continue to calculate the equation and print result
            }

推荐答案

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



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

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

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



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


这篇关于如果要继续,如何检查输入不是字母/符号和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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