Search code examples
c++loopsfor-loopstring-operations

C++ beginner no match for 'operator-'


I've ran into an issue with one of my intro to c++ labs. I have posted the instructions to this lab and my code below. I would like point out that Pattern B must be displayed beside pattern A(exactly as shown) and not below it.

C++ Lab Assignment

When I attempt to build this code I get the error no match for 'operator-' on the line b = b-y. Is there a simple method of decrementing the symbols within the string? The instructions state to use a second loop for pattern B but I'm not sure where to start with that. I would really appreciate any advice that will steer me in the right direction to completing this assignment. Thank you.

#include <iostream>
#include <string>

using namespace std;

int main()
{

    string a = "*";
    string b = "**********";
    string y = "*";


    for (int i=0; i <= 9; i++) {
        cout << a << "       " << b << "\n";
        a = a + y;
        b = b - y;
    }


    return 0;
}

Solution

  • The way I understood it:

    #include <iostream>   
    using namespace std;
    
    int main()
    {
        int aCount = 1;
        int bCount = 10;
        while(bCount > 0) 
        {
            // Loop over A
            for (int i = 0; i < aCount; i++)
            {
                cout << '+';
            }
    
            // Output a few tabs to separate the samples
            cout << "\t\t\t\t";
    
            // Loop over B
            for (int i = 0; i < bCount; i++)
            {
                cout << '+';
            }
    
            // Go to next line
            cout << endl;
    
            aCount++;
            bCount--;
        }
    
        return 0;
    }