Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-07
9
Medium Priority
?
390 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Avik Dasgupta
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:Avik Dasgupta
ID: 10777206
sorry, make that '\n'. Although previously answered.

Avik.
0
 
LVL 2

Expert Comment

by:Avik Dasgupta
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:Avik Dasgupta
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Suggested Courses

916 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