-5

I was experimenting with a statement in C++ using online compilers. When I try to run this specific code

cout << num[i] + " " + num[i];

The online compilers give no output. I can change the + symbol to << but I want to know the reason that the code does not give any output on these online compilers.

Online compilers that I tried are onlinegdb, programiz, and jdoodle.

#include <iostream>
#include <string>

int main() {
    std::string num = "123";
    int i = 0;
    std::cout << num[i] + " " + num[i];
    return 0;
}
wovano
  • 4,543
  • 5
  • 22
  • 49
  • What does the error message say??? – Mark Benningfield Apr 23 '22 at 18:15
  • @MarkBenningfield There is no error, In one online compiler it gives X X X, another compiler it gives nothing. I want to know why it fails. – Yash Vaishnav Apr 23 '22 at 18:23
  • 3
    https://stackoverflow.com/questions/22261891/string-plus-char-what-is-happening – Raymond Chen Apr 23 '22 at 18:27
  • 2
    `num[i] + " " + num[i]` => `char + char* + char` => `int + char* + int` => `char* + offset` => " "[98]` (98 is 2 * 0x31) . Which is junk at offset 98 from you space literaal – pm100 Apr 23 '22 at 18:30
  • 1
    @YashVaishnav (Moved my comment from the answer section to the main section). C++ is not Python or JavaScript. The basic issue is that you are using other computer languages as a model in writing C++ code. Don't do this. If you do this, all you will wind up with are programs that are buggy, inefficient, or just look weird to an actual C++ programmer. C++ is a complex language that must be learned independent of whatever other language you may have used before. Your example of using `+` to concatenate numbers is a prime example of being burned by using other languages as a model. – PaulMcKenzie Apr 23 '22 at 18:38
  • @YashVaishnav You invoked undefined behavior. There is no guarantee of anything crashing in C++ if you do something wrong. C++ works differently than Python or JavaScript in this regard. You do something "dumb" in C++, there is no guarantee of a crash -- the program may work, may crash, may work today and crash tomorrow, etc. That's why you cannot approach C++ as you would with those other languages. – PaulMcKenzie Apr 23 '22 at 18:42
  • @YashVaishnav to be clear your program is not valid c++ - I merely showed you what most c++ compilers would do with it. Its a very long winded way of writing `" "[98]` which invokes UB. I was surprised that it compiled with no complaint what so ever on my machine – pm100 Apr 23 '22 at 23:30

1 Answers1

0

C++ is not like JavaScript or many higher-level languages, as in you may not delimit you data with +'s or ,'s. As shown in Lewis' answer, each item you wish to have printed must be separated by an insertion delimiter (<<). As for extracting, you may use the extraction delimiter (>>).

In your case, you are doing mathematical operations on the the characters themselves (adding together their numerical ASCII representations together, which could print unprintable and invisible characters). The printable ASCII characters range from 32 (space character) to 127 (delete character) (base 10). When summing '1' + ' ' + '1' you are left with (49 + 32 + 49) or (130) which exceeds the printable character range. Or you may also be accessing garbage as @pm100 said in the comments due to pointer arithmetic.


Here is an example of using the insertion operator:

#include <iostream>

int main(void) {
    int some_int = 1;

    std::cout << "this is my " << some_int << "st answer on StackOverflow :)"
    << std::endl;

    return 0;
}

And as for the extraction operator:

#include <iostream>

int main(void) {
    int num;

    std::cout << "Enter an integer: ";
    std::cin >> num; // stores the input into the `num` variable
    std::cout << "The number is: " << num << std::endl;
    return 0;
}

Pointer arithmetic:

const char* get_filename(const char* _path, size_t _offset) {
    return (_path + _offset);
}

// This is an example
//
// path = "path/to/my/file/file.txt";
// offset  ^               ^
//         0               |
//        + 16 ------------|
// path = "file.txt";
Gpine185
  • 44
  • 8