Wt happened is just i have made a very simple menu driven programme and in that programme wen i use character not matching wid d switch cases , it runs twice...i want it to run once only..i know its becoz getchar(); but y???

#include<stdio.h>
#include<conio.h>
void even_odd();
int n;
void main()
{
char ch;
clrscr();
do
{


printf("\n1. please print hi");
printf("\n2. please print hello");
printf("\n3. exit d programme");
printf("\n enter ur choice");
printf("\n");
ch=getchar();

}while(ch!='1'&& ch!='2'&& ch!='3');

switch(ch)
{
case '1':
printf("enter a number");
scanf("%d", &n);
even_odd();
break;

case '2':
printf("hello");
break;
case '3':
exit(1);
}
getch();
}

void even_odd()
{
if(n%2==0)
printf("its even");
else
printf("its odd");
}

Some important things you should pay attention to:

Comments
Works for me :)

Wt happened is just i have made a very simple menu driven programme and in that programme wen i use character not matching wid d switch cases , it runs twice...i want it to run once only..i know its becoz getchar(); but y???


#include<stdio.h>
#include<conio.h>
void even_odd();
int n;
void main()
{
char ch;
clrscr();
do
{


printf("\n1. please print hi");
printf("\n2. please print hello");
printf("\n3. exit d programme");
printf("\n enter ur choice");
printf("\n");
ch=getchar();

}while(ch!='1'&& ch!='2'&& ch!='3');

switch(ch)
{
case '1':
printf("enter a number");
scanf("%d", &n);
even_odd();
break;

case '2':
printf("hello");
break;
case '3':
exit(1);
}
getch();
}

void even_odd()
{
if(n%2==0)
printf("its even");
else
printf("its odd");
}

What??? i cant get u

What??? i cant get u

Well, if you can't get me, then you probably can't get someone else who's trying to help you, so it's probably the best that you don't waste your time here.

In order to get help, you first have to help us to help you, you can do this for instance by using code tags.

Information about code tags is spread all over the website:

1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in!

OK!!
I have just made a simple menu driven programme. using switch case..
When i enter 4 for instance, it should just prompt me again with the menu once but it does this twice..Her's my code: sorry last time again i used the code a bit earlier..

#include<stdio.h>
#include<conio.h>
void even_odd();
int n;
void main()
{
char ch;
clrscr();
do
{


printf("\n1. please print hi");
printf("\n2. please print hello");
printf("\n3. exit d programme");
printf("\n enter ur choice");
printf("\n");
ch=getchar();

}while(ch!='1'&& ch!='2'&& ch!='3');

switch(ch)
{
case '1':
printf("enter a number");
scanf("%d", &n);
even_odd();
break;

case '2':
printf("hello");
break;
case '3':
exit(1);
}
getch();
}

void even_odd()
{
if(n%2==0)
printf("its even");
else
printf("its odd");
}

getchar() will only get one character, which means that when you enter a character and press the ENTER button on your keyboard, the ENTER is not read (which means it stays in the input stream, available for the next read operation), so the following time when you invoke the getchar() function, an ENTER will be read because it was left in the input stream.
What you need to do is read the ENTER away from the input stream, right after you read the user's choice:

do
{
  printf("\n1. please print hi");
  printf("\n2. please print hello");
  printf("\n3. exit d programme");
  printf("\n enter ur choice");
  printf("\n");
  ch=getchar();
  
  [B]char c; // Added this line[/B]
  [B]while( (c = getchar()) != '\n'); // Added this line [/B]
} while(ch!='1'&& ch!='2'&& ch!='3');

EDIT:: I changed the above code to read everything after the user's choice away, until a newline is encountered.

Yea..Thanks..and i think i can instead use getche(); or getch(); as well if i don't want to add that line..
Right!!?

Yea..Thanks..and i think i can instead use getche(); or getch(); as well if i don't want to add that line..
Right!!?

Well, you could do that, but it's generally not recommended because getch(), getche(), and all those other functions which are part of the conio-library are unportable, which means that if someone else tries to compile your code and if your code makes use of conio, it could be that his compiler refuses to compile the code because it doesn't ship with the conio-library.

Another remark about your code: if you use getchar() to get the user's choice, only the first entered character is significant for the choice, for example when the user enters: 3287, your program will interpret this as choice 3.

This question has already been answered. Start a new discussion instead.