Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with "do" and "switch"

Status
Not open for further replies.

jdonnell

ISP
Sep 16, 1999
2
US
the &quot;do&quot; and &quot;switch&quot;<br>
statements below are not working the way i think they should. if i remove<br>
the do statement the switch works fine. if the do statement is in the<br>
program the switch statement will go to the default everytime except when i<br>
enter 's'. what am i missing<br>
<br>
<br>
// menu template<br>
//bloc<br>
<br>
#include &lt;stdio.h&gt;<br>
#include &lt;conio.h&gt;<br>
<br>
void menu () {<br>
clrscr ();<br>
printf (&quot;E: enter picks\n&quot;);<br>
printf (&quot;V: view picks\n&quot;);<br>
printf (&quot;L: Least picked\n&quot;);<br>
printf (&quot;S: Save and Quit\n&quot;);<br>
}<br>
<br>
<br>
void main (){<br>
char selection;<br>
do {<br>
menu ();<br>
selection = getchar();<br>
switch (selection){<br>
case 'e' :<br>
case 'E' :<br>
printf(&quot;%c&quot;, selection);<br>
break;<br>
case 'v' :<br>
case 'V' :<br>
printf(&quot;%c&quot;, selection);<br>
break;<br>
case 'l' :<br>
case 'L' :<br>
printf(&quot;%c&quot;, selection);<br>
break;<br>
case 's' :<br>
case 'S' :<br>
printf(&quot;%c&quot;, selection);<br>
break;<br>
default:<br>
clrscr ();<br>
printf (&quot;invalid choice\n&quot;);<br>
getchar (); getchar ();<br>
break;<br>
}<br>
}while (selection != 's');<br>
getchar();<br>
getchar();<br>
<br>
}<br>
<br>

 
your code look fine! you need to flush the memory before the line &quot;selection = getchar();&quot; put fflush(stdin) or flushall() function before the &quot;selection = getchar();&quot; then it should work. Question why did you have two getchar after the printf(&quot;invalid choice\n&quot;); one should be enough.<br>
<br>
<br>
Good Luck!
 
There are a couple of logical problems <br>
1) Just use fflush(stdin) before selection=getchar(). Because of this , when you press any thing it goes in the default statement.<br>
<br>
2) Remove one of the getchar() from default statement.<br>
<br>
3)while checking in while , just use tolower(selection) != 's' . so that case sensitiveness should not affect the program .Otherwise it will work for only small letter 's'.<br>
<br>
Does it solves your problem ?<br>
<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A><br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top