can't get hello, world program to work - d

I'm trying to get the hello, world program from D (The Programming Language)/d2/Hello, World! to work on codepad.org. Here's my code:
/* This program prints a
hello world message
to the console. */
import std.stdio;
void main()
{
writeln("Hello, World!");
}
Here's the output I get when I run it at http://codepad.org/MdLVQEMm:
Line 9: Error: undefined identifier writeln
Line 9: Error: function expected before (), not writeln of type int
Any ideas as to what I'm doing wrong? I pretty much just copy / pasted the code..

From their about page, codepad.org uses D version 1.026, which was released in 2008. For reference, the current version is 2.074.1.
It's unlikely that modern D code will work with such an old version of D, especially after a major version bump. You'll have to use a different service with updated tools.

Use https://run.dlang.io for an online editor backed by an up-to-date compiler.

Related

Why does my c++ program output garbled code

I use MinGW64 to compile c++ programs. But since I upgraded to Windows 10, I found my c program output Chinese will be garbled code.
I follow the online method, adding a code in the program header: SetConsoleOutputCP(65001);, then it fixes. but I think it's so troublesome to do this for each c++ program. What should I do?
I think this is my system's problem, the same code in Windows 7 is not a problem, I just want to find a more convenient solution instead of adding the same code to every file
There's the code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
SetConsoleOutputCP(65001);
cout << "文本" ; //Output will be garbled code if there's no line above
return 0;
}
The console in most operating systems only expects ASCII character input. In order to show some other char set you have to specify that in your code. The SetConsoleOutputCP command sets the "code page" windows should read from. By the way not all versions of windows have the same code for this command.
Please refer to the documentation found here.
The documentation suggests using EnumSystemCodePages to make sure the code for that language exists on that system.
P.S.
Your English is very good :)
EDIT
I tested your code on my computer with Visual Studio 2019 and got the following
Warning C4566 character represented by universal-character-name '\u6587' cannot be represented in the current code page (1255)
even with the SetConsoleOutputCP command you added. I assume you need to have chines installed for this to work. The problem is that I don't have the relevant code page for the windows console to look in for the char set. see this answer and this answer.

Migrating flex 2.5.4a to 2.6 (lexical analyser generator)

I have a file that generates cc code using flex. When I use the version 2.5.4a-10 the codes works as expected.
If I use bit more recent version 2.5.37 or even newer like 2.6 the generated code seems not to allocate anything. It uses some pointers defined with nullptr and crashes.
I think the syntax has changed in between these versions. I find it also strange that Debian/Ubuntu have a package called flex-old saying:
flex is a tool for generating scanners: programs which recognize lexical
patterns in text. This is the old 2.5.4a version, which is no longer
being developed. You should normally choose flex, unless you have
legacy lexer files that do not work with a modern flex.
This product includes software developed by the University of California,
Berkeley and its contributors. The upstream source code can be found at
http://flex.sourceforge.net/
(Editor's note: Flex has moved to Github but v2.5.4a is not there.)
That version seems to be a big deal for others I suspect. Getting to my question:
Is there any manual or guide of what I have to do in order to port that code to generate some c++ code that works in more recent versions of flex?
EDIT: Here is my simple example taken from something larger:
int num_lines = 0, num_chars = 0;
%%
\n ++num_lines; ++num_chars;
. ++num_chars;
%%
int main()
{
yy_init=1;
yylex();
printf( "# of lines = %d, # of chars = %d\n",
num_lines, num_chars );
return 0;
}
flex it with flex file.l and build it with gcc lex.yy.c -lfl. Now, if you used version 2.5.4 it will work. With later versions it translates and compiles just fine, but when you run the program you will get segmentation fault.
I found the problem myself. The variable yy_init can be explicitly set in that old version. In newer versions it is not allowed. I'm not sure if that is intended, maybe someone can explain why this behavior is observed. I find it a bit strange.
If someone has a similar problem, you might want to take a look at the yy_init variable. Other than that I had no issues.

How to pass a value from a C++ executable to a Rails app?

I have a Rails app that runs a C++ executable from the command line. I'm able to print the C++ cout to the command line, but I'd like to assign it back to a variable, output = in a Rails controller.
Is this possible?
Below is a simplified example of my Rails controller action and C++ .cpp to help explain the question.
Rails controller:
def get_variable
system("cd ~/workspace/OutputTest/src && ./output.bin")
end
Note I've already compiled and created a C++ executable file named output.bin.
C++ file:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!!!";
return 0;
}
I'm familiar with Rails (not necessarily running files from the command line through Rails), but a complete newb to C++.
Any guidance would be very appreciated. If there is another approach I should be taking completely, that would also be very helpful to know.
I would prefer not to do this inline, as I'd like to do a lot more in C++ once I can solve this initial step.
UPDATE
The best solution I've come up with so far is writing a string to a .txt in C++, and then using File.read() in Rails. But it doesn't feel scalable, or give flexibility over data structure.
So I'm still wondering if there is a more straightforward way, like somehow keeping the value in memory for Rails to access it. Not sure though.
Maybe you have already tried this, but there is a backtick operator in ruby, which returns the output of a command as a string.
example:
def system_date
`date`
end
puts system_date #=> "Wed Nov 16 18:59:28 CET 2016"
in your case it would be
def get_variable
`~/workspace/OutputTest/src/output.bin`
end
The best idea I thought of is running the C++ app, piping (|) the input to rails (if that works) and doing $stdin.gets(). I'm not a ruby expert, and I haven't used it much, but I think it should work. Try
./cppapp | rubyapp
on a bash terminal.
You can capture an output of your programm with pipe:
test.cpp:
#include <iostream>
int main() {
std::cout << "Hello world!!!";
return 0;
}
Compile it: $ g++ test.cpp -o hello
test.rb:
var = IO.popen("./hello") do |cmd|
cmd.read
end
puts var
#=> "Hello world!!!"
More information about pipe reading can be found in documentation: IO.popen
You can use the back-tick operator to capture the output of command line executions in ruby.
irb(main):008:0> x = `echo foo`
=> "foo\n"

Simple D program Output order is wrong

I am learning a new language called "D" but i have a problem when trying to write a simple program
import std.stdio;
void main()
{
double gradeOne;
writeln("Please enter the First Test Grade: ");
readf(" s", &gradeOne);
}
Why does my program ask me for the input first before the output message?
I think its just the DDT problem, when i run the program in command prompt its working fine
Output to Eclipse buffers output by larger data blocks rather than lines. To force output to appear, insert calls to stdout.flush(); before asking for input to ensure it shows up when you want it.
See also: Eclipse console writes output only after the program has finished

execl - No memory available to program now (OS X / XCode / C++)

I'm trying to execute a very simple program that runs "ls" command
Im working under Mac OS 10.7, with XCode and C++
This is the code:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world" << endl;
execl("/bin/ls","ls",NULL);
return 0;
}
It crashes after following output
Hello world
No memory available to program now: unsafe to call malloc
I tried to google it but no luck, any ideas on what I might be doing wrong?
This is just "my opinion"
From man page:
The exec family of functions replaces the current process image with a
new process image.
It could be that it tries to replace the debugger process and so it crashes (the app is run from Xcode..). If you execute the app from command line it works...
Seems to work fine:
http://ideone.com/8AoZ3
But seems like on your platform some sort of weird recursion is taking place. Can you change your call to:
execl("/bin/ls","/bin/ls",0);
I know this may not be exactly what you want to do, but the following SO question is using execv to execute echo:
how-to-create-a-process-on-mac-os-using-fork-and-exec