?
Solved

very simple question scanf() [Urgent]

Posted on 2003-03-30
7
Medium Priority
?
244 Views
Last Modified: 2010-05-19
I am new to C and I am searching for a way to:
end the program if the user presses ESC while the program wants an answer for the scanf.
e.g:

int main() {
int d, m, y;
printf("Please enter a date:\n");
scanf("%d %d %d", &d, &m, &y);      // This is the line!
.
.
.
}
The user should be able to quit pressing ESC once, having already typed anything or not.
0
Comment
Question by:alemdar
[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
  • 3
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
Kocil earned 225 total points
ID: 8234487
Can't use scanf for that mate.
But this may help

main()
{
   char buffer[80];
   char ch; int i;
   int d, m, y;
   printf("Please enter a date:\n");
   for (i=0; i<80; i++) {
     ch=getche();
     if ((ch == '\r') || (ch == 27)) break;
     buffer[i] = ch;
   }
   if (ch == '\r) {
      sscanf(buffer, "%d %d %d", &d, &m, &y);
      /* do something with dmy here */
   }        
}

0
 
LVL 5

Expert Comment

by:Kocil
ID: 8234496
I forgot to end the buffer after the for loop.
  ...
  buffer[i] = 0;  
  if (ch != 27) {
     sscanf(buffer, "%d %d %d", &d, &m, &y);
     /* do something with dmy here */
  }        
  ...
0
 

Author Comment

by:alemdar
ID: 8234569
It is a good solution, but because of the buffering method, user is not allowed to make any mistakes (i mean using backspace). Meanwhile, another question is: what is the difference between scanf and sscanf?
0
Industry Leaders: 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!

 

Author Comment

by:alemdar
ID: 8234693
Ok, i found that the code of backspace is 8 and solved the problem:

for (i=0; i<30; i++) {
    ch=getche();
    if (ch == 27) {
         return 0;
    }
    if (ch == '\r') break;
    buffer[i] = ch;
    if (ch == 8) i = i - 2;
  }

Therefore, I also didn't required to put all the code in a if-statement as you had written. Now I ask about sscanf??
0
 
LVL 15

Expert Comment

by:efn
ID: 8235569
sscanf is just like scanf except it reads from memory instead of an input stream.  In Kocil's code, it is reading from the array named "buffer".

When you are processing a backspace, it would be a good idea to check to make sure subtracting 2 from i doesn't cause i to become less than zero, so you don't store subsequent characters outside the bounds of the buffer array.  That is, for example, if the user hits backspace as the very first key pressed, you don't want i to be set to -2.
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8235856
efn answered the sscanf.
And to make you love C more, this is another trick :)

#include <stdio.h>
#include <conio.h>
#include <stdarg.h>

int esc_scanf(char *format, ...)
{
   char buffer[10];
   char ch; int i=0;
   va_list args;
   va_start(args, format);

   do {
      ch = getch();
      switch (ch) {
        case 27   : putchar('\n'); return 0;
        case '\b' : i--; printf("\b \b");
        case '\r' : break;
        default : if (i < 9) putchar(buffer[i++] = ch);
                  else putchar(7);
      }
   } while (ch != '\r');
   putchar('\n');
   buffer[i] = 0;
   /* see how we can pass variable number of arguments to vsscanf */
   i = vsscanf(buffer, format, args);
   va_end(args);
   return i;
}


main()
{
   int a,b;

   if (esc_scanf("%d %d", &a, &b) == 2)
     printf("*** %d %d\n", a, b);
   else
     printf("Wrong input\n");
   getch();
}

0
 

Author Comment

by:alemdar
ID: 8236071
Thanks for the support, efn. Now I change the code to:
for (i=0; i<30; i++) {
    ch=getche();
    if (ch == 27) return 0;
    if (ch == '\r') break;
    buffer[i] = ch;
    if (ch == 8 && i != 0) i = i - 2;
    if (ch == 8 && i == 0) i = i - 1;
  }
And thanks again, Kocil, for the trick ( i have to study it :) ) and for the first answer that i have used in my program. It worked and I learned something useful indeed.
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

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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

765 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