Lua Function Not Doing Anything - c++

I finally was able to get Lua running with my compiler using C++. I now tried starting some tutorials and the first program won't do anything. I downloaded the code they used and it still did nothing. No errors however. Test prints out fine but I get no response from the luaL_dostring. I am using v5.1.4-46. Is it possible that i installed it incorrectly in some way that doesn't result in errors? I printed out the memory address of the lua state and that seems to be working. If someone could give me some sample code to run I would really appreciate it.
Here's the code:
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
int main()
{
lua_State *L = lua_open();
luaL_openlibs(L);
cout << "Test" << endl;
luaL_dostring(L, "print(\"Hello from Lua!\n\")");
lua_close(L);
return 0;
}

Your dostring syntax generates invalid Lua code... "\n" gets passed to lua parser before execution. you need a "\\n"... think so. So your "luaL_dostring" error code is actually a syntax fault. funny thing...
you should not use dostring for more than testing.

Related

C++ cout is not printing to command prompt

I'm getting started with C++ and am trying to work with vectors and printing stuff. I am using Windows 10, writing my code in Visual Studio Code, I just downloaded the MinGW-g++ compiler, and am trying to run my code in CMD.
This is my code, I've tried printing different things, and it's not even printing this "Hello". I've also tried using << endl; which also prints nothing. I also tried adding and remove a "return 0;" at the end but it changed nothing. The program compiles just fine. Any advice?
#include <iostream>
using namespace std;
int main(void)
{
std::cout << "Hello";
std::cout.flush();
}

Terminal can't print vector in c++

I am new to C++ and trying my hands on std::vector. But for some reason even a single std::cout doesn't work with the vector I have declared.
Here is the sample code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
std::vector <int> test_score;
test_score.push_back(10);
cout << test_score.at(0);
return 0;
}
The terminal output shows nothing. Here it is.
The same code works fine on IDEs like CodeBlocks.
Just for your info, your code works fine on my terminal without the image description part.
Maybe try to comment these lines out as they are not involve in the problem or try to compile a Hello World program first may help.

11db error in XCode C++

I'm following some C++ tutorials and have started experimenting with structures, however a test structure I built is not behaving as intended. I have tried running it a few times and all it outputs is (11db), and when I try rerunning it I get a window saying that the product is already running.
Here is the code...
#include <iostream>
using namespace std;
int main(){
struct Address {
int streetNum;
string streetName;
};
Address myHouse;
myHouse.streetNum = 911;
myHouse.streetName = "Inverness Street";
cout << myHouse.streetName << endl;
return 0;
}
I would expect the output to be "Inverness Street". Why am I getting an error instead?
EDIT ** Bonus points for anyone who can tell me how to remove a missing file warning? I removed a useless file that was created as a means to find my way around creating different types of files, but since I deleted it I have had a warning in the file that it once shared a folder with.

How To Use LUA_COMPAT_ALL?

I am new to Lua and especially new to Luabind. When I tried to compile (with Clang++), my first file using Luabind:
#define LUA_COMPAT_ALL
#include <luabind/luabind.hpp>
#include <luaconf.h>
#include <iostream>
int main() {
lua_State *myLuaState = luaL_newstate();
luabind::open(myLuaState);
luaL_dostring(
myLuaState,
"function add(first, second)\n"
" return first + second\n"
"end\n"
);
std::cout << "Result: "
<< luabind::call_function<int>(myLuaState, "add", 2, 3)
<< std::endl;
lua_close(myLuaState);
}
I got a whole heap of error messages.
So, I did a bit of looking around I found it had to do with my Lua version being 5.2 vs 5.1 and found that the solution was LUA_COMPAT_ALL (which I found out at Lua project compiling with errors (luabind)).
Unfortunately, I'm a bit of a scrub when it comes to Lua; so, I don't know where I put that.
I hope my question wasn't too stupid :)
Just defining it in your code should work, as told in the Lua source (CTRL+F for "LUA_COMPAT_ALL"): http://www.lua.org/source/5.2/luaconf.h.html
(It might've been in the manual, but removed from it later on)

C++ program gives a run-time error when strings are used

#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
string st = "Hello world";
return 0;
}
and
#include <string>
int main ()
{
std::string st = "Hello world";
return 0;
}
I tried compiling this code using minGW compiler on netbeans. It brings up the following error after the successful build.
RUN FAILED (exit value -1,073,741,511, total time: 93ms)
But it works clean when strings are not used. I would like to know what I am doing wrong here. Thanks in advance.
Use c++ strings and don't use using namespace std:
#include <string> //c++ string header
int main ()
{
std::string st = "Hello world";
return 0;
}
#include <string.h> is the old C-style string header and most likely isn't what you want to use here. See this question for more details: Difference between <string> and <string.h>?
Note: If you really wanted the old C-style strings then you really should be using #include <cstring> because this will put those functions into the std namespace and won't cause any namespace pollution that can lead to other undesirable outcomes.
Likely what happened was that you used the old style string header and didn't properly initialize those strings. The old C-style strings don't have a constructor and operator= defined like the std::string class.
Edit: After looking at the Netbeans forum this is a problem with Netbeans and not a c++ issue. Try changing the output to an external terminal in Netbeans. Or run the program directly from the command line. If these approaches don't fix the problem or are undesirable then make a post over on the Netbeans forum. Also have a look at this question: Program won't run in NetBeans, but runs on the command line!
Uss #include <string> instead of string.h