Unable to print string by using cout << method(); where method() returns a string

3480 views c++
3

following is my code

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

string m(int x) {
    string h="";
    char q;
    for(int i=0;i<x;i++) {
        q=i;
        h +=q;
    }
    return h;
}

int main() {
int x;
cin >> x;
cout << m(x) << endl;

return 0;
}

but this is my output ,what are these strange looking square symbols. enter image description here

answered question

What do you think printing the character representation of the integer 17 (for example) is going to look like? Take a look at this ASCII table and see what the numeric values for "normal" characters are. Notice that none of them are less than 23.

Also, please reconsider your use of what are often considered bad practices: using namespace std; and endl (those are links to explanations).

but i haven't seen those symbols before

The funny squares are basically your terminal giving up, saying "this character you want me to print is some unprintable garbage, so here's a dummy character instead". If you want to print out the numbers, so 123456789101112131415..., see acraig5075's answer.

@BoBTFish i agree but the whole purpose it to keep the code as short and precise as possible.

"Short" and "precise" are two completely unrelated ideals. Making your code shorter often makes it harder to understand (for a human at least), therefore less "precise" in the sense that the exact intended meaning is not being effectively conveyed. endl is less precise than '\n' because it does extra stuff you probably didn't expect, and is exactly the same number of characters.

If the value of x do not correspond to the ASCII value of a printable character, this is what you ll get!

5 Answers

3

Strings are strings, and ints are ints. Don't mix them up and expect magic.

Try this:

h += std::to_string(q);

posted this
4

Your terminal is likely trying to interpret the string as either ASCII or UTF8. Either way, most of the characters with a value up to 23 are going to be unprintable control characters.

If you started your loop at 33 instead of 0 you would get more sensible output.

posted this
9

You are mixing integers with strings. To concatenate string, first you have to convert a character,int to string. try the code below:

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

string m(int x) {
    string h="";
    char q;
    for(int i=0;i<x;i++) {
        q=i;
        h += std::to_string(q);
    }
    return h;
}

int main() {
int x;
cin >> x;
cout << m(x) << endl;

return 0;
}

posted this
7

The second line of for loop should be like this

h +=to_string((int)q);

posted this
12

First of all, what you are doing is essentially printing chars with values from 0 to x. If you look up the ascii table here: http://www.asciitable.com/ you can look at the numeric value of the character and see what it represents as a char. If it can not be represented, then console prints it out as squares you are seeing.

Now you are saying you want your code to be short and precise. Let me rework that a bit.

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

string m(int x) {
    string returnVal;
    for (int i = 0; i < x; i++)
        returnVal += (char)i;
    return returnVal;
}

int main() {
    int input;
    cin >> input;
    cout << m(input) << endl;
    return 0;
}

Using letters are variable names is a terrible practice. Avoid that at all costs. Name your variables intuitively, so they are self describing. That is more important than having a "Short" and "precise" code.

Concerning that, you do not need the additional variable you named Q. You can directly typecast it into a char. Also avoid using {} if you can. It decreases code complexity and increases readability

posted this

Have an answer?

JD

Please login first before posting an answer.