Page 1 of 2 Next >>
/*
* File: main.cpp
* Author: Angel Macias
*
* Created on September 18, 2010, 8:05 PM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
/*
* Using a for loop, print out all the odd numbers from 1-45.
*/
int main(int argc, char** argv) {
int value = 0;
for(int count=0;count<25;count++)
{
cout << ++value <<endl;
value++;
}
return 0;
}
Bobotastic wrote:
Is .cpp your main coding file?
The majority of your code (ie methods and function implementations) go in cpp files. The header files is where you function and class definitions go. Why? Because the C++ compiler needs to see the definitions before it can deal with the references to those definitions in the implementations. So you separate the definitions into .h files and #include them at the top of the .cpp files. The .cpp files are ones that get compiled, and the #include statements do a virtual copy/paste of the .h files in at the top of the .cpp files.Bobotastic wrote:
These are the basic questions I have. Apparently C++ programs have header files. Wtf are those for? They have source files as well; enlighten me please lol.
In VS2010, File -> New Project -> Visual C++ -> Win32 -> Win32 Console ApplicationBobotastic wrote:
So I installed Visual Studios. I open up to make a new project, I get about 9 different options under the Visual C++ tab. All I'm saying to myself is "Which one do I choose to make a basic console program?..."
The console window closes because the program has ended as you haven't written any code to stop it ending immediately after looping. You can either do something like read a key from the console, or simply run your program from the command line to ensure that the output is still visible once the program has ended. There may be a Visual Studio option somewhere to help you hold the window open as well.Bobotastic wrote:
However, now when I run it, console windows comes up, then it just completely exits, not even letting me see the results. So I can't exactly see if I translated the code correctly. Help please?
No. See my previous post.Bobotastic wrote:
Oh I see. So a header file is like what a interface would be in Java? Assuming you know Java >.>.
Not just public methods. It's the whole class definition. This includes fields and methods of any accessibility level (public, private, protected).LuckySword wrote:
i.e. any public methods you want exposed on the class would have their signatures on the .h file but the signature aswell as the actual method code in the .cpp file.
Worked great. Thank you. If anyone ever reads this for help, make sure you run it without debugging :).bayou wrote:
Actually, scratch that. You should be able to do it through the UI.
1. Right-click your project (2nd level down) in the Solution Explorer window, choose Properties
2. In the tree on the left-hand side, navigate to Configuration Properties > Linker > System
3. In the SubSystem option on the right, choose "Console"
4. Click OK
Hopefully that will sort you out.
So would it be correct programming etiquette to write all your functions in the .h file?Ratfink wrote:
The majority of your code (ie methods and function implementations) go in cpp files. The header files is where you function and class definitions go. Why? Because the C++ compiler needs to see the definitions before it can deal with the references to those definitions in the implementations. So you separate the definitions into .h files and #include them at the top of the .cpp files. The .cpp files are ones that get compiled, and the #include statements do a virtual copy/paste of the .h files in at the top of the .cpp files.
In general, that's correct.Bobotastic wrote:
So would it be correct programming etiquette to write all your functions in the .h file?
Just the definitions, not the actual body...
I LOLed.maareek wrote:
I just figured I'd point out that I'm getting considerable entertainment out of reading up on this ostensibly intelligent group of people having trouble with anything over a C+. Seriously, people these days...
#include <iostream>is totally fine.
using namespace std;
int add(int a, int b)
{
return a + b;
}
int main(int argc, void **argv)
{
int sum = add(1, 3);
cout << sum << endl;
return 0;
}
#include <iostream>The purpose of using header files has more to do with sharing libraries with other programmers. You would distribute your compiled code and also your header files. The programmer using your library needs the header files to know function names, parameter lists, etc. Unlike Java, this information cannot be divined from a compiled library (or package, or whatever). The programmer would include your header files at the top of her code, "link" against your compiled library, and joy would ensue shortly thereafter. That way, you don't distribute your super-secret source code - just the bits other people need to use it.
using namespace std;
// forward-declare the add function in the same file
int add(int, int);
int main(int argc, void **argv)
{
int sum = add(1, 3);
cout << sum << endl;
return 0;
}
int add(int a, int b)
{
return a + b;
}
Haha, I was thinking that myself, but more because of the fact that I use Visual Studio pretty much everyday at work to compile stuff, but never take the time to look at all the code behind it.. since I don't understand a thing lol.maareek wrote:
I just figured I'd point out that I'm getting considerable entertainment out of reading up on this ostensibly intelligent group of people having trouble with anything over a C+. Seriously, people these days...
;p
Page 1 of 2 Next >>