473,756 Members | 4,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error when entering wrong data type in do-while loop

Hello

I need help with a problem

I have homework for a larger program than shown below but the code below
effectively shows the problem whem compiled and executed.

Basically, I want to be able keep the loop going even if a character
(alphabet) is entered when it should be a number i.e. the wrong data type is
entered by mistake.

The concept is 'that the loop should work under all conditions'.

At the moment if a character is entered, the result is that the screen
constantly scrolls and is not readable.
Ideally I would like to insert into the program a line to the effect

{
'if (n != int)
cout << "that is not a valid option. Please try again"
}

However, for those of you who know c++ better than me, you will know that
this cannot be entered

Is there any way of entering this logical statement

Below is a part of the code, when compiled and executed will demonstrate the
problem
#include <iostream.h>

int main()
{
unsigned long n;
do
{
cout<< " Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n" ;
}
while (n != 0);

return 0;
}
Jul 23 '05 #1
3 2280
Jeff wrote:
Hello

I need help with a problem

I have homework for a larger program than shown below but the code below
effectively shows the problem whem compiled and executed.

Basically, I want to be able keep the loop going even if a character
(alphabet) is entered when it should be a number i.e. the wrong data type is
entered by mistake.

The concept is 'that the loop should work under all conditions'.

At the moment if a character is entered, the result is that the screen
constantly scrolls and is not readable.
Ideally I would like to insert into the program a line to the effect

{
'if (n != int)
cout << "that is not a valid option. Please try again"
}

However, for those of you who know c++ better than me, you will know that
this cannot be entered

Is there any way of entering this logical statement

Below is a part of the code, when compiled and executed will demonstrate the
problem
#include <iostream.h>

int main()
{
unsigned long n;
do
{
cout<< " Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n" ;
}
while (n != 0);

return 0;
}


Add something like:

if (!isaplha(n) {
cout << "You were supposed to enter a number" << endl;
}
Jul 23 '05 #2
"Jeff" <Je********@wyn lea.wanadoo.co. uk> wrote...
[...]
Basically, I want to be able keep the loop going even if a character
(alphabet) is entered when it should be a number i.e. the wrong data type
is entered by mistake.
[...]


This is covered in the FAQ. Please look it up. If you're new here and
don't yet know it, the link is http://www.parashift.com/c++-faq-lite/

V
Jul 23 '05 #3
Karsten Baumgarten wrote:

if (!isaplha(n) {
cout << "You were supposed to enter a number" << endl;
}


Sorry for the typo! The function is called isalpha().
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5088
by: Morten Gulbrandsen | last post by:
Hello programmers, I think something is wrong with my administration, Basically I get these messages, ERROR 1005 at line 9: Can't create table '.\company\department.frm' (errno: 150) ERROR 1045: Access denied for user: 'elmasri@127.0.0.1' (Using password: YES)
5
16258
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information Server might not be running or the path exists and is redirected to another machine. Please check the status of this virtual directory in the Internet Services Manager."
4
25248
by: Saso Zagoranski | last post by:
Hi! I have created an SqlCommand object and I have set some parameters to it... One of the parameters is of the DateTime type; here is the code: sqlCommand.Parameters.Value = DateTime.Parse(this.DateTextBox.Text); sqlCommand.ExecuteNonReader(); I get the error:
7
5021
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying. I created the websetup and built the MSI, have the bundled version. Copied to webserver and ran Websetup.msi. Said I had to remove old version, which I did, then reran WebSetup.msi and keeps giving me this error. "The installer was interrupted...
4
3950
by: SJ | last post by:
Hi all, I have come across a weird problem when attempting to automatically set the focus in a vb.net form to a checkbox control... In my form I have (on a tab page in a tab control) several textboxes and a checkbox. The behaviour I want from my app is as follows:- When the textbox (which is prior in tab order to the tab control) has been filled with a certain length of text by the user, the focus is
669
26159
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
9
14324
by: sparks | last post by:
I have a table in 2003 that has 221 fields defined. I tried to add another variable and got this error. I was surprised when it didn't say too many fields defined. I look at the error description and it says size greater than the largest permissible size of 2K. what value does this 2k refer to? if I do a size on the table it shows 1901.
35
3803
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
2
4901
by: bloukopkoggelmander | last post by:
Hi all I am getting the following error and it is driving me insane. I just cannot find a fix for it. : You tried to assign the NULL value to a variable that is not a Variant data type. Now I have a main form linked to a as400 table. This has a subform on it, also linked to a AS400 table. Now the foirst control on the subform( also the first field a user needs to complete on the form) is the one I am trying to popullate. The minute I...
17
2297
by: MLH | last post by:
I have tested the following in immed window: ?isnumeric(1) True ?isnumeric(1.) True ?isnumeric(1.2) True ?isnumeric(1.2.2)
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.