Solved

looking for a quick fix

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 410
Problem with form project 66 316
C dll call freezes 5 93
Trouble linking program with -lcrypt 3 141
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

932 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

11 Experts available now in Live!

Get 1:1 Help Now