Jump to content
  • Advertisement
Sign in to follow this  
Haku

Help the noob

This topic is 4995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, i recently made this. I have been doing c++ for a month now. I want to know if i can put all of the functions i have made before 'int main' in a header file to make it look neeter because i have never made a header file so i dont know what their purose is . My source:
[source=Cpp}#include <iostream>

using namespace std;

void playgame()  // No int, you don't need a return value here
{
  cout<<"Play.\n";
}
void loadgame()  // No int, you don't need a return value here
{
  cout<<"Saved.\n";
}  // Forgot a close brace
void playmultiplayer() // No int, you don't need a return value here
{
  cout<<"multiplayer.\n";
}  // forgot a close brace
void settings()   // No int, you don't need a return value here
{
  cout<<"settings.\n";
}  // forgot a close brace
void quit(){}  // No int, you don't need a return value here, and you forgot a function body(in this case, I don't think you need one that contains anything)
	
int main()
{
  int input;
  
  cout<<"1. Play game\n";
  cout<<"2. Load game\n";
  cout<<"3. Play multiplayer\n";
  cout<<"4. Settings\n";
  cout<<"5. Quit\n";
  cout<<"Selection: ";
  cin>> input;
  switch ( input ) {
  case 1:           
    cout<<"kool";
   cin.get();
     break;
  case 2:            
    loadgame();
    break;
  case 3:            
    playmultiplayer();
    break;
  case 4:            
    settings();  // C++ is case sensitive: settings is not the same as Settings
    break;
  case 5:
    quit();  // Forgot ()
    break;
  default:            // Note the colon, not a semicolon
    cout<<"Error, bad input, quitting\n";
    break;
  }
  cin.get();
}



Share this post


Link to post
Share on other sites
Advertisement
A header's to give predeclarations of the things you're using. For example, the header would have:

int Function( int params );

And the corresponding CPP file would have:

int Function( int params )
{
return params + 2;
}

Then, you could include the header file into another CPP and call the function.

Share this post


Link to post
Share on other sites
Put the declarations of the functions in a header file. Make a .cpp file with the functions' definitions and a higher build priority than main that includes the header file. Then include the original header into main.

EDIT: Oops, I got beat to it.

Share this post


Link to post
Share on other sites
so could i make a header file and put all of the fuctions i created in my cpp sorce in so that it is neater? So that i only have to call the functions in my cpp source?

EDIT: So can i declare functions in header files?

Share this post


Link to post
Share on other sites
you simply can write the prototypes of your functions on the beginning of your cpp file and the functions themselves at the end of the file or wirte the prototypes in an header file like functions.h and then write on the top of your cpp
#include "functions.h"

your prototypes may look like

void playgame();
....

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!