C program always uses 1503657284 or -1863345812 as entered scanf choice -



C program always uses 1503657284 or -1863345812 as entered scanf choice -

i learning c programming. in next programme have 6 choices. user makes 1 of 6 choices , programme prints information. goes on until user says quit. when execute programme choice 1503657284 programme exits fine when press 6. using eclipse on fedora.

the code here.

#include <stdio.h> #include <stdlib.h> int main(void) { int selection = 0; printf("welcome program\n"); while(choice != 6) { printf( "1. selection 1\n" "2. selection 2\n" "3. selection 3\n" "4. selection 4\n" "5. selection 5\n" "6. quit\n" "please come in choice: \n"); scanf("%d", &choice); printf("you made %d choice\n", &choice); switch(choice) { case '1' : printf("choice 1\n"); break; case '2' : printf("choice 2\n"); break; case '3' : printf("choice 3\n"); break; case '4' : printf("choice 4\n"); break; case '5' : printf("choice 5\n"); break; case '6' : printf("choice 6\n"); break; default : printf("choice not found\n"); break; } } printf("you have made out"); homecoming exit_success; }

when run programme following.

welcome program. 1. selection 1 2. selection 2 3. selection 3 4. selection 4 5. selection 5 6. quit please come in choice: 4 made -1863345812 selection choice not found 1. selection 1 2. selection 2 3. selection 3 4. selection 4 5. selection 5 6. quit please come in choice: 2 made -1863345812 selection choice not found 1. selection 1 2. selection 2 3. selection 3 4. selection 4 5. selection 5 6. quit please come in choice: 6 made -1863345812 selection choice not found have made out

thanks.

there 2 problems in code:

first said printing memory address of variable choice , not choice itself.so alter printf statement to:

printf("you made %d choice\n", choice);

secondly , create switch wrong cases: have utilize type want match , in case int choice , , not char. alter cases numbers , not characters:

case 1 : ...

instead of

case '1' :...

c

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -