Do While loop

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3819 Views - Last Post: 25 April 2010 - 09:25 PM Rate Topic: -----

#1 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

Do While loop

Posted 25 April 2010 - 07:04 PM

I need help trying to figure out what I did wrong with this do while loop.


#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Do While loop

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Do While loop

Posted 25 April 2010 - 07:11 PM

It's
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

Was This Post Helpful? 0
  • +
  • -

#3 Christoph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 233
  • View blog
  • Posts: 1,559
  • Joined: 23-August 09

Re: Do While loop

Posted 25 April 2010 - 07:11 PM

Might want to double check your loop

do {
    statement;
} while (condition);


Was This Post Helpful? 0
  • +
  • -

#4 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

Re: Do While loop

Posted 25 April 2010 - 07:23 PM

So do I put the condition in the (). Can you give me an example?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Do While loop

Posted 25 April 2010 - 07:28 PM

This is really, REALLY simple logic...please try to understand, this is very important.

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.
Was This Post Helpful? 1
  • +
  • -

#6 Christoph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 233
  • View blog
  • Posts: 1,559
  • Joined: 23-August 09

Re: Do While loop

Posted 25 April 2010 - 07:37 PM

What he 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 :D

This post has been edited by Christoph: 25 April 2010 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

Re: Do While loop

Posted 25 April 2010 - 07:42 PM

I changed the code around. I want to know if I am on the right track now.

#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.

View PostChristoph, on 25 April 2010 - 06:37 PM, said:

What he 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 :D

Was This Post Helpful? 0
  • +
  • -

#8 Christoph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 233
  • View blog
  • Posts: 1,559
  • Joined: 23-August 09

Re: Do While loop

Posted 25 April 2010 - 07:45 PM

Well, think about it. What's the point of creating a loop to let the user know they entered the right letter? That's like never giving a dog a treat for doing the correct trick.
Was This Post Helpful? 1
  • +
  • -

#9 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

Re: Do While loop

Posted 25 April 2010 - 07:55 PM

I think I am starting to get the point now. I put the if statment inside the do while. Now I am having issues with it running due to the fact that it has one error now.


#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;

}


Was This Post Helpful? 0
  • +
  • -

#10 Christoph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 233
  • View blog
  • Posts: 1,559
  • Joined: 23-August 09

Re: Do While loop

Posted 25 April 2010 - 08:24 PM

Ok, I may be confusing you so I'll just try to break it down.

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;
}


Was This Post Helpful? 1
  • +
  • -

#11 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

Re: Do While loop

Posted 25 April 2010 - 08:34 PM

Ok, I am comparing my program to your program to see what mistakes that I did wrong. I am still confused with this do while loop mess. Its confusing business that I am trying to learn.

View PostChristoph, on 25 April 2010 - 07:24 PM, said:

Ok, I may be confusing you so I'll just try to break it down.

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;
}


Was This Post Helpful? 0
  • +
  • -

#12 Christoph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 233
  • View blog
  • Posts: 1,559
  • Joined: 23-August 09

Re: Do While loop

Posted 25 April 2010 - 08:37 PM

No problem, if all else fails read and re-read and read again. Some things may take more than one take until it catches on.
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Do While loop

Posted 25 April 2010 - 08:50 PM

Chris your if statment is pretty pointless. Here you only need a single do-while loop taaliyah. Its not hard to understand

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

Was This Post Helpful? 1
  • +
  • -

#14 Christoph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 233
  • View blog
  • Posts: 1,559
  • Joined: 23-August 09

Re: Do While loop

Posted 25 April 2010 - 08:54 PM

Not sure why I didn't catch myself on that one.
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Do While loop

Posted 25 April 2010 - 08:56 PM

View PostChristoph, on 26 April 2010 - 02:54 AM, said:

Not sure why I didn't catch myself on that one.


the way you implemented it works, theres just easier ways :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2