-5

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;
}
Chase
  • 87
  • 1
  • 6
  • As far as I'm aware you can concatenate a string using `+` but not remove a section using `-` – Dillanm Mar 01 '17 at 11:24
  • 1
    You can use the [`erase()`](http://en.cppreference.com/w/cpp/string/basic_string/erase) function to _"decrement"_ the string. – πάντα ῥεῖ Mar 01 '17 at 11:25
  • 4
    Please don't try to guess C++ syntax. Read your book and documentation instead. – Baum mit Augen Mar 01 '17 at 11:26
  • Read the assignment **carefully**. You are supposed to use a loop to display Pattern A, **and another loop** to display Pattern B. – n. m. could be an AI Mar 01 '17 at 11:26
  • Plenty of methods available: `.resize( )` would work as well. Or `.pop_back( )`. None of these are content-sensitive though, they'll remove anything. – MSalters Mar 01 '17 at 11:26
  • @n.m.: I don't think that's actually possible. The exercise clearly states that the patterns have to appear side by side, and C++ has no way to move the cursor up again. If a first loop prints pattern A, the second loop prints pattern B _below_ pattern A which is specifically banned. – MSalters Mar 01 '17 at 11:29
  • Why not keep a counter for the number of '+' symbols that need to be displayed in A and increment it, and a counter for B and decrement it? The excersize doesn't demand use of strings. You can move the cursor in the console, but the functions that do that are OS dependent – Eyal K. Mar 01 '17 at 11:34
  • @MSalters I have no idea what's in the head of his instructor (probably not much) but I would interpret this as a requirement to print a single line of Pattern A using a loop, then a single line of Pattern B using another loop. – n. m. could be an AI Mar 01 '17 at 11:36
  • @Chase Gould It looks like the task is impossible. At least I can't imagine now how to use two loops one after another to output the patterns side by side. – Vlad from Moscow Mar 01 '17 at 11:45
  • @n.m.: Fair idea, that's the closest you could get. Still a third loop for the lines, but at least it has _enough_ loops. Not what I'd use though. (`std::string` ctor taking char and count) – MSalters Mar 01 '17 at 15:59

3 Answers3

0

You can't 'subtract' strings in c++. There is no '-' operator for strings.

One approach is to use string.substring() to get a 'shortened' version of your original string:

b=b.substr(0, b.size()-1); // copy all but last character of b to b
Ken
  • 4,367
  • 4
  • 28
  • 41
0

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;
}
Eyal K.
  • 1,042
  • 8
  • 23
  • This helped me a lot. When I run this code the last 3 lines of pattern b are out of line. I can't seem to understand why that is. – Chase Mar 01 '17 at 12:40
  • 1
    It's because of the way tabs behave - just output `20-aCount` spaces or something instead to align the start of B. – Useless Mar 01 '17 at 12:49
  • I solved this by replacing the line cout << "\t\t\t\t"; with: if (aCount <= 7 ) cout << "\t\t\t\t"; if ( a Count > 7) cout << "\t\t\t"; – Chase Mar 01 '17 at 12:52
  • Thanks @Useless that looks cleaner. – Chase Mar 01 '17 at 13:06
  • Yeah, go with what Useless said. I didn't actually test this code :) Also, in case my interpretation of the assignment is wrong, it is possible to print all lines of pattern A first, and then pattern B, but that requires using OS dependent console functions, which I doubt is what the instructor meant – Eyal K. Mar 01 '17 at 13:13
0

There is no operator operator - in the class std::string. You should use the method erase instead,

However if you will output strings for each row then there will not be two loops one after another.

It seems the assignment means something like the following

#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        const char c1 = '+';
        const char c2 = ' ';

        std::cout << "Enter a non-negative number (0 - exit): ";

        unsigned int n;

        if ( not ( std::cin >> n ) or ( n == 0 ) ) break;

        std::cout << '\n';

        for ( unsigned int i = 1; i <= n; i++ )
        {
            for ( unsigned int j = 1; j <= i; j++ )
            {
                std::cout << c1;
            }

            std::cout << std::setw( 2 * n - i ) << std::setfill( c2 ) 
                      << std::right << c2;

            for ( unsigned int j = n - i + 1; j != 0; j-- )
            {
                std::cout << c1;
            }

            std::cout << '\n';
        }

        std::cout << std::endl;
    }

    return 0;
}

The program output might look like

Enter a non-negative number (0 - exit): 10

+                   ++++++++++
++                  +++++++++
+++                 ++++++++
++++                +++++++
+++++               ++++++
++++++              +++++
+++++++             ++++
++++++++            +++
+++++++++           ++
++++++++++          +

Enter a non-negative number (0 - exit): 9

+                 +++++++++
++                ++++++++
+++               +++++++
++++              ++++++
+++++             +++++
++++++            ++++
+++++++           +++
++++++++          ++
+++++++++         +

Enter a non-negative number (0 - exit): 8

+               ++++++++
++              +++++++
+++             ++++++
++++            +++++
+++++           ++++
++++++          +++
+++++++         ++
++++++++        +

Enter a non-negative number (0 - exit): 7

+             +++++++
++            ++++++
+++           +++++
++++          ++++
+++++         +++
++++++        ++
+++++++       +

Enter a non-negative number (0 - exit): 6

+           ++++++
++          +++++
+++         ++++
++++        +++
+++++       ++
++++++      +

Enter a non-negative number (0 - exit): 5

+         +++++
++        ++++
+++       +++
++++      ++
+++++     +

Enter a non-negative number (0 - exit): 4

+       ++++
++      +++
+++     ++
++++    +

Enter a non-negative number (0 - exit): 3

+     +++
++    ++
+++   +

Enter a non-negative number (0 - exit): 2

+   ++
++  +

Enter a non-negative number (0 - exit): 1

+ +

Enter a non-negative number (0 - exit): 0
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335