0

This is a clip of my code

void start_hang(){
            cout << "*********************\n";
            cout << "                     \n";
            cout << "                     \n";
            cout << "                     \n";
            cout << "                     \n";
            cout << "                     \n";
            cout << "                     \n";
            cout << "*********************\n";
            cout << "=====================\n";
        }

But here is my output

********************* 
                            *********************=====================

Here is my other attempt to make it work:

  1. Not using namespace
  2. Put std:: in front of every output
void start_hang(){
        std::cout << "*********************\n";
        std::cout << "                     \n";
        std::cout << "                     \n";
        std::cout << "                     \n";
        std::cout << "                     \n";
        std::cout << "                     \n";
        std::cout << "                     \n";
        std::cout << "*********************\n";
        std::cout << "=====================\n";
    }

And still does not work.
Maybe I'm using class wrong?

Here is my full code:

https://pastebin.com/AmfZErqS

Here is my compiler:

g++ (tdm64-1) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
phuclv
  • 37,963
  • 15
  • 156
  • 475
Amirul Akmal
  • 401
  • 6
  • 13
  • 4
    what is your terminal? Produces expected output here: https://godbolt.org/z/7Ea8j4 – 463035818_is_not_an_ai Nov 20 '20 at 16:18
  • 2
    btw not `using namespace std;` is always good! (but not the cause of your current problem) – 463035818_is_not_an_ai Nov 20 '20 at 16:19
  • Looks like your console doesn't parse `\n` correctly. It could be some Windows/Unix incompatibilty perhaps? – Yksisarvinen Nov 20 '20 at 16:20
  • Try with `\r\n`? Unrelated opinion: I would change these functions to just one function and passing in an index, then either using a map or a switch statement (probably the latter) to output the hanged man. – Lily Nov 20 '20 at 16:22
  • @idclev463035818 in windows CMD terminal – Amirul Akmal Nov 20 '20 at 16:25
  • @ColonD just wanna ask , these function is in class ..is that effect anything? – Amirul Akmal Nov 20 '20 at 16:26
  • That should not effect anything that I know of – Lily Nov 20 '20 at 16:26
  • What compiler are you using? I've only seen this problem on ancient versions of Borland Turbo C++ on ancient versions of Windows. – Eljay Nov 20 '20 at 16:27
  • it should not but in general you should make sure that the code you posted really does reproduce your problem. See [mcve] – 463035818_is_not_an_ai Nov 20 '20 at 16:27
  • 1
    please add more information to the question. As the problem is not in the code, anything might help. Compiler, OS, used terminal – 463035818_is_not_an_ai Nov 20 '20 at 16:29
  • 1
    If you are using Windows CMD, how do you compile your code? What compiler etc.? I'd guess compiling under Cygwin and using the resulting executable in CMD would be one way to get such results. – Yksisarvinen Nov 20 '20 at 16:42
  • @Yksisarvinen g++ 4.9.2 – Amirul Akmal Nov 20 '20 at 17:24
  • Do not use `\n`, replace it with `<< std::endl`. On some terminals, newline is represented by `\r\n` while others use `\n` only, `std::endl` makes sure you use the proper one. – Everyone Nov 20 '20 at 17:30
  • 1
    @Everyone I can not find any resources that says that `std::endl` adds `\r`, it seems to be a common misconception because I have seen it three times in this thread so far (other answers got deleted). – Lily Nov 20 '20 at 17:40
  • 3
    @Everyone `\n` in C++ is not ASCII value 0x10, it's an escape sequence for new line. Depending on the target system, compiler will replace it with CRLF, LF or CR. – Yksisarvinen Nov 20 '20 at 17:40
  • @ColonD my info should be outdated based on some older tests I've done. When I used to print `\n` in VS to a file, the file would only show `\n` without `\r` and Notepad didn't display newline. I just did the test again and `\n` is replaced by `\r\n`. `std::endl` always was replaced with `\r\n` on Visual Studio, then and now. I redid the test to confirm. – Everyone Nov 20 '20 at 17:51
  • @Yksisarvinen true for now, but it wasn't always the case on VSC++. – Everyone Nov 20 '20 at 17:52
  • `cout << endl` simply does `cout.put(cout.widen('\n')); cout.flush()` internally. It is the output stream (in this case, `cout`) that translates printed `'\n'` characters into CRLF or LF depending on the platform, not the compiler. So `cout << "...\n"` is correct to use in this situation – Remy Lebeau Nov 20 '20 at 21:14
  • @Everyone in fact you should **never use `std::endl`** unless you really know what you're doing. `\n` will automatically be translated to the proper new line character [by the C runtime](https://stackoverflow.com/q/22887211/995714), for example to `\r` on classic Mac – phuclv Nov 21 '20 at 06:13

2 Answers2

0

Im compiling with g++ game.cpp which resulting to the error hmm
however , after i do g++ -g game.cpp -o game.exe , it gives an output and and the program works as it supposed to be
A working code

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Amirul Akmal
  • 401
  • 6
  • 13
  • 2
    `g++ game.cpp` produces `a.exe` instead of `game.exe`. Did you forget to run `a.exe` and just run `game.exe` from a previous compilation? – phuclv Nov 21 '20 at 00:19
  • 1
    @phuclv ooooh , i think that's why..i didnt realise there are `a.exe` instead `game.exe`.thankss..maybe the `game.exe` that i ran is from previous build that i accidentally created in VSCode while the error still populated :P – Amirul Akmal Nov 21 '20 at 02:00
0

The code is correct and there's no issue with it. The issue lies in the way you're compiling and executing.

On UNIX-like systems, the default name for the output binary from gcc is a.out (which is also an ancient executable format)

  • -o file
    • Place the primary output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.

      If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.

https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html

On Windows it's a.exe by default. You must specify the name using the -o option, or you'll have to run a.exe. So if you compile with g++ game.cpp then run game.exe then you're executing some old buggy game.exe compiled before

See Why does my GCC compiler not compile C code?

phuclv
  • 37,963
  • 15
  • 156
  • 475