Announcement

Collapse
No announcement yet.

c++ questions?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    c++ questions?

    Currently learning c++ and thought I could post here any noob-style questions I had regarding c++ programming.

    First one is about header files:

    When using a header guard like so:

    #ifndef INSERT_HEADER_NAME_HERE
    #define INSERT_HEADER_NAME_HERE

    // content.......

    #endif

    how come the 'INSERT_NAME_HERE' can be all upper case letters and replace spaces with '_'?

    Considering c++ syntax likes coders to be so precise and accurate with everything they do, it leaves me wondering why it can be your own preference when naming your header guards?

    #2
    All capitals is used for naming constants where the value never changes.

    First letter capitalised is used for naming objects.

    First letter not capitalised but the first letter of every subsequent word capitalised is for variables.

    spaces can't be used for variable names.

    All those points other than the last are just good practice rather than required syntax. So long as you use allowed characters, you can name variables how you like.

    Comment


      #3
      So you are essentially creating a new variable as a replacement for the header file, and checking if that has been defined previously in place of the header file, rather than checking against the defining of the header file exactly?

      Comment


        #4
        It's to stop header files being #included more than once basically. If the macro is defined then you know you've already processed that header file and you can avoid processing it again. Some languages do this check implicitly but c-style languages need that preprocessor code.

        Comment


          #5
          What FSW said. The reason that the use of upper case is commonplace is because it's a holdover from the old C-style conventions (and why you typically won't see such naming style in most people's C++ code outside of macro definitions) and it's using the C preprocessor:

          Comment


            #6
            Anyone know how to use arguments in c++ strings properly?

            #include <iostream>
            #include <string>

            using namespace std;

            cout<< "Enter the month you wish to view: ";
            char sMonth[15];
            cin.getline (sMonth , 15, '\n');

            if(sMonth == "January" || "january" || "Jan" || "jan" || "J" || "j"){
            cout << "January" << endl;
            }else(...
            When run, the program wont respond to the argument January, or any of the other variations i've put in.

            I've only just started using strings, so i'm not sure if I'm even using them properly?

            Comment


              #7
              It's your conditional expression that's wrong, not the string handling. Try:

              if(sMonth == "January" || sMonth == "january" || sMonth == "Jan" || sMonth == "jan" || sMonth == "J" || sMonth == "j"){
              ...

              Your expression was 'oring' all those strings together and comparing the result with sMonth which will fail.

              Comment


                #8
                No luck for me, I think i've gone wrong on an even more basic level somewhere, as even 1 condition...

                if(sMonth == "j"){
                cout << "January"
                }
                wont work properly either?

                Is there a different way to declare or handle strings in c++, as I kind of just copied a tutorial example that I found on the internet...

                Comment


                  #9
                  Lol just noticed you are using a char array not an STL string so that won't work either. Your options are to declare sMonth as a string type or replace each occurance of sMonth == "whatever" with strcmp(sMonth, "whatever") == 0

                  Comment


                    #10
                    How do you declare a proper STL string type then?

                    Comment


                      #11
                      string sMonth;

                      However, cin.getline expects a char* so that line of code will fail if you do that.

                      This should work:

                      int main(void)
                      {
                      cout<< "Enter the month you wish to view: ";
                      string sMonth;
                      cin >> sMonth;


                      if((sMonth == "January") || (sMonth == "january") || (sMonth == "Jan") || (sMonth == "jan") || (sMonth == "J") || (sMonth == "j"))
                      {
                      cout << "January" << endl;
                      }
                      }

                      Comment


                        #12
                        Beautiful.

                        That's so much easier and fits with the basic input for intergers and chars etc. (makes more sense to me )

                        Was the original way I was trying to do it using a old c style string, not a c++ string?

                        Comment


                          #13
                          Yep. Generally speaking you can use the new C++ STL way of doing things. Much nicer. It's worth looking up how that old way of doing things works though in case you have to interface/integrate with some old code. Calling c_str() on an STL string will return you an old style c string too e.g.

                          string foo = "hello";
                          const char* bar = foo.c_str();
                          Last edited by Brad; 21-10-2011, 14:41. Reason: forgot constness :-)

                          Comment


                            #14
                            Now I start to see why coders say that programs are much easier to code now than they were in the old days.

                            Great stuff. Thank you very much for the help FSW.

                            Comment


                              #15
                              Depends what you're coding! IMO it's quite a bit harder than it used to be as we can do so much more these days. Different devices communicating with each other, multi-core, larger APIs to learn etc. but yes, writing Hello World is simplified these days

                              Are you doing this for fun Malc?

                              Comment

                              Working...
                              X