user4955112
user4955112

Reputation:

Loop in Function

I am coding a function to validate a number inserted by the user (between 1 and 3) and it doesn't matter what number I put the program crashes.

Here's my code:

    int validate(int low, int high) {
    int selection;

    do {
    scanf("%d", selection);
    if (selection < low || selection > high)
        printf("Invalid Input, try again: ");
    } while (selection > low || selection < high);

    return selection;
}

Anyone any idea, please?

Upvotes: 0

Views: 35

Answers (2)

makdu
makdu

Reputation: 946

While reading scanf, use & with the variable

 int validate(int low, int high) {
    int selection;
     printf("Enter your Selection ");
      do {
            scanf("%d", &selection);
            if (selection < low || selection > high)
           {
                printf("Invalid Input, try again: ");
          }
       } while (selection < low || selection > high) ;

        return selection;
    }

Upvotes: 1

Ejaski
Ejaski

Reputation: 199

2 bugs I see.

  1. makdu, mention the scanf issue.

  2. your while loop should use the && operator.

    } while (selection > low && selection < high);

Upvotes: 0

Related Questions