Solved

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

Posted on 2004-04-07
9
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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: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

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

Suggested Solutions

Title # Comments Views Activity
Problem with MFCApp 78 469
How to get time_t value that is a week old 6 174
Linux context switch  - loop takes long time to process 6 156
valid enum? 6 81
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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to create, access, and change arrays in the C programming language.

751 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