Sinisterly
what's wrong with my code - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: what's wrong with my code (/Thread-what-s-wrong-with-my-code--58205)

Pages: 1 2


what's wrong with my code - wolfking92 - 08-08-2014

ok i'm learning C programming and when i tried to do an excercise
withe the switch statement for a game meneu it didn't work here is the source code
Code:
#include<stdio.h> #include<conio.h> int main () { int select; printf("1)Start the game \n2)Options \n3)About game \n4)Exit the game\n"); scanf("Press a number %d", &select); switch (select) { case '1': printf("Loading..."); break; case '2': printf("1)Sound \n2)Controle \n3)Graphics"); break; case '3': printf("This game was created by Mohamed OMARI\nhe takes all the credit."); break; case '4': printf("Are you sure \nPress any key to continue..."); break; default: printf("Wrong section"); break; } getch(); return 0; }



RE: what's wrong with my code - Snipa - 08-08-2014

okay i noticed something, i tried to print the value of "select" , turns out it's not what you want it to be "1" "2" "3" "4" that's why your program jumped directly to the default section.

i removed the text written with the scanf,and modified the case statement.

you made it to check if the input is a char by using single quotation '1' instead of simply 1 since its an integer

Code:
#include<stdio.h> #include<conio.h> int main () { int select; printf("1)Start the game \n2)Options \n3)About game \n4)Exit the game\nPress a number "); scanf("%d", &select); switch (select){ case 1: printf("Loading..."); break; case 2: printf("1)Sound \n2)Controle \n3)Graphics"); break; case 3: printf("This game was created by Mohamed OMARI\nhe takes all the credit."); break; case 4: printf("Are you sure \nPress any key to continue..."); break; default: printf("Wrong section"); break; } getch(); return 0; }



RE: what's wrong with my code - wolfking92 - 08-08-2014

(08-08-2014, 09:10 AM)Snipa Wrote:

i removed the text written with the scanf,and modified the case statement.

you made it to check if the input is a char by using single quotation '1' instead of simply 1 since its an integer
thanks i tried it without the single quotasion as they are integer but it didn't work but now after that you deleted the written with the scan it worked like a charme thanks for that but why it didn't work with the written



RE: what's wrong with my code - alok9shm - 08-08-2014

1. case '1' = printf("Loading"); should have 1 without any quotes as @Snipa pointed out.

2. You are making the flow of the program to break right after case 1.
After the statement in case 1 is executed, the control does not pass to next elements of your game, as you have used a break statement, which pulls the program control away from the switch case.

And yes, the idea of this game is wrong. The main screen gives you 4 Option:
  1. Start Game
  2. Options
  3. About game
  4. Exit

Now, when Option 1 is selected, It prints out "Loading..." and the program exits. So, you can do 1 thing. write some simple interactive shit inside case 1 and then make the control pass to the Menu once again. An infinite loop, that will exit only when Option 4 is selected.


RE: what's wrong with my code - Psycho_Coder - 08-08-2014

It won't work because scanf takes data from stdin and formats them to the specified types. More details here : http://www.cplusplus.com/reference/cstdio/scanf/

Also you have declared select as int but in switch case you're using them as character and hence they are showing errors.

If you want them to work as characters then instead of int select; use : char select;

and when taking input the statement should be like :-

scanf("%c",&select);


EDIT : Its a bad programming practice to use getch() since it doesn't works everywhere.


RE: what's wrong with my code - Snipa - 08-08-2014

(08-08-2014, 09:32 AM)wolfking92 Wrote: thanks i tried it without the single quotasion as they are integer but it didn't work but now after that you deleted the written with the scan it worked like a charme thanks for that but why it didn't work with the written

simply because scanf is for getting input


RE: what's wrong with my code - wolfking92 - 08-08-2014

thanks guys @alok9shm i'm just a begginer i know that much yet maybe in the future after i learn about the reapeat loops that are in the programme i'm following thanks any way

Quote:simply because scanf is for getting input

well that makes sense :troll: thanks :Grin:


RE: what's wrong with my code - Snipa - 08-08-2014

You are welcome, hope you keep your motivation to programming
See you around

/marked as solved



RE: what's wrong with my code - alok9shm - 08-08-2014

(08-08-2014, 09:49 AM)wolfking92 Wrote: thanks guys @alok9shm i'm just a begginer i know that much yet maybe in the future after i learn about the reapeat loops that are in the programme i'm following thanks any way



well that makes sense :troll: thanks :Grin:

And do not get confused regarding the cases.
You missed using another switch case for the option no 2.
You need to practice some more of control structure.

And for your simplicity, keep the program simple. Use multiple printf statements instead of using a single one with multiple new-line characters. It will be less confusing for you, while you're learning. That way you can also design the flow of control using switch cases easily.


RE: what's wrong with my code - wolfking92 - 08-08-2014

@Snipa yes i'm very motivated even that i have read it (it's an E-book) , because i need it in my studies (embedded system) and to back on the track in the hacking world