Solved

looking for a quick fix

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

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
Unable to start eclipse ? 17 175
Super Scope, DHCP 5 101
Setting variables in a stored procedure 5 80
asp.net web app 3 59
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

759 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