We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

What's wrong with my do-while/if statement??

mwonch
mwonch asked
on
Medium Priority
218 Views
Last Modified: 2010-04-02
Hi!

I'm having a little problem with my program (see below). It does what it's supposed to do, so far.  However, there is one big problem I'm hoping someone can make small for me.

Most of the way down the code, you will see the do-while loop.  Now, the program works okay as long as a number is entered.  is used to go into an infinite loop whenever a character is entered. I added an else statement, suppose dto display a message is anything other than specified int he if statement is entered.  All this did was make the program crash when a single letter is entered, and the infinite loop occurs ONLY if a letter is entered inadvertantly (or on purpose) along with a number (examples: t crashes the program, where 3e starts the infinite loop).

Can someone help me with this little problem? It really is driving me NUTS! I'm not even sure if it's the do-while loop causing this.  Could it only be the if statement?

The code (in it's entirity):

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <bool.h>
#include <windows.h>

struct digit
{    int Value;
      digit* Next;
};

typedef digit* number;
number Head;
number Curr;
number Prev;
number Nu;

void Insert(int data)
// Insert() accepts input from user and places data in a node on the
// linked list
{
      Nu = new digit;
      Nu->Value = data;
      Nu->Next = Head;
      Head = Nu;

}

void Retrieve()
// Retrieve() will display all integer data in linked list
{
      for(Curr = Head; Curr != NULL; Curr=Curr->Next)
            {
                  cout <<Curr->Value<<",";
            }

}

digit* Increment(number N)
{
      if(N != NULL)
      {
            N = Head;
            N->Value += 1;
            //Head = Head->Next;
      }
      return N;
}
main()
{
      int Num;
      char reply;
      while(1)
      {
      cout <<"I) Insert Data\n"
              <<"R) Retrieve Data\n"
              <<"N} Increment Data\n"
              <<"X) Exit\n\n "<<flush;
      cin >> reply;

      reply = toupper(reply);

      switch(reply)
      {
            case 'I':
            cout << "Enter the number '10' to terminate input!"<<endl;

            do
            {

            clrscr();

            cout << "Enter Data:"<<flush;
            cin >> Num;
            if((isalpha(Num)== false)&& (Num >= 0) && (Num <= 9))
                  {
                  Insert(Num);
                  }
            else
                  {
                  cout << "Error! Enter a number int he range of 0 and 9!\n" << endl;
                  }

            }while(Num != 10);

            break;

            case 'R':
            Retrieve();
            getch();
            clrscr();
            break;

            case 'N':
            Increment(Head);
            getch();
            clrscr();
            break;

            case 'X':
            PostQuitMessage(0);
            exit(0);
            break;
      }

  }
}

Thanks in advance!

Mike Wonch
Comment
Watch Question

Author

Commented:
Adjusted points to 75

Commented:
if((isalpha(Num)== false)&& (Num >= 0) && (Num <= 9))

How about

if( isdigit( Num ))

Phillip  (still thinkin...)
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
Just say that snoegler found you a working answer.  Since I already had it done (and I've never done cin and cout, I made this a learning project), here's my answer...

Instead of a simple

   cin >> num;

How about getting character data, after all, all data is valid!

   char line[ 100 ];
   cin.getline( line, 100, '\n' );

   cin.getline( line, 100, '\n' );
// cin >> reply;
     
   line[ 0 ] = toupper( line[ 0 ] );
     
   switch( line[ 0 ] )
   {
   case 'I':
      cout << "Enter the number '10' to terminate input!"<<endl;
         
      do
      {
         clrscr();
         cout << "Enter Data:"<<flush;

         cin.getline( line, 100, '\n' );

         if( isdigit( line[ 0 ] ))
         {
            Insert( line[ 0 ] );
         }
         else
         {
            cout << "Error! Enter a number int he range of 0 and 9!\n" << endl;
         }

      }while( atoi( line ) != 10);
 
Talk about a bad 'karma' answer!!

Phillip

Commented:
This should work but there is one thing wrong, i think:
If the user enters '10' to stop the input loop, then 'isdigit(line[0])' returns TRUE and so another number is inserted ('1').
I don't like these 'cin' and 'cout' things ... ;)

Commented:
Good point, I can't stand those cin and cout(s) either!! I'm a MFC man myself.  (No questions to answer over there, I'm slummin') You are right about the isdigit, it should be more like

Num = atoi( line );

but once I saw that you already provided a good answer, I kinda lost the heart to finish everything!!

Phillip

Commented:
I made a mistake myself ;)
With just a simple getch() you will never get more than one char at a time ...
So entering '10' is not possible ...
This idea with cin.getline and atoi() that you mentioned would be better.
So now i lost the heart no finish everything <G>

Commented:
Don't lose heart now!  You got the points!!!

Phillip

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.