so i've been working on code 2d treasure hunt use single char's scanf , whether n,s,w,e specific output. unfortunately code not listening input, example, when type in n, nothing happens. wondering if guys point me onto right path
do { printf("please press n, s, w, or e find treasure\n"); scanf(" %c", &n); if (scanf(" %c", &n) == 'n') { current - 11; new = current - 11; if (abs(potofgold - current) < abs(potofgold - new)) printf("getting warmer\n"); else if (abs(potofgold - current) > abs(potofgold - new)) printf("getting colder\n"); else printf("hooray\n"); current = new; } else if (scanf("%c", &s) == 's') { current + 11; new = current + 11; if (abs(potofgold - current) < abs(potofgold - new)) printf("getting warmer\n"); else if (abs(potofgold - current) > abs(potofgold - new)) printf("getting colder\n"); else printf("hooray\n"); current = new; } else if (scanf("%c", e) == 'e') { current + 1; new = current + 1; if (abs(potofgold - current) < abs(potofgold - new)) printf("getting warmer\n"); else if (abs(potofgold - current) > abs(potofgold - new)) printf("getting colder\n"); else printf("hooray\n"); current = new; } else (scanf("%c", &w) == 'w'); { current - 1; new = current - 1; if (abs(potofgold - current) < abs(potofgold - new)) printf("getting warmer\n"); else if (abs(potofgold - current) > abs(potofgold - new)) printf("getting colder\n"); else printf("hooray\n"); current = new; } } while (current != potofgold); return 0;
take input first scanf("%c", &variable)
above if
condition. use if (n == 'n')
instead of if (scanf(" %c", &n) == 'n')
each if condition. because scanf
return number of input, can take successfully
Comments
Post a Comment