Jump to content
Banner by ~ Ice Princess Silky

technology Questions for programmers


pocketshotgun

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.
  • Brohoof 2
Link to comment
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.

Link to comment
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
  • Brohoof 1
Link to comment
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*

Link to comment
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
Link to comment
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
  • Brohoof 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Join the herd!

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...