473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Detect if character is number and add delay time

11
Hello i made a program based on the tutorial on youtube. It's suppose to calculate the average between 3 numbers so i got some questions about that.

1. How to detect when a person enters a number not a character = letter or non-numeric sign. So if he enters non-number i want the program to return him to the place where he entered it and ask him to enter the number again.
2. The program works great and calculates the average. The only thing i want is to add a delay time cuz when my program calculates the average it exits instantly(after 0.5 secs or something) so the person may not see the average.

Code:
Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7.  
  8.     double n1 = 0.0;
  9.     double n2 = 0.0;
  10.     double n3 = 0.0;
  11.     double average = 0.0;
  12.  
  13.     std:: cout << "This program will calculate the average of 3 numbers " << std::endl;
  14.     cout << "Enter the first number: " << endl;
  15.     cin >> n1;
  16.     cout << "Enter the second number: " << endl;
  17.     cin >> n2;
  18.     cout << "Enter the third number: " << endl;
  19.     cin >> n3;
  20.  
  21.     average = (n1 + n2 + n3) / 3;
  22.  
  23.     cout << "The average of the entered numbers is: " << average << endl << endl;
  24.  
  25.     system("PAUSE");
  26.     return 0;
  27.  
  28. }
Thanks for replies
Sep 12 '09 #1
5 3653
weaknessforcats
9,208 Expert Mod 8TB
You might try an infinite while loop with an embedded switch statement:

Expand|Select|Wrap|Line Numbers
  1. while (1)
  2. {
  3.      Get a character
  4.  
  5.      switch (on that character)
  6.      {
  7.           case 0:
  8.           case 1: 
  9.           etc...
  10.                  Call a function to do your work
  11.                   break;
  12.           case E:
  13.           case e:
  14.                   This is exit choice.
  15.                   Do a return. If  this is in main(), this will stop the program.
  16.           default:
  17.                  Display invalid character message
  18.                  break;
  19.      }
  20. }
There are any number of ways to do this. You might use this to display a menu and when a choice is made to do some work you could ask for your digit inside the function that was called using the menu choice.
Sep 12 '09 #2
Banfa
9,065 Expert Mod 8TB
the header ctype.h contains a lot of useful functions for classifying characters. isdigit might be useful to you look it up in your standard library reference.
Sep 13 '09 #3
donbock
2,426 Expert 2GB
Looks like the OP wants to parse floating-point numbers. If so, then I'd suggest copying the input characters into a string and using strtod to parse the string.

If upon return, endptr points to a string of zero or more whitespace characters then the input string must have been a valid floating-point number.

However, I'm not sure what strtod does if it is passed a string containing only whitespace characters. You should try that as an experiment to see if you need to explicitly trap that case.

... or at least that's how I would do it in C. The OP is using C++; I leave it to the C++ gurus to reveal some fancy polymorphic overloaded magical better way to do it.
Sep 13 '09 #4
IgorZ
11
Can you give the full code of it? Because i'm only a begginer i don't understand it clearly...
Sep 13 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
I just did. The rules prevent me from giving you a complete solution that compiles and links. Just follow the directions in my post.

Also, use Google. There are all sorts of example code out there.
Sep 13 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
0
by: Giovanni | last post by:
Hi, I am using VS 2005 - BETA 2. I'd like to use My.Computer.Network.IsAvailable to detect whether an internet connection is available. Is this the best "new" way of doing things or does the...
8
by: Luke Matuszewski | last post by:
I have read all posts about how to detect that url have changed to new page and trigger the event handler then eg. function aidLogout(evt) { if(evt) { /* maybe via analyse of evt object i can...
2
by: sachinik19 | last post by:
Hi, We are using SAXParser (xerces) for parsing an xml with utf-8 encoding. For some special character it gives SAXException with error message - "The invalid character is found in the document...
2
by: Midsomer | last post by:
Hi. I have an Access database with a form containing 4 datasheets and I allow the user to change column widths. On closing the form, a routine is fired that saves each column width to an INI file....
3
by: =?Utf-8?B?WHltb3g=?= | last post by:
I'm developing apps in WindowsXP which calls IBM's Websphere MQ Client. This client is a .NET application. My app is using an ActiveX wrapper in native Windows XP. When the application calls the...
21
by: Bob Darlington | last post by:
I have a routine which checks idle time and opens a pop up screen to advise users before shutting them down. The pop up has an option for the user to keep the application open, in which case the...
2
by: GS | last post by:
How do I properly detect row change before allowing moving to next row? I tried using RowLeave . here is brief descript of the form setup. I have ieStringTmpDataSetwith regex table...
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.