C++ not run an output properly - c++

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:
Not using namespace
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.

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?

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

Related

How to compile .cpp to a valid .cgi?

Below is my test.cpp file.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
cout << "Content-type:text/html" << endl << endl;
cout << "<html>" << endl;
cout << "<head>" << endl;
cout << "<title>Hello World - First CGI Program</title>" << endl;
cout << "</head>" << endl;
cout << "<body>" << endl;
cout << "<h2>Hello World! This is my first CGI program</h2>" << endl;
cout << "</body>" << endl;
cout << "</html>" << endl;
return 0;
}
Below is how I compiled the test.cpp file to test.cgi file.
g++ -g test.cpp -o test.cgi
Below is how I change mode of the test.cgi file.
chmod 755 test.cgi
There is no error at all.
But when I visit the page in the browser "localhost:8080/test.cgi", I get the following error:
C:/xampp/cgi-bin/test.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[cgi:error] [pid 22568:tid 1864] (9)Bad file descriptor: [client ::1:60380] AH01222: don't know how to spawn child process: C:/xampp/cgi-bin/test.cgi
The default cgi.cgi of Apache server works as well. It seems like the test.cgi file is invalid. Because I cannot even view the content of the file in Visual Studio Code, while I can view the content of the cgi.cgi file as well, the default file of Apache server.
When I run the following command line in the terminal, the content of the test.cgi file is printed successfully.
./test.cgi
How to create a valid cgi file from a cpp file?

What causes the following linking error with the boost c++ libraries?

Hi I get a linking error when compiling my program with the gcc compiler on cygwin. The first picture is a simple sample program from the boost filesystem libraries tutorial page where I have included filesystem.hpp in the boost folder. Beneath that is the picture of my linker error when I try to compile with the following command:
g++ -I C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0 -I C:/Users/Ejer/Desktop/c++Dep/eigen-3.4.0 -L C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0/stage/lib test.cpp -o ser
Here I try to compile my program test.cpp with the eigen and boost libraries and set the includer path that they tell me to set as the path after I have built the library with b2.exe. I have also linked to the lib files for boost. I have also tried linking to the different filesystem lib files specifically. Thanks in advance
#include <iostream>
#include <boost/filesystem.hpp>
using std::cout;
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: tut3 path\n";
return 1;
}
path p (argv[1]);
try
{
if (exists(p))
{
if (is_regular_file(p))
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p))
{
cout << p << " is a directory containing:\n";
for (directory_entry& x : directory_iterator(p))
cout << " " << x.path() << '\n';
}
else
cout << p << " exists, but is not a regular file or directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
I get a linking error when compiling my program
No, you don't. You are getting a linking error when linking your program, not when compiling it.
The reason: you didn't supply the library (-L C:/Users/.... tells the linker where to search for libraries; not which libraries to link). Your command line should look something like:
g++ -I ... -L ... test1.cpp -o ser -lboost_filesystem

reading large binary chunk > INT32_MAX with std::ifstream::read

I have a large file, about 4GB. I need to read a portion of that file that is bigger than INT32_MAX. std::ifstream::read() accepts a std::streamsize as second input (number of bytes to read). Since I'm on 64 bit, that is a typedef to ptrdiff_t which should be a int64_t.
So I would expect to be able to read 9223372036854775807 at once. My example below proves me wrong. The failbit is set when I read more than INT32_MAX.
What am I missing?
#include <fstream>
#include <iostream>
#include <limits>
int main() {
std::cout << "Maximum of std::streamsize: "
<< std::numeric_limits<std::streamsize>::max() << std::endl;
std::cout << "INT32_MAX: " << std::numeric_limits<int32_t>::max()
<< std::endl;
auto const filename = R"(C:\TEMP\test.dat)"; // a large file > INT32_MAX
auto dataStream = std::ifstream();
dataStream.open(filename, std::ios_base::binary);
dataStream.seekg(0, dataStream.end);
size_t filesize = dataStream.tellg();
std::cout << "Size of file: " << filesize << std::endl;
// buffer for the whole file
auto buffer = new uint8_t[filesize];
dataStream.seekg(0, dataStream.beg);
std::cout << "Reading INT32_MAX bytes..." << std::endl;
dataStream.read(reinterpret_cast<char*>(buffer),
std::numeric_limits<int32_t>::max());
std::cout << "Read failed: " << dataStream.fail() << std::endl;
dataStream.seekg(0, dataStream.beg);
std::cout << "Reading INT32_MAX + 1 bytes..." << std::endl;
dataStream.read(
reinterpret_cast<char*>(buffer),
static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1);
std::cout << "Read failed: " << dataStream.fail() << std::endl;
delete[] buffer;
}
I compiled with:
$ g++ --version
g++.exe (Rev2, Built by MSYS2 project) 9.2.0
Copyright (C) 2019 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.
On a Windows 7 Laptop gives:
Maximum of std::streamsize: 9223372036854775807
INT32_MAX: 2147483647
Size of file: 4001202702
Reading INT32_MAX bytes...
Read failed: 0
Reading INT32_MAX + 1 bytes...
Read failed: 1
I worked around this issue by reading multiple INT32_MAX sized chunks. I'm interested to know why this failed though.
EDIT: I did some more testing. compiled on Linux with GCC 8.3: works, compiled with MSVC15: works.
So I guess there's a problem with the libstdc++ that comes with MinGW-W64.
EDIT2: Problem still exists with
$ gcc --version
gcc.exe (Rev8, Built by MSYS2 project) 10.3.0
Copyright (C) 2020 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.
can you try this
auto buffer = new int32_t[filesize];

Why is cout flushing early?

I've been learning how flushing works with cout, so I decided to perform this quick test.
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
cout << "Line 1..."; // OR cout << "Line 1..." << flush;
usleep(500000);
cout << "\nLine 2" << endl;
cout << "Line 3" << endl ;
return 0;
}
In the case that is presented above, the expected output is for:
Line 1...
Line 2
Line 3
to print out altogether after some delay. However, in the scenario in which
"<< flush;" is included, the expected result is for Line 1 to print immediately, then after some delay, Line 2 and Line 3 print.
These expected outputs ONLY occur when I compile my program on a Linux machine using the command:
g++ -o myFile.out myFile.cpp -Wall
Then run it using:
./myFile.out
When I run these same code pieces on my windows machine, line 1 is ALWAYS displayed immediately, regardless of the insertion of "<< flush;". Why does this happen?
It should be noted that on my windows machine, I am compiling and running my code through codeblocks x64. According to Codeblocks Settings > Compiler > Toolchain Executables, my C++ compiler is "mingw32-g++.exe". Isn't this the same compiler as running g++ on Linux as I did earlier? Thanks!

Creating makefile dynamically and running make command automatically by the main program

I developed a code which create a .cpp file from a .isc file. This .isc file contains tons of lines with logic circuit information. My code read every line of this .isc file and write a code in a .cpp file that will simulate the logic of each .isc line, and this .cpp is saved in the same folder of the code which creates it. What I want to do is compile and run the executable of this .cpp file I created with a command line straight from my main code. I've been doing some researches and I found that a makefile could do that for me. About makefile I found some information here:
Can I compile all .cpp files in src/ to .o's in obj/, then link to binary in ./?
C++ makefile on Linux with Multiple *.cpp files
Based on that, After creating and writing the converted code in the .cpp file, I created a makefile (with dynamically name), here is it:
ofstream make_file("Makefile", ios::out | ios::trunc); //read and open the file
if (make_file == NULL ){ cout << "Error creating makefile!"; return 1; }
make_file << "# Makefile" << endl;
make_file << "# This makefile will run the new cpp file created\n" << endl;
make_file << "CC = g++\n" << endl;
make_file << "# FLAGS:" << endl;
make_file << "CFLAGS = -g -B -Wall\n" << endl;
make_file << "Executable target:" << endl;
make_file << "TARGET = " << netlist << "\n" << endl;
make_file << "all: $(TARGET)\n" << endl;
make_file << "$(TARGET): $(TARGET).c" << endl;
make_file << "\t$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).cpp\n" << endl;
make_file << "clean:" << endl;
make_file << "\t$(RM) $(TARGET)" << endl;
make_file.close();
So, my objective is to make this makefile compile the .cpp file and run its executable, assuming this is possible. If it is and I created the makefile in a correct way, how do I execute, or "make" it?
Edit: I'm using codeblocks