i have a a switch statement with about 10 cases. What i want the program to do is pretty much run the entire program again. I tried not putting in the break at the end of each case, but the program ends up doing all the cases. n
i have a a switch statement with about 10 cases. What i want the program to do is pretty much run the entire program again. I tried not putting in the break at the end of each case, but the program ends up doing all the cases. n
do u mean soemthing like this
Sampe code
ssharish2005Code:#include<stdio.h> int main() { int ch; printf("1. Add values\n"); printf("2. Sub values\n"); printf("3. Mul Values\n"); printf("4. Exit\n"); printf("\nEnter your choice\n"); scanf("%d",&ch); while(ch!=4) { switch(ch) { case 1: /*do something here*/ break; case 2: /* do something here*/ break; case 3: /* do soemthing here */ break; } printf("1. Add values\n"); printf("2. Sub values\n"); printf("3. Mul Values\n"); printf("4. Exit\n"); printf("\nEnter your choice\n"); scanf("%d",&ch); } getchar(); return 0; }
Last edited by ssharish2005; 11-20-2005 at 10:30 PM.
Nice indentation, ssharish2005! And did you ever consider using a do-while loop or something to eliminate the duplicate code?
I would re-write the above code to look like this:
Code:#include <stdio.h> int main(void) { int ch, c while(ch != 4) { printf("1. Add values\n"); printf("2. Sub values\n"); printf("3. Mul Values\n"); printf("4. Exit\n"); printf("\nEnter your choice\n"); scanf("%d",&ch); while((c = getchar()) != '\n' && c != EOF); switch(ch) { case 1: /*do something here*/ break; case 2: /* do something here*/ break; case 3: /* do something here */ break; default: break; } } getchar(); return 0; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
ch contains the garbage value.It may contain 4.Code:int main(void) { int ch, c while(ch != 4)
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
I'd use dwks' solution but with a do-while loop instead to eliminate the chance that ch starts off with the value 4:
Code:do { // blah blah blah } while(ch != 4);
If you understand what you're doing, you're not learning anything.
Oops, yeah, I forgot a semicolon and stuff, the line was supposed to be like this:
Code:int ch = 0, c;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.