This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 6 years ago.
This my main function:
void main(int argc, char **argv)
{
if (argc >= 4)
{
ProcessScheduler *processScheduler;
std::cout <<
"Running algorithm: " << argv[2] <<
"\nWith a CSP of: " << argv[3] <<
"\nFilename: " << argv[1] <<
std::endl << std::endl;
if (argc == 4)
{
processScheduler = new ProcessScheduler(
argv[2],
atoi(argv[3])
);
}
else
{
processScheduler = new ProcessScheduler(
argv[2],
atoi(argv[3]),
atoi(argv[4]),
atoi(argv[5])
);
}
processScheduler -> LoadFile(argv[1]);
processScheduler -> RunProcesses();
GanntChart ganntChart(*processScheduler);
ganntChart.DisplayChart();
ganntChart.DisplayTable();
ganntChart.DisplaySummary();
system("pause");
delete processScheduler;
}
else
{
PrintUsage();
}
}
The error I get when I compile is this:
Application.cpp:41:32: error: '::main' must return 'int'
It's a void function how can I return int and how do I fix it?
Try doing this:
int main(int argc, char **argv)
{
// Code goes here
return 0;
}
The return 0; returns a 0 to the operating system which means that the program executed successfully.
C++ requires main() to be of type int.
Function is declared as int main(..);, so change your void return value to int, and return 0 at the end of the main function.
Related
This question already has answers here:
How to compare strings
(4 answers)
Closed 1 year ago.
I am trying to make a CLI app in C++. This is my first time coding in C++.
I have this c++ code:
#include <iostream>
// using namespace std;
static void help(std::string argv)
{
std::cerr << "Usage:" << argv << " [options]\n"
<< "Options:\n"
<< "-h (--help): Displays this help message.\n"
<< "-o (--output=[output file]): Specifies the output file.\n"
<< "-p (--ports=[ports]) Sets the ports to scan.\n"
<< std::endl;
}
int main(int argc, char** argv)
{
if (argc > 1)
{
std::cout << argv[1] << "\n";
if (argv[1] == "-h" || argv[1] == "--help")
{
help(argv[0]);
return 0;
}
}
else
{
std::cout << "No arguments were given" << "\n";
};
};
// g++ -o cli main.cpp
It works! When I compile it, it successfully outputs No arguments were given, but when I run cli -h, I can see argv[1] is -h, but nothing is outputted.
What did I do wrong?
In your string comparison, argv[1] is a C string: a null-terminated char array. You cannot compare these with == and get the result you expect. If, however, you assign it to a std::string you can compare it with "-h" and "--help" the way you want.
std::string arg1 = argv[1];
if (arg1 == "-h" || arg1 == "--help") {
help(argv[0]);
return 0;
}
Alternatively you could use std::strcmp to compare C strings without creating a new std::string. In order to do this, you'll need #include <cstring>.
if (std::strcmp(argv[1], "-h") == 0 || std::strcmp(argv[1], "--help") == 0) {
help(argv[0]);
return 0;
}
My problem is that is access the kinect with the following code:
#include "libfreenect.hpp"
#include <iostream>
freenect_context* ctx;
freenect_device* dev;
void freenect_threadfunc(freenect_device* dev, void* v_depth, uint32_t timestamp){
short* d = (short*) v_depth;
std::cout << d[0] << std::endl;
}
int main(int argc, char const *argv[])
{
if(freenect_init(&ctx, NULL) < 0){
std::cout << "freenect_init() failed!" << std::endl;
}
if (freenect_open_device(ctx, &dev, 0) < 0){
std::cout << "No device found!" << std::endl;
freenect_shutdown(ctx);
}
freenect_set_depth_callback(dev, freenect_threadfunc);
freenect_set_depth_mode(dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
freenect_start_depth(dev);
while (true) {
}
return 0;
}
But for some reason i don't know, the callback function ´freenect_threadfunc´ doesn't execute. When executing freenect-glview which is a example provided by Openkinect, everything works fine.
Thank you for your help.
I'm trying to use a lua state within C++ and i need to pass it a str from C++, how ever when i try to call my function i wrote in lua, i get the error
attempted to call nil value. It compiles straight into the lua environment, but when i type the expression in i get the error.
int main(int argc, char** argv){
lua_State *L;
L = luaL_newstate();
string buff;
const char* finalString;
luaL_openlibs(L);
luaL_dofile(L,argv[1]);
getline(cin, buff);
lua_getglobal(L, "InfixToPostfix");
lua_pushstring (L, buff.c_str());
lua_pcall(L, 1, 1, 0);
finalString = lua_tostring(L, -1);
printf("%s\n", finalString);
lua_close(L);
}
from lua file:
function InfixToPostfix(str)
print("before for loop")
for i in string.gmatch(str, "%S+") do
it wont reach the print out before displaying error
The following works for me just fine:
#include <iostream>
#include <string>
#include <lua.hpp>
int main(int argc, char** argv)
{
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " script.lua\n";
return 1;
}
if ( luaL_dofile(L,argv[1]) != 0 )
{
std::cerr << lua_tostring(L, -1) << '\n';
return 1;
}
std::string buff;
std::getline(std::cin, buff);
lua_getglobal(L, "InfixToPostfix");
lua_pushstring (L, buff.c_str());
if ( lua_pcall(L, 1, 1, 0) != 0)
{
std::cerr << lua_tostring(L, -1) << '\n';
return 1;
}
if ( !lua_isstring(L, -1) )
{
std::cerr << "Error: Return value cannot be converted to string!\n";
return 1;
}
const char * finalString = lua_tostring(L, -1);
std::cout << finalString << '\n';
lua_close(L);
}
function InfixToPostfix(str)
print("before for loop")
for i in string.gmatch(str, "%S+") do
print(i)
end
return "Something"
end
For the C++ part you could also use the Selene library. This considerably reduces the amount of code needed, also no manual error checking is needed.
#include <iostream>
#include <string>
#include <selene.h>
int main(int argc, char** argv)
{
sel::State L{true};
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " script.lua\n";
return 1;
}
L.Load(argv[1]);
std::string buff;
std::getline(std::cin, buff);
std::string finalString = L["InfixToPostfix"](buff);
std::cout << finalString << '\n';
}
I'm trying to write a simple fingerprint scanning program in c++ using libfprint, however it intermittently segfaults when run. Valgrind says the error is in the call to fp_enroll_finger which is consistent with my debugging, however beyond that I have absolutely no idea what is causing this error. Some of the times the program is run its fine, but some times it seems to consistently segfault for a period of time whenever the program is run?
Heres the code:
#include <iostream>
extern "C"
{
#include <libfprint/fprint.h>
}
using namespace std;
fp_dev * fpdev;
fp_print_data ** fpdata;
bool createDevice();
int main(int argc, char **argv)
{
int r = fp_init();
if(r != 0)
{
return r;
}
while(createDevice())
{
cout << "Scan right index finger" << endl;
int enrollStatus = fp_enroll_finger(fpdev, fpdata);
if(enrollStatus != 1)
{
cout << "Bad scan" << endl;
fp_dev_close(fpdev);
}
else
{
cout << "Good scan" << endl;
fp_print_data_save(fpdata[0], RIGHT_INDEX);
break;
}
}
if(fpdev != NULL)
{
fp_dev_close(fpdev);
}
fp_exit();
return 0;
}
bool createDevice()
{
fp_dscv_dev ** listOfDiscoveredDevs;
fp_dscv_dev * discoveredDevice;
listOfDiscoveredDevs = fp_discover_devs();
discoveredDevice = listOfDiscoveredDevs[0];
if(discoveredDevice != NULL)
{
cout << "Device found" << endl;
fpdev = fp_dev_open(discoveredDevice);
}
else
{
cout << "No device found" << endl;
return false;
}
fp_dscv_devs_free(listOfDiscoveredDevs);
return true;
}
You need to define fpdev and fpdata as:
fp_dev * fpdev;
fp_print_data * fpdata;
And use them as:
fp_enroll_finger(&fpdev, &fpdata);
Also don't forget to free fpdata when you no longer need it with fp_print_data_free
fp_dev * fpdev;
fp_print_data ** fpdata;
Will create 2 uninitialised pointers pointing to random memory location and leading to segfault once fp_enroll_finger will attempt to acces that location.
Checking fp_enroll_finger return value can be useful as well.
I've been scratching my head for quite some time now, this code worked fine when I first used cmd to go inside the project\debug folder then run the program there. Then I added the if(in) and else part then it started giving me "debug assertion failed" errors mbstowcs.c
Expression s != NULL
It just doesn't make any sense to me..
I used this command in cmd: prog.exe test.txt nuther.txt
Both files exists inside the debug folder and the main project folder..
Any ideas?
int main(int argc, char **argv)
{
parse_opts(argc, argv); //parse the arguments
return 0;
}
void parse_opts(int argc, char **argv)
{
string compl_out;
if( argc > 1 )
{
for( int i = 1; i < argc; i++ )
{
if( argv[i][0] = '>' )
{
ofstream out_file(argv[i+1]);
out_file << compl_out;
out_file.close();
break;
}
ifstream in(argv[i]);
string buff;
if(in)
{
while(getline( in, buff ))
cout << buff << endl;
compl_out.append(buff);
}
else
{
cout << "Can't open file: " << argv[i]
<< ", file doesn't exist or is locked in use. " << endl;
}
}
}
else
{
usage();
}
}
First impressions:
if( argv[i][0] = '>' )
should be:
if( argv[i][0] == '>' )
You are assigning instead of comparing.
I think you also might have intended the compl_out.append to be inside the while loop? As it is it won't append anying to that buffer:
while(getline( in, buff ))
{
cout << "buf" << buff << endl;
compl_out.append(buff);
}