简体   繁体   English

如果在输入正确输入之前不再次询问,请检查是否在一定限制之间输入用户输入

[英]Checking For user input between certain limit if not asking again until correct input is entered

My program compiles correctly but i am having problem when i run it. 我的程序可以正确编译,但是运行时出现问题。 The first scanf (width) works correctly but when i try with another scanf(height) i get segmentation fault 11. And can i do this program to work without using pointers . 第一个scanf(宽度)可以正常工作,但是当我尝试另一个scanf(高度)时,我会遇到分段错误11。而且我可以在不使用指针的情况下使该程序正常工作吗。 (Also i need limit checker function because i have to use it again and again in my program). (我还需要限制检查器功能,因为我必须在程序中反复使用它)。

#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
    int* x;
    int* y;
    printf("Enter the width of the windows. (3 - 5) : ");
    scanf("%d", x);
    limitChecker(3, 5, x);
    printf("width: %d \n", *x);
    printf("Enter the height of the windows. (2 - 4) : ");
    scanf("%d", y);
    limitChecker(2, 4, y);
    printf("Height: %d \n", *y);

}

void limitChecker(int x, int y, int* input)
{
    while(!(*input>=x && *input<=y))
    {
    printf("Please enter a value between (%d - %d): ",x,y);
    scanf("%d", input);
    }
}

You did not allocate memory to hold x and y . 您没有分配内存来容纳xy

Allocate them on the stack and then use the & address of operator to obtain a pointer to that memory. 将它们分配在堆栈上,然后使用运算符的&地址获取指向该内存的指针。

#include <stdio.h>
int limitChecker(int x, int y, int input);
int main(void)
{
    int x;
    int y;
    printf("Enter the width of the windows. (3 - 5) : ");
    scanf("%d", &x);
    x = limitChecker(3, 5, x);
    printf("width: %d \n", x);
    printf("Enter the height of the windows. (2 - 4) : ");
    scanf("%d", &y);
    y = limitChecker(2, 4, y);
    printf("Height: %d \n", y);

}

int limitChecker(int x, int y, int input)
{
    while(!(input>=x && input<=y))
    {
    printf("Please enter a value between (%d - %d): ",x,y);
    scanf("%d", &input);
    }

    return input;
}

If you want x and y to be pointers then you have to assign them valid memory before you use them. 如果希望xy成为指针,则必须在使用它们之前为其分配有效内存。

int * x = malloc(sizeof(int));
int * y = malloc(sizeof(int));

You need to use a reference to the variables used in the scanf(). 您需要使用对scanf()中使用的变量的引用。

For example, scanf("%d", &x); 例如, scanf("%d", &x);

The first parameter of scanf() is for the type of data, and the following parameter(s) are a list of pointers to where you would like the user input to be stored. scanf()的第一个参数用于数据类型,以下参数是指向要存储用户输入的指针的列表。

CORRECTED CODE: 正确的代码:

#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
    int x;
    int y;
    printf("Enter the width of the windows. (3 - 5) : ");
    scanf("%d", &x);
    limitChecker(3, 5, &x);
    printf("width: %d \n", x);
    printf("Enter the height of the windows. (2 - 4) : ");
    scanf("%d", &y);
    limitChecker(2, 4, &y);
    printf("Height: %d \n", y);

}

void limitChecker(int x, int y, int* input)
{
    while(!(*input>=x && *input<=y))
    {
    printf("Please enter a value between (%d - %d): ",x,y);
    scanf("%d", input);
    }
}
#include <stdio.h>

int limitChecker(int x, int y, int value){
    return x <= value && value <= y;
}

int inputInt(void){
    //x >= 0
    int x = 0;
    int ch;
    while('\n'!=(ch=getchar())){
        if('0'<=ch && ch <= '9')
            x = x * 10 + (ch - '0');
        else 
            break;
    }
    return x;
}

int main(void){
    int x, y;
    do{
        printf("Enter the width of the windows. (3 - 5) : ");
        x = inputInt();
    }while(!limitChecker(3, 5, x));
    printf("width: %d \n", x);
    do{
        printf("Enter the height of the windows. (2 - 4) : ");
        y = inputInt();
    }while(!limitChecker(2, 4, y));
    printf("Height: %d \n", y);
    return 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM