Solved

looking for a quick fix

Posted on 1998-11-22
2
172 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
[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
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

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!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and writing to files 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.

756 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