the problem in function. it's supposed validate input 2 variables integers. did wrong? o__o :)
i used if else statement check change in variable valid exit loop once right input given. if input right values, 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 whole code reference. answers earlier able fix crash. now, either loop doesn't exit or 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 %d\n", ret); break; case '2': ret = subtract(n1, n2); printf("the difference %d\n", ret); break; case '3': ret = multiply(n1, n2); printf("the product %d\n", ret); break; case '4': ret = divide(n1, n2); printf("the quotient %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; }
change
if(scanf("%d", a) != 0)
to
if(scanf("%d", &a) == 1) // ^^^^ right check // ^^^ missing &
scanf
return eof
if fails assign first receiving argument. in case, return 1
if data read &a
.
similarly, change
if(scanf("%d", b) != 0)
to
if(scanf("%d", &b) == 1) // ^^^ ^^^^
Comments
Post a Comment