0

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

Shouldn't a string be printed because on every iteration a character is added.

shobhit
  • 91
  • 3
  • 12
  • 1
    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](https://en.cppreference.com/w/cpp/language/ascii) and see what the numeric values for "normal" characters are. Notice that none of them are less than 23. – BoBTFish Aug 15 '18 at 06:41
  • 1
    Also, please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Aug 15 '18 at 06:44
  • but i haven't seen those symbols before – shobhit Aug 15 '18 at 06:45
  • 2
    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 Aug 15 '18 at 06:46
  • @BoBTFish i agree but the whole purpose it to keep the code as short and precise as possible. – shobhit Aug 15 '18 at 06:46
  • "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. – BoBTFish Aug 15 '18 at 06:49
  • If the value of `x` do not correspond to the ASCII value of a printable character, this is what you ll get! – PhoenixBlue Aug 15 '18 at 06:52
  • 2
    Please edit your question and say what it is you expect to see. "12345..." or "abcde...". As it stands, your question is not clear. – acraig5075 Aug 15 '18 at 07:04
  • @BoBTFish no less than 32. Or 20h. 32 is space, all characters below it are unprintable (theoretically) because they have special meaning. On practice some of them have visual representation. – Swift - Friday Pie Aug 15 '18 at 08:43

5 Answers5

4

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;
}
Saboor
  • 352
  • 1
  • 10
3

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.

alanwj
  • 539
  • 5
  • 5
1

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

Try this:

h += std::to_string(q);
acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • @paxbun That is **entirely** the cause of the problem, which acraig5075 is suggesting a solution for (based on the reasonable assumption that the asker really wanted to print out the numbers, not the represented characters - that's not entirely clear from the question though). – BoBTFish Aug 15 '18 at 06:56
  • @BoBTFish I didn't see the comments. By the way, I deleted my comment at the same time you answered, as I realized it. The content was 'q is char, which means h += q is equivalent to h.push_back(q).' – paxbun Aug 15 '18 at 06:58
0

The second line of for loop should be like this

h +=to_string((int)q);
Mukarram Ishaq
  • 748
  • 1
  • 8
  • 18
  • Why do you have the explicit cast to `int`? – BoBTFish Aug 15 '18 at 07:00
  • Because I think he wants to print numbers through string. So a `char` needs explicit cast to `int` before concatenation with a string. – Mukarram Ishaq Aug 15 '18 at 07:07
  • I agree with your interpretation of the question, but why do you think `char c = 5; std::to_string(c);` won't work? I.e. instead of calling `to_string` with a `char`, you cast to an `int` first? – BoBTFish Aug 15 '18 at 07:10
  • Actually, When you give an `int` value to a `char` variable, that `int` value is treated as ascii or unicode value implicitly (according to the encoding). – Mukarram Ishaq Aug 15 '18 at 07:13
  • `char y = 49; std::cout< – Mukarram Ishaq Aug 15 '18 at 07:29
  • If you read my previous comment, I am suggesting `std::cout << std::to_string(y)`. Is there a reason you cast to an `int` *inside* the `to_string` call? (I am politely trying to suggest it is unnecessary, but there might be a reason I don't know about.) – BoBTFish Aug 15 '18 at 07:36
  • Yes, you are right, explicit cast to `int` is unnecessary in this case. Because `std::to_string()` converts `char` value to its ascii value and then paints the ascii value as a string. – Mukarram Ishaq Aug 15 '18 at 08:05
0

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 as 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

Erik9631
  • 87
  • 9
  • I disagree about avoiding `{}` around the body of the `for` loop. Even though it is only one line, using a consistent code layout makes the entire project more readable, and makes it harder to misinterpret the size of the loop body. Whitespace often gets messed up when two people with different editor settings touch a file, and braces offer some protection against that ruining readability. Using braces even for a single line body protects against someone accidentally making that into a multi-line body but not adding the braces. Especially if that happens through using a (poorly written) macro. – BoBTFish Aug 15 '18 at 07:04
  • Using braces for multi line code layouts while avoiding them at single line layouts does not break consistency. There is a system in that. Also according to the studies, from a psychological perspective of a human being, reading a code with less braces is faster and more efficient, since there is less junk you have to filter out, it is a coding standard used among many programmers. It is advantage is more useful in functions with many sequential if statements. Standard C++ editors all support this. To add to this, any standard C++ code should be tabbed with 2 spaces, not 4. – Erik9631 Aug 15 '18 at 07:11
  • @Erik9631 it's actually up to code standard used at particular place of employment. It's considered bad for most merging and code checking tools to avoid closure braces in code, conflict resolution may be more prone to errors. All formatting issues are also up to code standard of employer. – Swift - Friday Pie Aug 15 '18 at 09:02