#include<iostream>
using namespace std;
int main()
{
char input, X, Y;
cout<<"Enter in the Y or X"<<endl;
cin>>input;
do ( input == Y || input == X)
{
cout<<" You have entered the correct input!!!"<<endl;
}
while (input != Y || input != X )
cout<<"Error: Input a correct letter!!!"<<endl;
return 0;
}
20 Replies - 2512 Views - Last Post: 25 April 2010 - 09:25 PM
#1
Do While loop
Posted 25 April 2010 - 07:04 PM
Replies To: Do While loop
#2
Re: Do While loop
Posted 25 April 2010 - 07:11 PM
do
{
// this code
} while (this_is_true);
That simple. There's NOTHING after the do except that block of code from { to } while (this_is_true);
EDIT: Should you only want to do what's in the do-while loop based on another condition, then test that condition with an if() statement and THEN enter the do-while loop if that condition isn't what you want.
This post has been edited by JackOfAllTrades: 25 April 2010 - 07:13 PM
#3
Re: Do While loop
Posted 25 April 2010 - 07:11 PM
do {
statement;
} while (condition);
#4
Re: Do While loop
Posted 25 April 2010 - 07:23 PM
#5
Re: Do While loop
Posted 25 April 2010 - 07:28 PM
int i = 0;
if (i != 9)
{
do
{
// Something that changes the condition must happen in this loop,
// otherwise you will NEVER LEAVE IT.
i++;
} while (i != 9);
}
That's it. Badda-bing, badda-boom.
#6
Re: Do While loop
Posted 25 April 2010 - 07:37 PM
Just think about how to check to see if the user didn't enter X or Y with an if statement then prompt the user to enter a correct letter using a do while loop to keep prompting for the correct letter if they still don't type X or Y.
I believe I'm explaining that right
This post has been edited by Christoph: 25 April 2010 - 07:37 PM
#7
Re: Do While loop
Posted 25 April 2010 - 07:42 PM
#include<iostream>
using namespace std;
int main()
{
char input, X, Y;
cout<<"Enter in the Y or X"<<endl;
cin>>input;
if ( input == Y || input == X)
{
do
{
cout<<" You have entered the correct input!!!"<<endl;
}
while (input =!)
{
cout<<"Error: Input a correct letter!!!"<<endl;
}
return 0;
}
Thats what the problem states if the right input is not enter you have to tell them that they have failed. The program should run into they enter the right input.
Christoph, on 25 April 2010 - 06:37 PM, said:
Just think about how to check to see if the user didn't enter X or Y with an if statement then prompt the user to enter a correct letter using a do while loop to keep prompting for the correct letter if they still don't type X or Y.
I believe I'm explaining that right
#8
Re: Do While loop
Posted 25 April 2010 - 07:45 PM
#9
Re: Do While loop
Posted 25 April 2010 - 07:55 PM
#include<iostream>
using namespace std;
int main()
{
char input, X, Y;
do
{
cout<<"Enter in the Y or X"<<endl;
cin>>input;
if ( input == Y || input == X)
cout<<" You have entered the correct input!!!"<<endl;
}
while (input =!);
{
cout<<"Error: Input a correct letter!!!"<<endl;
}
return 0;
}
#10
Re: Do While loop
Posted 25 April 2010 - 08:24 PM
First thing is that you only need one char variable "input" for this, the X and Y char variables don't do anything in your program unless you want to assign values to them.
Use the if statement to see if the characters were not X and Y and if they were not then we jump into the do while loop. Tell the user they need to input a correct letter and prompt for the input again, after they input a letter the loop checks to see if the character was not a correct one and if it wasn't then it goes back up to the top of the loop and starts over.
If they did enter a correct letter it breaks out of the loop and then breaks out of the if statement and lets the user know they entered a correct letter and returns 0 to the main function and ends the program.
#include <iostream>
using namespace std;
int main()
{
char input;
cout << "Enter in the Y or X: ";
cin >> input;
if ((input != 'X') && (input != 'Y'))
{
do
{
cout << "Error: Input a correct letter!!!" << endl;
cout << "Enter in the Y or X: ";
cin >> input;
} while ((input != 'X') && (input != 'Y'));
}
cout << "You Entered the correct letter" << endl;
return 0;
}
#11
Re: Do While loop
Posted 25 April 2010 - 08:34 PM
Christoph, on 25 April 2010 - 07:24 PM, said:
First thing is that you only need one char variable "input" for this, the X and Y char variables don't do anything in your program unless you want to assign values to them.
Use the if statement to see if the characters were not X and Y and if they were not then we jump into the do while loop. Tell the user they need to input a correct letter and prompt for the input again, after they input a letter the loop checks to see if the character was not a correct one and if it wasn't then it goes back up to the top of the loop and starts over.
If they did enter a correct letter it breaks out of the loop and then breaks out of the if statement and lets the user know they entered a correct letter and returns 0 to the main function and ends the program.
#include <iostream>
using namespace std;
int main()
{
char input;
cout << "Enter in the Y or X: ";
cin >> input;
if ((input != 'X') && (input != 'Y'))
{
do
{
cout << "Error: Input a correct letter!!!" << endl;
cout << "Enter in the Y or X: ";
cin >> input;
} while ((input != 'X') && (input != 'Y'));
}
cout << "You Entered the correct letter" << endl;
return 0;
}
#12
Re: Do While loop
Posted 25 April 2010 - 08:37 PM
#13
Re: Do While loop
Posted 25 April 2010 - 08:50 PM
take a look
#include <iostream>
using namespace std;
int main()
{
char input;
do{ //do-while just means loop this while checking the conditions at the end of the loop
cout << "Enter in the Y or X: ";
cin >> input;
}while (input != 'X' && input != 'Y'); //keep looping if these are true
//obviously were out of the loop now
cout << "You Entered the correct letter" << endl;
return 0;
}
if you are wanting to display an error message, just check the conditions inside of the while loop
#include <iostream>
using namespace std;
int main()
{
char input;
while(true) //loop until i hit a break;
{
cout << "Enter in the Y or X: ";
cin >> input;
if(input == 'Y' || input == 'X')
break; //break out of the loop
else
cout<<"INVALID INPUT..."<<endl;
}
cout << "You Entered the correct letter" << endl;
return 0;
}
There are many possible solutions when using while loops, you could just check the conditons in a regular while loop
#include <iostream>
using namespace std;
int main()
{
char input = '\0';
while(input != 'Y' && input != 'X') //loop until i hit a break;
{
cout << "Enter in the Y or X: ";
cin >> input;
}
cout << "You Entered the correct letter" << endl;
return 0;
}
you decide how you want to do it
you can even just put everything inside your condition if you wanted.
#include <iostream>
using namespace std;
int main()
{
char input;
do{ cout<<"ENTER X OR Y : " }
while(cin>>input && input!='X' && input!='Y');
cout << "You Entered the correct letter" << endl;
return 0;
}
I think ive mapped out every possible way to do it so ill shut up now lol
This post has been edited by ImaSexy: 25 April 2010 - 09:03 PM
#14
Re: Do While loop
Posted 25 April 2010 - 08:54 PM
#15
Re: Do While loop
Posted 25 April 2010 - 08:56 PM
|
|

New Topic/Question
Reply




MultiQuote






|