code help


 
Thread Tools Search this Thread
Top Forums Programming code help
# 1  
Old 10-12-2002
code help

why does this code present me the menu twice everytime i chose an option? i think it has something to do with the while loop.

#include <stdio.h>
void main(){
char choice;
int enter =1;
while(enter ==1){
printf("choose from options\n");
printf("a do something\n");
printf("b do something else\n");
printf("c quit\n");

choice = getchar();
switch(choice){
case 'a':
printf("apply a");
break;
case 'b':
printf("apply b");
break;
case 'c':
enter=0;
break;
default:
printf("something");

}
}
}

Last edited by bb00y; 10-12-2002 at 05:27 AM..
# 2  
Old 10-12-2002
What help do u need???
# 3  
Old 10-12-2002
sorry. everytime i run this code, enter an option, the menu of options always display twice before i could choose the option again. for some reason, the first time through it doesn't let me enter an option, like it totally disregard the getchar() statement. it go to the dafault case, and start over the loop, and then it let me enter an option.
# 4  
Old 10-12-2002
That is because the last time you made a choice and hit [ENTER], the [ENTER] key was also stored in the input buffer along with your choice. And this [ENTER] key code was retrieved the next time you called getchar(). To avoid this, replace your getchar() line with:
Code:
while((ch = getchar()) != '\n');

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Linux

Go to a line of code , skip few lines of code

Hi , I have a code where i am using a infinite while loop . some thing like below while do if then #go to line 20 fi command 1; command 2; #line 20: sleep 34; (5 Replies)
Discussion started by: Paarth
5 Replies

2. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

3. UNIX for Dummies Questions & Answers

If ‘922’ Code does not exist on ‘03’ Record, ‘901’ Code will be there instead, move ‘03’ R

01,011600033,011600033,110516,0834,2,90,,2/ 02,011600033,011103093,1,110317,0834,,2/ 03,105581,,015,+00000416418,,,901,+00000000148,,,922,+000000 00354,,/ 03,113806,,015,+00000559618,,,901,+00000000096,,,922,+000000 00621,,/ 88,902,+0000000025218,,/... (1 Reply)
Discussion started by: sgoud
1 Replies

4. Shell Programming and Scripting

translate ksh code to csh code

hi all, Can any 1 help me translate this korn shell code to C shell code : email=$(grep "^$1" $folder/config_2.txt | awk '{print $2'}) In config_2.txt the content is : which mean in korn shell , $1=groupname and $2=email address. Now i need to write in C shell script,when i set the... (2 Replies)
Discussion started by: proghack
2 Replies

5. Programming

how i prepare a c++ code(c code) for implementing my own protocol format

helo my protocol format is given below { destno,mode,no.of packet,pktsize,,pktno,textsize,CRC} description:- { is starting flag destno - 4bytes mode - 1 byte no.of pkt - 4byes pktsize - 6 bytes ... (1 Reply)
Discussion started by: amitpansuria
1 Replies

6. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies
Login or Register to Ask a Question