When the user enters the letter n as the major mark, the major mark will instead be represented by the number:

My code goes like this

#include <iostream>
#include <string>
using namespace std;

int main() {
    string z,zz ="";
    int x,y;
    cin >> x >> y >> z >> zz;
    for (int a = 1; a <= x; ++a) {
        cout << z;
      for (int b = 1; b <= y; ++b) {
         cout << zz;
      }
   }
   cout << z;
   return 0;
}

If my input is 3 9 n a

It should print out 0aaaaaaaaa1aaaaaaaaa2aaaaaaaaa3

But my code prints out naaaaaaaaanaaaaaaaaanaaaaaaaaan

Answer

If you:

  • Move the declaration of a outside of the outer for loop.
  • Change both cout << z; statements to cout << a;.
  • Change the outer for loop to start at 0 instead of 1, and to use < insted of <=.

Then you will get the output you want.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string z, zz;
    int x, y;
    cin >> x >> y >> z >> zz;
    int a;
    for (a = 0; a < x; ++a) {
        cout << a;
        for (int b = 1; b <= y; ++b) {
            cout << zz;
        }
   }
   cout << a;
   return 0;
}

Online Demo

Note that in this case, z becomes redundant and can be eliminated if you can remove n from your input.