i have been told to use a do while loop instead of a goto statment,

here is a code i made to try the do while loop,

int main()
{
	int A = 0;

	cout << "Enter a number from 1 - 5 " ;

	cin >> A;
	
		do 
		{
			cout << "your number was 1";
			cout << endl;
		}while (A == 1);

		do 
		{
			cout << "your number was 2";
			
		}while (A == 2);

		do 
		{
			cout << "your number was 3";
			
		}while (A == 3);


		do 
		{
			cout << "your number was 4";
			
		}while (A == 4);

		do 
		{
			cout << "your number was 5";
			
		}while (A == 5);

but when i run this program it goes through every loop
i understand that even if it did work properly it would be a infinate loop, so next question is how do i exit the do while loop?

1.
      int main()
   2.
      {
   3.
      int A = 0;
   4.
       
   5.
      cout << "Enter a number from 1 - 5 " ;
   6.
       
   7.
      cin >> A;
   8.
       
   9.
      do
  10.
      {
  11.if(A == 1)
      {
      cout << "your number was 1";
  12.
      cout << endl;
  13.
      }

  14.
       
  15.
      
  16.if(A == 2)
      {
  17.
      cout << "your number was 2";
  18.
       
  19.
      }
  20.
       
  21.
      if(A ==3)
  22.
      {
  23.
      cout << "your number was 3";
  24.
       
  25.
      }
  26.
       
  27.
       
  28.
      if(A == 4)
  29.
      {
  30.
      cout << "your number was 4";
  31.
       
  32.
      }
  33.
       
  34.
      if(A == 5)
  35.
      {
  36.
      cout << "your number was 5";
  37.
       }
  38.
      }while (A <= 5);

kdcorp87, use the preview button once in a while....

i have been told to use a do while loop instead of a goto statment,

That is an excellent suggestion. Although in some cases a while loop would work, too.

but when i run this program it goes through every loop
i understand that even if it did work properly it would be a infinate loop, so next question is how do i exit the do while loop?

Um, yeah...? Code goes from statement to statement. If you have loop after loop after loop, the code executes each loop. Why are you surprised at that?

To break out of a loop, change the value the loop is using as an exit condition to make the while fail -- a comparison yielding false .

int a = 0; 
	cout << "Please enter the number between 1-5: ";
	cin >> a;   // ask user to enter value btw 1-5
	do     // run do-while loop to control the value of a.
	{
// check if the value of a is between 1 and 5
// if a is between 1-5 then show the entered value and exit the 
		if(a>=1 && a<=5)   

		{
		cout << "Your number was " << a << endl;
		break;
		}
		else    // else exit the loop if the if-statement is false.

			break;
		}while(true);

i am also learning c++ i started my course two weeks ago and it's only fun.
all c++ experts if there is something wrong with my code or logic please kindly tell me.

I am not an expert but there is a logical flaw. You use the do...while loop when you want the thing to loop. By adding in a break whether or not it is between 1-5 you defeat the purpose of a loop. you could do the same code with just an if statement.

Much simpler to write it like this.

int a;
	cout << "Please enter the number between 1-5: ";
	cin >> a;   // ask user to enter value btw 1-5

		if(a>=1 && a<=5)
            cout << "Your number was " << a << endl;

If you need the code to return to the beginning if the entry is not between 1-5

int a;
	do{
                cout << "Please enter the number between 1-5: ";
	        cin >> a;   // ask user to enter value btw 1-5
        }while (a<1||a>5);
		if(a>=1 && a<=5)
            cout << "Your number was " << a << endl;
This article has been dead for over six months. Start a new discussion instead.