473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

when i writes my program when once i started it & puts value, it closed very fast

3
hi i am new to use borland i writes my following program and when i run my program the cmd window closes very fast automatically?


#include <stdio.h>
void main(void)
{
char name;
printf ("\nEnter The First Letter Of Your Name: ");
scanf("%c",& name);
switch(name)
{
case 'M':
printf("\nName: Moeez Akbar");
printf("\nAge: 24 Years");
printf("\nCompany: BA");
break;
case 'MD':
printf("\nName: Moeed Akbar");
printf("\nAge 23 Years");
break;
case'U':
printf("\nName: Omar Akbar");
printf("\nAge 21 Years");
break;
case'H':
printf("\nName: Hamza Akbar");
printf("\nAge 18 Years");
break;
default:
printf("\nNo Record Found");
}
getchar();
}
Jun 17 '15 #1
4 1295
weaknessforcats
9,208 Expert Mod 8TB
What happens is you enter the first letter of the last name, like M. When you d this you type two keys. The M. The enter key.

You tell scanf %c so it puts the M in the name variable and returns. That leaves the enter key still in the input buffer and that is what getchar() sees. It thinks you just pressed the enter key. The effect is that your program finishes in the blink of an eye.

You must deal with that enter key after scanf completes and before the getchar() is called.
Jun 17 '15 #2
Um Ak
3
Dear weaknessforcats i m very thankful to u for your reply...sir can u please tell me that what do you mean by enter key after scanf completes and before the getchar() is called
can you give me answer of this line in detail ?
Jun 17 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
OK. When you code:

Expand|Select|Wrap|Line Numbers
  1. scanf("%c",& name);
  2.  
the scanf function sees the %c and knows you want one character.

However, scanf lets you type characters until you press the enter key. Then it processes those characters and returns to your main().

So, in the case of entering, say an M, you must press the enter key for scanf to know you are finished entering data. The %c says to take one byte and put it in your name variable. That would be the M. But the enter key character is still in the input stream.

The next function to use input is getchar(). It waits until you press the enter key but this time the enter key character is the one left over from the scanf input. It's already there. getchar() doesn't know (and doesn't care) how the enter key got there. It merely takes input data, and if it is an enter key character, it returns immediately.

An easy fix is add a call to getchar() right after the call to scanf(). That getchar() call would use the enter key left over from the scanf. Your program would then process and reach the second getchar() at the end of the program where the program would halt until you press the enter key again.
Jun 17 '15 #4
Um Ak
3
hmm okay thanks sir it works
Jun 21 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sims | last post by:
Hi, I am trying to tidy up my code and now i am looking at my database work. Now correct me if i am wrong, (I am using mysql_connect). Lets assume a few functions... ...
27
by: Jason Shohet | last post by:
I have an ascx control, and it needs a value called 'orgID' which the parent page will have. I want to avoid the parent page setting values in fields on the ascx control since this violates...
2
by: Ben Amada | last post by:
I'm a little confused about in what Event should I add dynamic controls and in what Event should I retrieve the value of a dynamic control on postback. I've found that adding dynamic controls in...
22
by: Mike Polinske | last post by:
I am new to the C programming language but have been programming in Cobol for over 10 years. When I compile the following code, it compiles clean but I get an application error both under Windows...
1
by: TheRain | last post by:
I'm working on a .NET C++ project. The program loads and creates a tray icon, but is not visible on the screen. So what I'm looking for is a way to detect when a specific key combination is...
1
by: pkirk25 | last post by:
I have a program written in C++ with STL that does work. If I double click on the executable, it examines a data source and write a report to a file and writes useful messages to cout. I want...
1
by: babu198649 | last post by:
hi if we new more than once the same object does it allocates memory multiple times.
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
4
by: JDS | last post by:
I have an application that interfaces with a USB device using the .Net serial port. The code works fine, displaying live data on the screen; that is until the USB lead is pulled out from the PC...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.