Solved

looking for a quick fix

Posted on 1998-11-22
2
168 Views
Last Modified: 2010-04-15
In my program, I prompt the user to enter a number.  I use the gotoxy statement to position the cursor but the problem being, is that if the user presses enter before the number is entered then the cursor will do a carriage return and when the user types a number and presses enter it is on the left hand side of the screen.

Is there a way to prevent the user from pressing enter before a number is entered?  Please can I have the answer in the least amount of code possible.
0
Comment
Question by:sadorsky
2 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 1254580
#include <stdio.h>
#include <conio.h>

#define MAX_DIGITS  5

void main() {
  char ch, buffer[100];
  int x = 0;
  int flag=0;
  while ((!flag) && (x<MAX_DIGITS))
  {
       ch = getch();
       if ((ch>='0')&&(ch<='9')) {putchar(ch); buffer[x++]=ch; }
       if ((ch==8) && (x>0)) {gotoxy(wherex()-1,wherey());
            putchar(32); gotoxy(wherex()-1,wherey()); x--;}
       if ((ch==13) && (x>0)) flag = 1;
  }


  buffer[x]='\0';
  printf("\n\n%s",buffer);
}



This sample program shows how it is done.  It ONLY allows the user to press number keys, backspace, or enter.  It will not allow the user to press backspace or enter unless at least one number has been entered.  In this program, I set the max_digits to 5, i.e. the program will automatically "press enter" for you when 5 digits have been entered.  You can tailor this to your needs.

The number the user entered is stored in a string called "buffer".
0
 

Author Comment

by:sadorsky
ID: 1254581
Thanx scapdog, I'll try it.  Are there any shorter ways of doing it?

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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.
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.

762 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

21 Experts available now in Live!

Get 1:1 Help Now