Thursday, July 9, 2009

Is this C program code correct?

#include%26lt;stdio.h%26gt;


void main()


{


int choice;


printf("Enter 1,2,3\n");


scanf("%d",%26amp;choice);


switch(choice)


{


case 'a':


printf("You selected choice 1\n");


break;


case 'b':


printf("You selected choice2\n");


break;


case 'c':


printf("You selected choice 3\n");


break;


default:


printf("ERROR\n");


break;


}


getchar();


}

Is this C program code correct?
No. Start with changing void main to int main.





Is there any reason it isn't correct? Hey, you want to know if your C code is correct, that's what your *compiler* is for. Compiler giving you errors? Try working through them, and if not, post the errors here (verbatim).





FWIW: I see two problems. First is void main. Second is int choice, but you are, in your switch statement, using case a, b, c, rather than 1, 2, 3.
Reply:You code is correct. void main or int main all are ok. But I got something. Why you ask the person to enter the number 1, 2 or 3. That’s ok to use scanf with %d and keep value in choice variable (int).





Then, you use switch…case. You have to change the case from yours to be mine for working well. See the following code.





#include%26lt;stdio.h%26gt;


void main()


{


int choice;


printf("Enter 1,2,3\n");


scanf("%d",%26amp;choice);


switch(choice)


{


case 1:


printf("You selected choice 1\n");


break;


case 2:


printf("You selected choice2\n");


break;


case 3:


printf("You selected choice 3\n");


break;


default:


printf("ERROR\n");


break;


}


getchar();


}





Because you keep value as integer variable, you must use the case like this.





By the way, if you keep the user input value into char, you can use case with ‘1’ or ‘2’ or ‘3’. See the following code.





#include%26lt;stdio.h%26gt;


void main()


{


char choice;


printf("Enter 1,2,3\n");


scanf("%c",%26amp;choice);


switch(choice)


{


case '1':


printf("You selected choice 1\n");


break;


case '2':


printf("You selected choice2\n");


break;


case '3':


printf("You selected choice 3\n");


break;


default:


printf("ERROR\n");


break;


}


getchar();


}





Astalavista :)
Reply:Yes. your program is free from errors..


but u have written "enter 1 2 3" in printf statement.


you have to change your case statements








#include%26lt;stdio.h%26gt;


void main()


{


int choice;


printf("Enter 1,2,3\n");


scanf("%d",%26amp;choice);


switch(choice)


{


case 1:


printf("You selected choice 1\n");


break;


case 2:


printf("You selected choice2\n");


break;


case 3:


printf("You selected choice 3\n");


break;


default:


printf("ERROR\n");


break;


}


getchar();


}














As you have declared variable 'choice' as integer.you need not give in single quotes(')


for eg:


case '1':
Reply:Use tools as far as possible:


URL IS:


http://www.groovyweb.uklinux.net/index.p...





Warner Geek Squad. Ten Dollars.
Reply:wow, you've got your data types mixed-up...





anyway, change the lines (for example)





...


case 'a':


....


case 'b':


....


case 'c':


....





change them to something like





....


case 1:


....


case 2:


....


case 3:


....








Explanation:





When you asked the user to "Enter 1, 2, or 3", the user will most likely enter any of the 3 numbers you mentioned. Now since you used an integer (see line "int choice;" for the variable that will hold the "choice". You will need to check whether the value entered is either 1, 2, or 3 (without quotation marks).





A single quotation mark signifies a character, but then again, if your user enters 1, 2, or 3, the correct way to test them using case is " case '1': " and so on.





You may also use getch() instead of getchar() [ although i don't remember if indeed there is a getchar() ]. getch() will practically wait for the user to key in any character from the keyboard, but will not echo what the user types. This is useful to allow a stop before finishing the program. (you can add a printf before the getch() which may say, "Press any key to end program."





Added: void main() is okay since you don't return any value (notice no "return 0;" or some similar line).


No comments:

Post a Comment