Question:
Building a program for C++?
2012-02-16 10:44:40 UTC
how do i even begin? what does #include mean? i hae to build a calculator program. im completely lost. i just ned someone to walk me htrough the beginning steps of writing a program. like where does usingnamespace std; go? where does int main() go? and how do i create variables? any and all help appreciated
Four answers:
2012-02-16 11:03:13 UTC
A source code for Calculator here:

http://algoritmosurgentes.com/en/algorithm.php?a=127
jplatt39
2012-02-16 19:29:28 UTC
I don't know what compiler or OS you are using, but go look at the binaries, go up one folder and look for one named "INCLUDE". Look in that and you should see a file called iostream. This is a header file which declares some basic concepts like cout cin << and >>. The brackets <> tell the compiler to look in the Include directory. Under that write "using namespace std;" End the line with a semicolon. Oh, you include all the header files you need to include then you put using namespace std;. Under it you declare functions and global variables. A word to the wise: if you want to declare global variables don't use C++. It exists because it is often a bad idea and C++ provides many alternatives. Under those you declare the main() function which always returns an integer or int so:



int main()

No semicolon note. Either on that line or the next have an open curly-bracket {



Under that declare your variables. You can declare them in the program but they are easier to find if you declare them first. Then put your code and finally return 0; which tells the operating system that all is well. And a close curly-bracket } followed by a carriage return to end the program.
deanyourfriendinky
2012-02-16 18:48:47 UTC
If you're using an Integrated Development Environment open it up, start a new C++ project, and in the source code editor, start typing your code.



If you're using a programming-friendly text editor and compiling on the 'command line' open the editor and begin typing your code.



You can start your code with a comment containing a clever name, like 'Calculator' or some such descriptive word.



For example, you might begin with something like this:



// EXAMPLE Message Output

// Here's a simple program that outputs a message onscreen.



#include



using namespace std;



int main()

{

cout << "\n\tThis program outputs this simple"

<< "\n\tstring of characters!\n"

<< endl;

return 0;

}



The first two lines are comments, they exist solely to help you (or another programmer) know what the program is for.



The first real statement is an 'include directive'. It tells the compiler's preprocessor to fetch the contents of the C++ Standard Library header named 'iostream' and include those contents in the source code.



The next real statement tells the compiler that you want to use all of the entities within 'std' namespace without fully qualifying them (because the fully qualified name of 'cout' is 'std::cout' and the fully qualified name of 'endl' is 'std::endl'. However, this is not SAFE programming, it introduces the possibility of name conflicts.



Next is the start of the function called main(). It's contents are enclosed between two curly brackets { and }. You begin the first line of the function main() by declaring what kind of value it will return (in this case, it will return an 'integer') to the operating system when the program is finished.



So, start your source file with a comment describing its purpose.



Then, put your #include
statements.



Then, put your using statements.



Then, start your main() function.
Milad
2012-02-16 18:47:50 UTC
http://www.cplusplus.com/

this site has some good tutorials


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...