0

The problem is in this function. It's supposed to validate input for two variables as integers. What did I do wrong? O__O Thanks :)

I used an if else statement to check for change in the variable valid so that it will exit the loop once the right input is given. Even if I input the right values, the code still crashes.

void input(int *n1, int *n2, char *opt)
{
    int valid = 0;
    int v2 = 0;
    char choice;
    int a, b;


    while (v2  == 0)
    {
        printf("Enter first number: \n");
        if(scanf("%d", &a) == 1)
        {
            while(v2 == 0)
            {
                printf("Enter second number: \n");
                if(scanf("%d", &b) == 1)

                {
                    v2 =1;
                    getchar();
                }
                else
                {
                    getchar();
                    printf("Invalid input!\n");
                }
            }
getchar();          
        }
        else
        {
            getchar();
            printf("Invalid input!\n");
        }
    }



    while( valid == 0)
    {
        printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
        scanf("%c", &choice);
        if (choice == 'r' || choice == 'e')
        {
            choice = toupper(choice);
        }
        if ((choice  == '1') ||  (choice  == '2') ||  (choice  == '3') ||  (choice  == '4') ||  (choice  == 'R') ||  (choice  == 'E'))  
        {
            valid = 1;
        }
        else
        {
            printf("Invalid input!\n\n");
        }
    }
    *opt = choice;
    *n1  = a;
    *n2  = b;
}

Here's the whole code for reference. The answers earlier were able to fix the crash. Now, either the loop doesn't exit or it doesn't work right.

#include <stdio.h>
#include <ctype.h>

int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);
void input(int *n1, int *n2, char *opt);


int main(void)
{
    int n1, n2, ret;
    char opt;

    start:
    input(&n1, &n2, &opt);

    switch(opt)
{
            case '1': 
                ret = add(n1, n2);
                printf("The sum is %d\n", ret);
                break;
            case '2':
                ret = subtract(n1, n2);
                printf("The difference is %d\n", ret);
                break;
            case '3': 
                ret = multiply(n1, n2);
                printf("The product is %d\n", ret); 
                break;              
            case '4': 
                ret = divide(n1, n2);
                printf("The quotient is %d\n", ret);
                break;
            case 'R':
                goto start;
                break;
            case 'E':
                printf("Goodbye!\n");
                return 0;
                break;
    }
    goto start; 
}


void input(int *n1, int *n2, char *opt)
{
    int valid = 0;
    int v2 = 0;
    char choice;
    int a, b;


    while (v2  == 0)
    {
        printf("Enter first number: \n");
        if(scanf("%d", &a) == 1)
        {
            while(v2 == 0)
            {
                printf("Enter second number: \n");
                if(scanf("%d", &b) == 1)

                {
                    v2 =1;
                    getchar();
                }
                else
                {
                    getchar();
                    printf("Invalid input!\n");
                }
            }
getchar();          
        }
        else
        {
            getchar();
            printf("Invalid input!\n");
        }
    }



    while( valid == 0)
    {
        printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
        scanf("%c", &choice);
        if (choice == 'r' || choice == 'e')
        {
            choice = toupper(choice);
        }
        if ((choice  == '1') ||  (choice  == '2') ||  (choice  == '3') ||  (choice  == '4') ||  (choice  == 'R') ||  (choice  == 'E'))  
        {
            valid = 1;
        }
        else
        {
            printf("Invalid input!\n\n");
        }
    }
    *opt = choice;
    *n1  = a;
    *n2  = b;
}


int add(n1, n2)
{
    int result;
    result = (n1+n2);
    return result;
}

int subtract(n1, n2)
{
    int result;
    result = (n1-n2);               
    return result;
}

int divide(n1, n2)
{
    int result;
    result = (n1/n2);
    return result;
}

multiply(n1, n2)
{
    int result;
    result = (n1*n2);
    return result;
}
4
  • Did you try to run this inside a debugger? Commented Sep 26, 2015 at 7:36
  • If you mean like in cmd, yes. I'm trying to find it out myself too. :D Commented Sep 26, 2015 at 8:16
  • No, I mean using a debugger: en.wikipedia.org/wiki/Debugger Commented Sep 26, 2015 at 8:18
  • 1
    Hmmm... Not yet. I'll try it now. THanks Commented Sep 26, 2015 at 8:19

2 Answers 2

2

Change

    if(scanf("%d", a) != 0)

to

    if(scanf("%d", &a) == 1)
                   //  ^^^^ This is the right check
           //     ^^^ Missing &

scanf return EOF if it fails to assign to the first receiving argument. In this case, it will return 1 if data was successfully read into &a.

Similarly, change

            if(scanf("%d", b) != 0)

to

            if(scanf("%d", &b) == 1)
                       // ^^^  ^^^^
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It now accepts the value but the loop wont exit
0

Instead of

        if(scanf("%d", a) != 0)

You should use

        if(scanf("%d", &a))

Scanf could return 0,1 or EOF out of which only 1 indicates no error in input! However if your a was a pointer to some integer address location you could have used the former code.Change it for inputting b as well

1 Comment

Oops. I think I missed that. But the loop wont exit now even if it accepts the values :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.