samedi 14 mars 2020

How do I prevent my data from being reset after incrementing it?

I am writing a basic c++ program where I have to display a triangular prism using "*" and " ". I am running into an issue where I want spacesOut to increment by two in the if (rowsDrawn % 2 == 1) section and again by two in the if (rowsDrawn % 2 == 0). Instead, the value is reset each time it is returned to the initial value at either the odd or even section. Is there a way to create a localized incrementation that isn't reset when spacesOut is redefined in another loop. Here is the product of the program... see how the spaces to the left of the first * fail to incerment by two for both the even and odd rows. Below the solution is my code. Thank you for your help.

Enter an odd side value between 7 and 11 for your Triangular Prism
9

 *
  * //start of 2nd part
*  *
  *
*  *
  *
*  *
  *
*  *//end of 2nd part
*
*          * *
*
*          * *
*
*          * *
*

Here is my code for the 2nd part specifically, I'm assuming that the solution to this part translates to the other parts as well

               spacesDrawn = 0;
               if (rowsDrawn % 2 == 1) {
                   spacesDrawn = 0;
                   spacesOut = (side / 2) + .5;

                   while (spacesDrawn < spacesOut) {
                       output = output + " ";
                       spacesDrawn++;

                   }
                   output = output + "*";
                   spacesOut +=2;//it does increment here by two but it is replaced by the spacesOut in the even if loop ... once it goes back to odd it is replaced by the inital spacesOut in the odd if loop

                   rowsDrawn++;
                   output = output + "\n";//maybe put output=output+\n at the end of the part two if loop




               }
                else if (rowsDrawn % 2 == 0) {
                   spacesDrawn = 0;
                   spacesOut = (side / 2) - 1.5;
                   while (spacesDrawn < spacesOut) {
                       output = output + " ";
                       spacesDrawn++;

                   }
                   output = output + "*";
                   spacesDrawn = 0;
                   spacesIn = 2;
                   while (spacesDrawn < spacesIn)
                   {
                       output = output + " ";
                       spacesDrawn++;
                   }
                   output = output + "*";
                   spacesOut += 2;
                   spacesIn += 3;
                   rowsDrawn++;
                   output = output + "\n";


               }


           }```







Aucun commentaire:

Enregistrer un commentaire