Solved

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

Posted on 2004-04-07
9
344 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now