Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Few small C++ questions

Options
  • 02-01-2006 12:00am
    #1
    Closed Accounts Posts: 418 ✭✭


    //
    //  Program to convert temperature from Celsius degree
    //  units into Fahrenheit degree units:
    //  Fahrenheit = Celsius  * (212 - 32)/100 + 32
    //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
      // enter the temperature in Celsius
      int celsius;
      cout << "Enter the temperature in Celsius:";
      cin >> celsius;
    
      // calculate conversion factor for Celsius
      // to Fahrenheit
      int factor;
      factor = 212 - 32;
    
      // use conversion factor to convert Celsius
      // into Fahrenheit values
      int fahrenheit;
      fahrenheit = factor * celsius/100 + 32;
    
      // output the results (followed by a NewLine)
      cout << "Fahrenheit value is:";
      cout << fahrenheit << endl;
    
      // wait until user is ready before terminating program
      // to allow the user to see the program results
      system("PAUSE");
      return 0; 
    }
    

    Note: This is an example from a book. Posting because I don't fully understand it.

    Ok, I understand the basics of it because the book explains. Some parts the book doesn't explain.
    Ok, mostly I know all of what is happening.
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    

    I know this is the "template" part of the code, but what does it actually do? :confused:
    // output the results (followed by a NewLine)
      cout << "Fahrenheit value is:";
      cout << fahrenheit << endl;
    

    What is the << endl; at the end for?


Comments

  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    X-SL wrote:
    What is the << endl; at the end for?
    It's a newline. Like "\n". IIRC Unix uses "\n" and Windows uses "\r\n" (or vice versa) but endl allows you to get the same result regardless of of the OS.

    For the rest of the code, the comments are quite helpful. What parts do you not understand?


  • Closed Accounts Posts: 418 ✭✭X-SL


    the template part.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    X-SL wrote:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    

    I know this is the "template" part of the code, but what does it actually do? :confused:

    OK. The 3 includes bits import the standard C++ libraries (comes with C++ to perform a fixed task so that you don't have to include it).

    cstdio = C Standard Input and Output
    cstdlib = C standard library
    iostream = Buffers for input and output of text (so that your program keeps running and lets the operating system deal with output and input).

    int main(... is the main method of the program that will automatically be executed when the program is run, otherwise how would the compiler know where to start?


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    daymobrew wrote:
    It's a newline. Like "\n". IIRC Unix uses "\n" and Windows uses "\r\n" (or vice versa) but endl allows you to get the same result regardless of of the OS.
    As well as this, the endl token also flushes the stream and writes out anything stored in the buffer. If you just use "\n"s you might see a delay in yout output being written to the screen.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    'using namespace std;' tells it to, er, use namespace std where no other namespace is specified. Code can be partitioned into namespaces, and std is the namespace which contains all of the ISO standard objects, like iostream. It is preferable to omit it, and instead use 'std::cout << "bla" << std::endl', as it really does defeat the purpose of namespaces in the first place.

    And the includes don't include libraries, they include headers for those libraries. This will become an important distinction if you ever (and for non-trivial programs to will) have to write a program which uses non-ISO C++ libraries.


  • Advertisement
Advertisement