Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Error when entering wrong data type in do-while loop

Reply
Thread Tools

Error when entering wrong data type in do-while loop

 
 
Jeff
Guest
Posts: n/a
 
      02-05-2005
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;
}


 
Reply With Quote
 
 
 
 
Karsten Baumgarten
Guest
Posts: n/a
 
      02-05-2005
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;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-05-2005
"Jeff" <(E-Mail Removed)> 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


 
Reply With Quote
 
Karsten Baumgarten
Guest
Posts: n/a
 
      02-05-2005
Karsten Baumgarten wrote:
>
> if (!isaplha(n) {
> cout << "You were supposed to enter a number" << endl;
> }


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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Entering an enumeration type as a property Nathan Sokalski ASP .Net 6 11-07-2009 11:56 PM
Unexpected result when entering wrong data type japh C++ 4 01-08-2005 08:21 PM
How do i restrict entering non-numeric data =?Utf-8?B?c3A=?= ASP .Net 2 10-27-2004 06:09 PM
Prevent entering data into DB twice =?Utf-8?B?bWdAdGhld29ybGQuY29t?= ASP .Net 3 06-09-2004 07:48 PM



Advertisments