简体   繁体   English

我如何声明一个 int 类型的输入变量,例如。 输入 x(0

[英]How can i declare a int type input variable eg. take a input x(0<x<1000) in c?

I try to use this code我尝试使用此代码

#include <stdio.h>
int main() {
    int x,y,z;

    scanf("%d%d", &x, &y);
    z = x + y;
    printf("%d", z);
    return 0;
}

The problem is: Two man have x and y dollar.问题是:两个人有 x 和 y 美元。 Take input of x(0 < x < 10000) and y(0 < y < 10000) and print the summation of their dollars.输入x(0 < x < 10000)y(0 < y < 10000)并打印它们的总和。

You cannot put restriction on user entering any value.您不能限制用户输入任何值。 But you can ask user to re-enter the values again in specified limit with little modification in your code.但是您可以要求用户在指定的限制内再次重新输入值,而无需对代码进行少量修改。

scanf("%d%d", &x, &y);
while( x <= 0 || y <= 0 || x >= 10000 || y >= 10000) {
    // Suitable message for re-entry of numbers.
    scanf("%d%d", &x, &y);
}
z = x + y;
printf("%d", z);

There's no standard way to do a range check as part of an input operation in C - IOW, you can't tell scanf to reject an integer input that's outside of a specific range.作为 C - IOW 中输入操作的一部分,没有标准方法可以进行范围检查,您不能告诉scanf拒绝特定范围之外的整数输入。 You will have to add a range check separately:您必须单独添加范围检查:

if ( scanf( "%d%d", &x, &y ) == 2 ) // make sure both x and y are read
{
  if ( 0 < x && x < 1000 && 0 < y && y < 1000 )
  {
    z = x + y;
    printf( "z = %d\n", z );
  }
  else
  {
    printf( "Both x and y must be between 0 and 1000\n" );
  }
}
else
{
  printf( "At least one bad input for x or y\n" );
}

You cannot restrict scanf to not to get an undesired integer, but you can re-take the input from the user if the value entered is bigger than 10k or smaller than 0. That would be a simple solution.您不能将scanf限制为不获取不需要的整数,但是如果输入的值大于 10k 或小于 0,您可以重新获取用户的输入。这将是一个简单的解决方案。

scanf("%d%d", &x, &y);

if( x > 10000 || x <= 0) {
 printf("Please re-enter a value for x: \n");
 scanf("%d", &x);
 }

if( y > 10000 || y <= 0) {
 printf("Please re-enter a value for y: \n");
 scanf("%d", &y);
 }

If you want you can also make a termination scenario by using "End of File" ( EOF ).如果需要,您还可以使用“文件结尾”( EOF )来制作终止场景。 Putting this here for source.把这个放在这里作为来源。 How to use EOF 如何使用EOF

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个整数放入 int* 选项卡; 在两个不同的循环中,用 0 分隔,例如。 [1,1,1,0,1,1] 在 VS 2019 中 C - How do I put multiple integers into int* tab; in two different loops separated by 0 eg. [1,1,1,0,1,1] in VS 2019 in C 如何在C中以整数类型数组作为输入的前缀? 当我使用%[^ \\ n]输入字符串时 - How can i prefixed to take input in an integer type array in c? When i use %[^\n] in case of string input 我怎样才能在c中输入多个字符串? - How can i take multiple string input in c? 在 C 中调用 execv(“/bin/sh”, NULL) 时,如何与子进程通信(例如,通过写入运行命令)? - how can I communicate (eg. run commands via write) with a child process when execv(“/bin/sh”, NULL) was called in C? 如何将输入的电话号码存储到 int 数组中? C - How can I store a phone number input into a int array? C 为什么在C的字符串输入中使用类型为int的变量? - Why would a type int variable be used in a string input in C? 如何接受具有以小数点分隔的数字的用户输入并将该输入存储为不带小数点的int? - How can I take in user input that has a number separated by a decimal point and store that input as an int without the decimal point? 如何读取变量*(int *)x? - how to read the variable *(int*)x? 为什么我可以使用'static'限定符声明一个没有类型的变量? (在C中) - Why can I declare a variable with 'static' qualifier without type? (in C) 如何将int和double用作输入而不是char? - How to take int and double as input but not a char?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM