By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Error when entering wrong data type in do-while loop

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"Jeff" <Je********@wynlea.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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.