   Program returning -1 in main
  Hey,I've recently been looking through the www.codermind.com ray tracing tutorial, and after reading through it all, decided to give the code a try (from here http://www.codermind.com/articles/Raytracer-in-C++-Part-I-First-rays.html). However, in the main loop I keep getting the program ending because of a -1 return which I have found to be where I have commented below:int __cdecl main(int argc, char* argv[]){    if (argc < 3)    {   cout << "Usage : Raytrace.exe Scene.txt Output.tga" << endl;   return -1;  // ONCE IT HITS THIS IT RETURNS    }    scene myScene;    if (!init(argv[1], myScene))    {   cout << "Failure when reading the Scene file." << endl;   return -1;    }    if (!draw(argv[2], myScene))    {   cout << "Failure when creating the image file." << endl;   return -1;    }    return 0;}I'm assuming that argc needs to be 3 because it uses Scene.txt to read from and Output.tga to write to and Raytrace is the actual program, but I'm really quite lost.Thanks   
  only 2 not 3. dont count the program as well   ;  Ohk thanks. From what I understand from the code, it calls init and passes in Scene.txt and for the draw function it passes in Output.tga, I don't know how the program would go about passing these in.EDIT: argc only equals one, which would be the actual program I assume, how do I pass other files in?   ;  Yes, the first argument contains the name of the program (it's full path actually). You can pass other arguments by starting the program from the command line (or a batch file) and entering the arguments after the program name, divided by one or more spaces. And, at least under Windows, dragging one or multiple files onto the program icon starts it with the filenames as additional arguments. As for starting your program from within an IDE, there's usually a way to pass arguments to it in the project settings.   ;  How are you running your program? From an IDE, you can usually configure the arguments passed on the command line.   ;  On a side note, I would recommend putting different values for your error returns in that function. You have three places returning -1, so when a -1 comes back you don't necessarily know where it was returned. You should make it -1, -2, and -3 or something to that effect.   ;  Hey, thanks for the replies. I dropped and dragged the files onto the exe and it worked perfectly, thanks Captain P. I'm running Visual Studio 2008 Express Edition, how do I go about setting this up from the idea so I can simply compile and run and the files will be pulled?   ;Quote:Original post by Captain PYes, the first argument contains the name of the program (it's full path actually).  Not necessarily: Quote:ISO/IEC 14882:2003(E), 3.6.1 Main function, item 2 (p.43f)[...] argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". [...](NTMBS == null-terminated multibyte strings)So it might also be just the name of the program, or of some link used to invoke the program, or just the empty string.For example, on Linux and Unix some programs come with multiple links onto them, and then have different behaviour dependent on the link invoked to start the binary (afair, poweroff and reboot on my home-box link to shutdown).Quote:Original post by ShinkaFudanOn a side note, I would recommend putting different values for your error returns in that function. You have three places returning -1, so when a -1 comes back you don't necessarily know where it was returned. You should make it -1, -2, and -3 or something to that effect. No, I would not recommend that. The number can be very limited and hard to define in a portable manner. You might come into the way of system specific error codes (e.g. on GNU, 127 is "command not found"). The size of the return value is arbitray (often it's only a 1 byte value that is catched by the OS or shell), some systems might define negative values to be user-error-codes, some might say that values in [1..63] are for user-error-codes, et cetera.Once in the elder days, I had just coded error codes bigger than 0xFF, just to realize upon testing that error codes where always truncated to a byte on the system I used then (never mind the int in "int main").The standard (i.e. portable code without #ifdef-kludgery) sees only 0 or EXIT_SUCCESS, or EXIT_FAILURE (from <cstdlib> or in C <stdlib.h>), all else is specific to the environment:Quote:ISO/IEC 14882:2003(E), 18.3 Start and termination, item 8 (p.342f)Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined. I would just recommend to return EXIT_FAILURE to the system upon failure, but before that, output a simple diagnostic, e.g.:if (dammit) {    std::cerr << __FILE__ << ':' << __LINE__ << ": dammit that failed!" << std::endl;    return EXIT_FAILURE;}...if (crap) {    std::cerr << __FILE__ << ':' << __LINE__ << ": crap happens" << std::endl;    return EXIT_FAILURE;} [Edited by - phresnel on September 2, 2009 3:37:55 AM]