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);
18 Replies - 1740 Views - Last Post: 16 November 2010 - 03:29 AM
#1
Do-While Loop not executing properly
Posted 15 November 2010 - 09:17 PM
Replies To: Do-While Loop not executing properly
#2
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 09:25 PM
"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?
#3
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 09:26 PM
#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.
#4
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 10:40 PM
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
#5
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 10:46 PM
#6
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 10:53 PM
This post has been edited by Planet Telex: 15 November 2010 - 10:58 PM
#7
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 10:55 PM
#8
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 11:00 PM
This post has been edited by Planet Telex: 15 November 2010 - 11:02 PM
#9
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 11:05 PM
#10
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 11:11 PM
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
#11
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;
}
#12
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 11:20 PM
Planet Telex, on 16 November 2010 - 12:11 AM, said:
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.
#13
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 11:38 PM
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
#14
Re: Do-While Loop not executing properly
Posted 15 November 2010 - 11:52 PM
This post has been edited by Planet Telex: 15 November 2010 - 11:53 PM
#15
Re: Do-While Loop not executing properly
Posted 16 November 2010 - 12:00 AM
Quote
// 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;
}
}
}
|
|

New Topic/Question
Reply




MultiQuote




|