Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to make a single loop for two different variables?

I am looking for a way to make a single loop for two variables. I have a "p1" (player 1) and a "p2". Both have the particularity to use the same loop to have a certain number of spaces. Is there a solution to make a single loop instead of creating two? Here is the loop in question for the player one:

    for (string::size_type i = 1; i < (SIZE - p1.name.length()) / 2; i++) {
   
        cout << " ";
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Yes, you separate the initialization and updates with commas (,) and convert the condition to a compound one.

for (string::size_type i = 1, string::size_type j = 1; i < (SIZE - p1.name.length()) / 2 && j < (SIZE - p2.name.length()) / 2; i++, j++) {
   
        cout << " ";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading