pocketshotgun

Technology Questions for programmers

Recommended Posts

Hello! so recently I have been wanting to learn C++, I have some prior programming knowledge and I know the basis how things work in a program.

 

I think that if I understand how it works I will be able to learn better. So I have a few questions.

If anyone could do their best to answer that would be great!

 

1. what does this mean?

 

 int main(int nNumberofArgs, char*pszArgs[])

 

2. What do these mean?

 

    cin.ignore(10, '\n');
    cin.get();
    return 0;
 
Thanks for any help! I'll probably have further question in the future.

Share this post


Link to post
Share on other sites

int main(int nNumberofArgs, char*pszArgs[])

 

I'm not very vamiliar with C++ but I know a bit of C# which is quite similar, however I'd suggest you to google your questions :P

int main()

Is the main function which is called each time you run your program. int main() means that it returns an int and therefore needs a number. Some functions doesn't need to return anything and look like void myfunction(), however the main one must always return an integer.

int nNumberofArgs, char*pszArgs[]

These are the arguments for the function, they're the ones that decide what needs to be passed to the function in order for it to run. These arguments requires that an int and a char array is passed to the function. I'm not really sure what the star behind char is, it's probably a C++ thing which you'll have to look up for yourself.

 

cin.ignore(10, '\n'); cin.get(); return 0;

 

cin is a bit of code that allows you to grab user input and put it in an object. cin.ignore is basically what it says, it ignores certain characters. In your case it will ignore up to 10 characters of user input as well as the new line character (enter) ( \n , the two '' just show that \n is a char.) Again, I'm not an expert in C++ but I'm guessing that cin.get() just gets and stores the user input. You could probably type an argument inside the parenthesis.

return;

returns a value to a method or function (unless it's of the type void) Since the main method requires an int you'll need to return a number. (In this case 0) I'm not really sure when you'd need something else than 0 but I'm sure someone else can fill in.

Share this post


Link to post
Share on other sites

int main(int nNumberofArgs, char*pszArgs[])

 

You should probably call these parameters argc and argv rather than nNumberofArgs and pszArgs.

 

argc and argv are the names used for these variables in the overwhelming majority of code. Using any other name is likely to confuse people reading your code.

 

These arguments requires that an int and a char array is passed to the function

The main function always takes exactly these two arguments. When your program is run from the command line:

 

myprog.exe arg1 arg2 arg3 arg4

 

Then argc will be 4, and argv will be an array containing the strings "arg1", "arg2", "arg3", and "arg4". (Though see below about how C/C++ doesn't really have strings.)

 

I'm not really sure what the star behind char is, it's probably a C++ thing which you'll have to look up for yourself.

 

An asterisk between the variable type and the variable identifier indicates that the variable holds a pointer.

 

In particular, C and C++ don't have native string types, so instead programmers use a pointer to an array of chars.

  

I'm not really sure when you'd need something else than 0 but I'm sure someone else can fill in.

The main function should return 0 on success, and a positive integer on failure.

 

If your program can fail in multiple ways, you can use different numbers to indicate different failure reasons. Like maybe return 1 if your config file was unreadable, and return 2 if there was no input data. Whatever process launched your program can check the return value (commonly called the "exit status") to figure out what (if anything) went wrong.

 

For maximum cross-platform support, you should limit failure exist statuses to the numbers 1 to 127. (Because some platforms only support 7 bits for this value.)

Edited by VitalTwilightSparkle

Share this post


Link to post
Share on other sites

The main function should return 0 on success, and a positive integer on failure.

 

If your program can fail in multiple ways, you can use different numbers to indicate different failure reasons. Like maybe return 1 if your config file was unreadable, and return 2 if there was no input data. Whatever process launched your program can check the return value (commonly called the "exit status") to figure out what (if anything) went wrong.

 

For maximum cross-platform support, you should limit failure exist statuses to the numbers 1 to 127. (Because some platforms only support 7 bits for this value.)

 

Alright, cool. Thanks for telling us :) But yeah then I guess OP won't have to bother since cross-platform support shouldn't really be a concern for beginners.

 

Ugh all these main return values.. *goes back to C# cave where everything is void as default*

Share this post


Link to post
Share on other sites

Thanks the replies!

 

as for the: 

 

int main(int nNumberofArgs, char*pszArgs[])

part, that is just what was used in the book I am reading on C++

 

Another question, if I use the 

 

int main(int nNumberofArgs, char*pszArgs[])

 

will it limit me in anyway? or cause significant problems in the future? or is it not a big deal if I just keep using that part?

 

Thanks again!

Edited by pocketshotgun

Share this post


Link to post
Share on other sites

will it limit me in anyway? or cause significant problems in the future?

 

Variable names are arbitrary. There is no technical difference between naming a variable nNumberofArgs or argc or alistairSmith. All I'm saying is that if you call this particular variable argc, it will be more recognizable and understandable by other programmers. It doesn't make a jot of difference to the compiler.

Edited by VitalTwilightSparkle

Share this post


Link to post
Share on other sites

@pocketshotgun

 

I moved this to the Media forum and added the technology prefix. Vital did a decent job nailing your questions. 

 

Note that I gave my daughter the nickname foo. :P

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.