Solved

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

Posted on 2004-04-07
9
365 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

777 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