Solved

looking for a quick fix

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 how to use strings and some functions related to them 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.

623 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