banner



How To Create A Simple Program In C++

Download Article

Download Article

Ever wanted to program in C++? The best way to learn is by looking at examples. Take a look at the basic C++ programming outline to learn about the structure of a C++ program, then create a simple program on your own.

Steps Download Article

  1. 1

  2. 2

    Try some example programs. Copy and paste the following into a text/code editor:

      A simple program is given by Bjarne Stroustrup (developer of C++) to check your compiler:
                                                      #include                        <iostream>[[Image:Create a Simple Program in C++ Step 1 Version 3.jpg|center]]                                                #include                        <string>                                                                        using                        namespace                        std                        ;                        int                        main                        ()                        {                        string                        s                        ;                        cout                        <<                        "Your Name                                                \n                        "                        ;                        cin                        >>                        s                        ;                        cout                        <<                        "Hello, "                        <<                        s                        <<                        '\n'                        ;                        return                        0                        ;                        }                      
    • A program for finding the sum of two numbers:
                                                          [[                          Image                          :                          Create                          a                          Simple                          Program                          in                          C                          ++                          Step                          2                          Version                          3.                          jpg                          |                          center                          ]]                          #include                          <iostream>                                                    using                          namespace                          std                          ;                          int                          main                          ()                          {                          int                          no1                          ,                          no2                          ,                          sum                          ;                          cout                          <<                          "                          \n                          Enter the first number = "                          ;                          cin                          >>                          no1                          ;                          cout                          <<                          "                          \n                          Enter the second number = "                          ;                          cin                          >>                          no2                          ;                          sum                          =                          no1                          +                          no2                          ;                          cout                          <<                          "                          \n                          The sum of "                          <<                          no1                          <<                          " and "                          <<                          no2                          <<                          " = "                          <<                          sum                          <<                          '\n'                          ;                          return                          0                          ;                          }                        
    • A program for finding the product in multiplication problems:
                                                          [[                          Image                          :                          Create                          a                          Simple                          Program                          in                          C                          ++                          Step                          3                          Version                          3.                          jpg                          |                          center                          ]]                          #include                          <iostream>                                                    int                          main                          ()                          {                          int                          v1                          ,                          v2                          ,                          range                          ;                          std                          ::                          cout                          <<                          "Please input two numbers:"                          <<                          std                          ::                          endl                          ;                          std                          ::                          cin                          >>                          v1                          >>                          v2                          ;                          if                          (                          v1                          <=                          v2                          )                          {                          range                          =                          v2                          -                          v1                          ;                          }                          else                          {                          range                          =                          v1                          -                          v2                          ;                          }                          std                          ::                          cout                          <<                          "range = "                          <<                          range                          <<                          std                          ::                          endl                          ;                          return                          0                          ;                          }                        
    • A program for finding the value of exponents:
                                                          [[                          Image                          :                          Create                          a                          Simple                          Program                          in                          C                          ++                          Step                          4                          Version                          3.                          jpg                          |                          center                          ]]                          #include                          <iostream>                                                    using                          namespace                          std                          ;                          int                          main                          ()                          {                          int                          value                          ,                          pow                          ,                          result                          =                          1                          ;                          cout                          <<                          "Please enter operand:"                          <<                          endl                          ;                          cin                          >>                          value                          ;                          #                          cout                          <<                          "Please enter exponent:"                          <<                          endl                          ;                          cin                          >>                          pow                          ;                          for                          (                          int                          cnt                          =                          0                          ;                          cnt                          !=                          pow                          ;                          cnt                          ++                          )                          result                          *=                          value                          ;                          cout                          <<                          value                          <<                          " to the power of "                          <<                          pow                          <<                          " is: "                          <<                          result                          <<                          endl                          ;                          return                          0                          ;                          }                        

    Advertisement

  3. 3

    Save this as a .cpp file with a name that accurately reflects your program. Don't confuse there are many other extensions for C++ files, choose any of them (like *.cc, *.cxx, *.c++, *.cp) .

    • Hint: It should say Save as Type: {select "All Files"}
  4. 4

    Compile it. For users of linux and gcc compiler, use Command : g++ sum.cpp. Users of Window can use any C++ compiler, such as MS Visual C++,Dev-C++ or any other preferred program.

  5. 5

    Run the program. For users of Linux and gcc compiler Command : ./a.out (a.out is an executable file produce by compiler after compilation of program.)

    Advertisement

Add New Question

  • Question

    Are there online solving programs?

    Community Answer

    Yes, there are many types of online solving programming tests available on the internet.

  • Question

    What are the uses of C++?

    Community Answer

    Everything! It can be used in everything from programming games to making webpages, software, and databases.

  • Question

    I'm interested in learning C++, but I don't have any money. What can I do?

    Community Answer

    Look for tutorials like this one online and teach yourself.

  • Question

    How would I know which data type I'm supposed to use?

    Community Answer

    Think about what the data needs to do. For example, if you need to do something like simple counting or a loop, use int. If you need to keep track of multiple characters, use a string.

  • Question

    How can I learn programming using C++?

    Sebir Moran

    Sebir Moran

    Community Answer

    Watch some tutorials on YouTube. Or buy a book on C++ programming like Sumita Arona's Programming with C++.

  • Question

    What are the steps to start C++ programming?

    Community Answer

    If you know C you can can easily learn C++. There is no much difference in C & C++.

  • Question

    How can I create a program that stores school records through C++?

    Soos Marton

    Soos Marton

    Community Answer

    The simplest way is to use the standard input method (cin) to get the user's input, then store it in a file using fstreams.

  • Question

    How can I learn C programming in a simple way?

    Community Answer

    You can watch YouTube tutorials and follow along: that goes a long way. Alternatively, you could join a program such as Treehouse or Cademy. You could also pick up a book and read about C++.

  • Question

    I want to create a program that allows my customers to search all the products in my store. What do I need to know to do this?

    Are Lodgaard

    Are Lodgaard

    Community Answer

    You need to learn a programming language and some type of server language.

  • Question

    Is C++ a better language than C#? If so, why?

    Community Answer

    They're both object oriented, and similar in many ways, but I can't say which one is better. C# is a little easier to learn, and widely used for most business-oriented applications nowadays, but it doesn't have the same "power" as C++.

See more answers

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

Advertisement

Video

  • cin.ignore() prevents the program from ending prematurely and closing the window immediately (before you have time to see it)! Press any key if you want to end the program. cin.get() functions in a similar manner.

  • Add // before all of your comments.

  • Learn programming in C++ with ISO standards

  • For more details about programming in C++ give a visit cplusplus.com

Thanks for submitting a tip for review!

Advertisement

  • Your program will crash if you try to input alphabetical values to one of the "int" vars. Since no propper error trapping is done your program can't convert the values. Better read string or catch your exceptions.

  • Make sure to stay as far away from Dev-C++ as possible, because it has multiple bugs, an outdated compiler, and has not been updated since 2005.

Advertisement

Things You'll Need

  • A text/code editor (e.g. vim, notepad, etc).
  • A compiler.
  • Alternatively, an IDE contains an editor and a compiler.
  • Turbo c
  • Codepad online
  • Notepad++

About This Article

Thanks to all authors for creating a page that has been read 471,419 times.

Is this article up to date?

How To Create A Simple Program In C++

Source: https://www.wikihow.com/Create-a-Simple-Program-in-C%2B%2B

Posted by: hiserotile1968.blogspot.com

0 Response to "How To Create A Simple Program In C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel