Do-While Loop not executing properly

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2054 Views - Last Post: 16 November 2010 - 03:29 AM Rate Topic: -----

#1 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

Do-While Loop not executing properly

Posted 15 November 2010 - 09:17 PM

My do-while does not execute properly and I'm not sure why. I believe it has something to do with my if...else if statement because the loop executes properly if I isolate either of the if statements. Any help would be greatly appreciated.
  int option;
	bool valid;

        cout << "Enter an option ((0-6) - 0 to exit) ";

        do
	{
		cin >> option;
		valid = true;
		if(!(cin >> option))
		{
			cout << "\nInvalid entry. Enter an integer (0-6) ";
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			valid = false;
		}
		else if((option < 0) || (option > 6))
		{
			cout << option << " is an invalid entry. "
					  "Enter an integer (0-6) ";
			valid = false;
		}
	}while(!valid);


Is This A Good Question/Topic? 0
  • +

Replies To: Do-While Loop not executing properly

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 09:25 PM

Welcome to DIC!

"My do-while does not execute properly" is not a bug report. It gives us nothing useful to work with.

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,990
  • Joined: 10-May 07

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 09:26 PM

This is how I would do it :

#include <iostream>
using namespace std;

int main(void) {
  int option;
  bool valid=true;

  while(valid) {
    cout << "Enter an option ((0-6) - 0 to exit) ";
    cin >> option;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    if(option<0) valid=false;
    if(option>6) valid=false;
    else valid = true;
  }
  cout << option << " is an invalid entry. ";
  return 0;
}


Rather than manually setting the value of valid twice in the code, then manually dealing with three failures... I would just as soon handle the correct values, exit on failure, & report why it failed. Done.
Was This Post Helpful? 0
  • +
  • -

#4 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 10:40 PM

I get a run-time error. If I input a non-integer, the do while loop seems to execute properly. If I enter an integer that is out-of-bonds, then nothing happens. I must input another out-of-bounds integer to get the correct output. If I exclude either of the if-statements the program works properly.

I'd like to use a do-while loop because this is just a function in my program. I'd like to have the user re-enter until he inputs a valid one. Once output correctly the value is returned to the main function. Ty for the responses!

Here is a sample output:

Enter an option (0 to exit) 3 //return
99 //return
99 is an invalid entry. Enter an integer (0-6) 88 //return
88 //return
88 is an invalid entry. Enter an integer (0-6) r //return

Invalid entry. Enter an integer (0-6) t //return

Invalid entry. Enter an integer (0-6) 4 //return
4 //return - program terminates
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,990
  • Joined: 10-May 07

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 10:46 PM

Then you need to check the properties of the input, as well as it's value. You get a runtime error trying to compare a character to an integer.

isdigit()
isalpha()
Was This Post Helpful? 0
  • +
  • -

#6 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 10:53 PM

But if the value isn't an integer, then doesn't the program forgo the else if? Doesn't if(!(cin >> option)) execute if the value isn't an integer.

This post has been edited by Planet Telex: 15 November 2010 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,990
  • Joined: 10-May 07

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 10:55 PM

You would need to implement the logic for however you want to handle non-integer values.
Was This Post Helpful? 0
  • +
  • -

#8 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 11:00 PM

But don't I do that with the if(!(cin >> option))? If untrue then it checks to see if the integer value is valid, right?

This post has been edited by Planet Telex: 15 November 2010 - 11:02 PM

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,990
  • Joined: 10-May 07

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 11:05 PM

I wouldn't. That won't fail, cin is still going to pass a value to option. It doesn't know what you want, or what you consider acceptable. It's just going to dump standard input. It's up to you to validate the value of the variables before you use it.
Was This Post Helpful? 0
  • +
  • -

#10 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 11:11 PM

O i see. I had thought that checked to see if it was an integer. Then why when I input a non-integer, does it appear to execute properly?

Enter an option (0 to exit) w

Invalid entry. Enter an integer (0-6) q

Invalid entry. Enter an integer (0-6) r

Invalid entry. Enter an integer (0-6) t

Invalid entry. Enter an integer (0-6) y

Invalid entry. Enter an integer (0-6) 77
66
66 is an invalid entry. Enter an integer (0-6)

...

I mean, this is the algorithm we were given in class. So I figure it should work.

This post has been edited by Planet Telex: 15 November 2010 - 11:13 PM

Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,990
  • Joined: 10-May 07

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 11:13 PM

#include <iostream>
#include <string>
#include <sstream>
#include <locale>
using namespace std;

int main(void) {
  string option_str;
  int option;
  bool valid=true;

  while(valid) {
    cout << "Enter an option ((0-6) - 0 to exit) ";
    cin >> option_str;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    if(!isdigit(option_str[0])) valid=false;
    else option=atoi(option_str.c_str());
    if(option<0) valid=false;
    if(option>6) valid=false;
    else valid = true;
  }
  cout << option << " is an invalid entry. ";
  return 0;
}

Was This Post Helpful? 1
  • +
  • -

#12 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,990
  • Joined: 10-May 07

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 11:20 PM

View PostPlanet Telex, on 16 November 2010 - 12:11 AM, said:

I had thought that checked to see if it was an integer.


You can try it both ways & figure out if it works or not.

Someone will correct me if I'm wrong, & it wouldn't be the 1st time. But :
a.) I'm pretty certain that it wouldn't work for what you are trying to use it for. The if(!cin...)) is going to check the return value of cin. If cin accepts & passes a value (of any case) it's going to return success.
b.) In my book it's silly to validate upon input. I say let each function do it's job & handle the situation on a per-basis. Accept the value, validate the value, parse the value, verify the value, handle any input errors.
Was This Post Helpful? 0
  • +
  • -

#13 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 11:38 PM

So I've forgone the if(!(cin >> option)) and decided to use the isdigit() like you suggested and the program executes correctly. Im unsure as to why my original code doesnt work though but I suppose it doesnt really matter. Thank you for your input. It executes properly and that's all I really care about. Here is my finalized code:

  string option_str;
        int option;
        bool valid;
       
        cout << "Enter an option (0 to exit) ";

        do
	{
		cin >> option_str;
		valid = true;
		if(!(isdigit(option_str[0])))
		{
			cout << "\nInvalid entry. Enter an integer (0-6) ";
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			valid = false;
		}
		else
		{
			option = atoi(option_str.c_str());
			if((option < 0) || (option > 6))
					{
						cout << endl << option << " is an invalid entry. "
								          "Enter an integer (0-6) ";
						valid = false;
					}
		}

	}while(!valid);


This post has been edited by Planet Telex: 15 November 2010 - 11:44 PM

Was This Post Helpful? 0
  • +
  • -

#14 Planet Telex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-November 10

Re: Do-While Loop not executing properly

Posted 15 November 2010 - 11:52 PM

As I understand it, the cin >> integerValue returns a bool value of true if it is an integer input. Then again I'm not really sure and can't elaborate beyond that.

This post has been edited by Planet Telex: 15 November 2010 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,990
  • Joined: 10-May 07

Re: Do-While Loop not executing properly

Posted 16 November 2010 - 12:00 AM

Maybe this is what you are looking for?

Quote

In this example, cin sets the fail bit on the stream when it encounters non-numeric characters. The program clears the fail bit and strips the invalid character from the stream to proceed.
// iostream_cin.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main()
{
   int x;
   cout << "enter choice:";
   cin >> x;
   while (x < 1 || x > 4)
   {
      cout << "Invalid choice, try again:";
      cin >> x;
      // not a numeric character, probably
      // clear the failure and pull off the non-numeric character
      if (cin.fail())
      {
         cin.clear();
         char c;
         cin >> c;
      }
   }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2