Solved

How do I get the scanf to accept a RETURN as the Default choice switch.

Posted on 2004-04-07
9
371 Views
Last Modified: 2010-04-15
I have a program that uses switch ie.

Enter 1 for this
Enter 2 for this
Enter 3 for this

How do I get the scanf to accept a RETURN as the choice?

Enter return for choice 4.

0
Comment
Question by:scuzz1
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10775210
Do this:

int main()
{
 char c;
 scanf("%c",&c);
 //when return is pressed c='\n';
 switch(c)
 {
  case '1':break;
  case '\n':break;
 }
}
0
 

Author Comment

by:scuzz1
ID: 10775239
Is there a way I can do this so it dosn't accept char's?
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 250 total points
ID: 10775345
Try this:

int main()
{
int c;
scanf("%c",&c);
//when return is pressed c=10;
c=c-48;//if return c=-38;
switch(c)
{
 case 1:break;
 case -38:break;
}
}
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10775482
U can do this as well:

Similar to last one.Just explains scanf options:

int main()
{
int c=0;
scanf("%[^\n]",&c);//accept input till enter is not pressed
//when return is pressed c=0 the initial value; else ascii for digit is returned
c=c-48;//if return c=-48;
switch(c)
{
case 1:break;
case -48:break;
}
}
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10777162
Hi, u can scanf ur input as a character
scanf("%c",&c);

switch (c)
{
...
case '\r' : for return key
}

Avik.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10777206
sorry, make that '\n'. Although previously answered.

Avik.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10777564
Hi,
Another suggestion as u said ...
>> Is there a way I can do this so it dosn't accept char's?

It is not possible to examine a 'return' entry without accepting the entry as a character.
Ankuratvb 's suggestions are somehow not working in my compiler(Turbo C++ v3.0). i.e it is not accepting c=10 when the 'ENTER' key is pressed. U can try to get the ascii of a number by interrupt calling. (Remember all these fuss would not have been done if u wanted the normal way).
THIS ROUTINE IS ONLY APPLICABLE IN DOS ENV.
// example code not pertaining to ur problem
#include<stdio.h>
#include<dos.h>
void main()
{
 int c;
 union REGS r;
 int86(0x16,&r,&r); // interrupt call
 c=r.h.al; // contains the ascii code
 switch(c){
  case 13:{ printf("1");break;}// 'return key pressed'
  default : printf("0");
 }
 getch();
}
The ascii of 'return key' is 13.
Instead of writing the scanf statement call the inturrupt statement int86(0x16,&r,&r). here 0x16 is the inturrupt number which is generally multiplied by 4 to get to the interrupt service routine. Here the ISR is the scan keyboard one and it outputs the ascii value scanned in the lower half of 16 bit register A(it considers 16 bit portion in DOS).

Avik.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10777664
Ohhhh. if i do int c=getchar() and press return i get c=10 but when c=getch() is done 13 is returned, i think it to be the \r\n combination that is causing the problem. i.e
\r is returned by getch() which is the return key
\n (next character) is returned by getchar() which is the newline key.

so to avoid all the ROM BIOS interrupt techniques u can just do
c=getch();

Avik.
0
 

Author Comment

by:scuzz1
ID: 10778291
This worked.

I just did a cast to int to get the ascii or the return which is 10.

thanks.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with MFCApp 78 424
Linux context switch  - loop takes long time to process 6 129
How to access ANSI/IEEE Std 754 or equivalent information ? 3 73
Fill Null values 5 29
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question