program is adding too many characters to string

closed account (1Ck93TCk)
Hello all,

I'm doing this program as a school assignment and I'm really stuck. It's supposed to draw an x with a character that the user chooses. It's supposed to look like this:
#.....#
.#...#.
..#.#..
...#...
..#.#..
.#...#.
#.....#

I've only got the program figured out part way, but I'm running into unexpected results. The program first creates a string of (.) with the correct number of characters (chosen by user) Then it uses replace to put the characters in their specific location. Output adds 45 characters in front of the string.

############################################....

I've tried different values in the row.replace(), but it doesn't seem to change anything. Where am I getting all of these characters from?

Thanks in advance!

BTW, since this is homework and I'm supposed to be learning from it, I don't really want it solved for me, I just want to be pointed in the right direction. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  #include <iostream>
#include <string>

using namespace std;

int oneLine(string& row, int howBig, char symbol);

int main()
{
    char symbol;
    string row;
    int howBig;
    int rowCounter;

    cout << "This program draws a big X" << endl;
    cout << "Enter the number of rows you want: ";
    cin >> howBig;
    cout << "Enter a character for drawing the X: ";
    cin >> symbol;

    for (rowCounter = 0; rowCounter < howBig; rowCounter++)
    {
        oneLine(row, howBig, symbol);
    }
    return 0;

}
int oneLine(string& row, int howBig, char symbol)
{
    int indent;
    row.insert(0, howBig, '.');

    row.replace(indent, 1, '.', symbol);
    cout << row << endl;
    row.clear();


    return 0;
}
Hello jmb,

Four things I see with your function "oneLine":

First it returns a value that is never used.

Second int indent; reserves space for the variable, but it only contains a garbage value at this point and is never receives a proper value before it is used in the "replace" function.

This means that your "replace function could look like:
row.replace(-858993460, 1, '.', symbol);
Since the numeric parameters are all "size_t" AKA "unsigned int" this will not work.

Third the third parameter I believe should be a (1) not the '.' that you have.

Forth the value of "indent" never changes. Even if you use int indent{}; and initialize the variable to (0) zero it will do this each time the function is called.

I also noticed after some testing that it might be better to define "indentB" and "indentE", (The 'B' for beginning and the 'E' for end) of the line, and do this in "main" then pass the variables to the function and change their values after the function call in the for loop.

When 'B' and 'E' are the same number you would then have to subtract from 'B' and add to 'E'.

Last since a function should do only one thing I would move the "cout" statement of line 34 to the for loop in "main".

Hope that helps,

Andy

Edit:
P.S. I would also move the row.clear(); to the for loop in "main" since you are passing "row' by reference.
Last edited on
Hello jmb,

Sorry I was interrupted and forgot to mention http://www.cplusplus.com/reference/string/string/replace/

I have been working on the changes I mentioned and it is working better. In order to print the bottom half of the 'X' i used an if/else in the for loop in main to either add and subtract ot to subtract and add to the "indent?" variables.

Hope that helps,

Andy
Hello jmb,


Then it uses replace to put the characters in their specific location. Output adds 45 characters in front of the string.



When you look at the above link pay attention to the third parameter of ".replace(...)".

Andy
closed account (1Ck93TCk)
I made the changes you recommended and now all of those extra characters are gone. Thank you!!
closed account (1Ck93TCk)
Andy,

Thank you for being so helpful and so quick about it!
I looked at the link and I have questions. I'm still very new to this. I copied and pasted a couple of lines from the top of the page. When I tried using 3 parameters, like the first example, it wouldn't run at all. The second example looks like it has 5 parameters. I assume const string& str represents the character that I want to insert. I'm a little confused about the rest of it. Currently it's working with 4 parameters, but I'm not sure what the third is supposed to represent. I thought at first that it was the character you want to replace, with the 4th parameter being the character that will replace it. But it's working fine with just a 1 there instead of a character.

string& replace (size_t pos, size_t len, const string& str);

string& replace (size_t pos, size_t len, const string& str,size_t subpos, size_t sublen);

Please help me understand.
Thank you!

JMC
Hello jmb,

Looking a little closer to the link I posted it appears the you are using number (5) fill.

Looking at what you are using row.replace(indent, 1, '.', symbol); VS tells me the parameters are; (size_t _Off, size_t _NO, size_t _Count, char _Ch).

Where the first parameter is the offset from the start of the string, i.e., (string[0]), this is referred to as "pos" in the link.

The second parameter is the number of characters to replace. Referred to as "len" in the link.

The third parameter is number of characters to copy. Referred to a "n" in the link. The problem you are having is that the decimal value of "." is 46 copies when you only need (1).

The forth parameter is what you want to replace with. Referred to as 'c' in the link.

What you could use is the first definition of replace using three parameters which is line 16 in the code. Also everywhere it says char symbol "char" would have to change to "std::string".

Hope that helps,

Andy
closed account (1Ck93TCk)
Yes, that clears things up a lot for me. Thank you!
Topic archived. No new replies allowed.