Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Error when entering wrong data type in do-while loop (http://www.velocityreviews.com/forums/t288649-error-when-entering-wrong-data-type-in-do-while-loop.html)

Jeff 02-05-2005 06:44 PM

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;
}



Karsten Baumgarten 02-05-2005 06:55 PM

Re: Error when entering wrong data type in do-while loop
 
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;
}

Victor Bazarov 02-05-2005 06:55 PM

Re: Error when entering wrong data type in do-while loop
 
"Jeff" <Jeffwatson@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



Karsten Baumgarten 02-05-2005 06:56 PM

Re: Error when entering wrong data type in do-while loop
 
Karsten Baumgarten wrote:
>
> if (!isaplha(n) {
> cout << "You were supposed to enter a number" << endl;
> }


Sorry for the typo! The function is called isalpha().


All times are GMT. The time now is 10:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.