【发布时间】:2021-06-19 17:27:34
【问题描述】:
这是我学习 C 编程的第一个学期,我试图将用户输入限制为 22 位有符号整数,并且如果命令由于某种原因不起作用 我尝试用 else 更改第二个 if 语句,但没有做任何事情
#include <stdio.h>
#include <stdlib.h>
void main ( int argc, char *argv[] )
{
int first;
int second;
char menu_choice;
if(first>=2097150|| first<=-2097150|| second<=-2097150||second>=2097150)
{
printf("please choose a number between -2097150 and 2097150");
return(0);
}
if(first<=2097150|| first>=-2097150|| second>=-2097150||second<=2097150)
{//rest of the code goes here but that is not part of the problem
}
【问题讨论】:
-
first在您测试它的时候是不确定的。没有输入。 -
我没有简单的解决方案来限制输入为 22 位,但是你可以限制读取的位数:
scanf("%6d", &n); -
@Amadan first 和 second 在运行 ./a.exe(23,34 或其他)时输入
-
如果您键入
./a.exe 23 34,它们将在argv中作为字符串提供;first和second对此一无所知。您需要转换它们,并将值分配给first和second;这应该可以解决问题:first = atoi(argv[1]); second = atoi(argv[2]); -
最简单的方法是将
int first; int second;更改为int first = atoi(argv[1]); int second = atoi(argv[2]);,然后 first 和 second 将具有值。
标签: c