#include <iostream>
#include <string>
#include "inn.h"
#include "globals.h"
#include "Store.h"
usingnamespace std;
int main()
{
int PlayerChoice, e, E;
char UserName[12];
cout << "Welcome to Cinz\n";
system("pause");
cout << "What would you like to do?\n";
cout << "1) Play\n";
cout << "E) Exit\n";
cin >> PlayerChoice;
if(PlayerChoice == 1)
{
cout << "Many years ago when I was child my dad left he told me keep your mother safe I will be back tomorrow. I sat there at the window for days just waiting for him to return. Everytime I fell asleep all I could see was his face fading away from my eyes. It had been years before I let him go. Now everytime I here his name Rale. My body builds up constant rage. Now that he is gone I have set out to avenge her.\n\n";
system("pause");
cout << "What is your name: ";
cin >> UserName;
cout << "Hello " << UserName << " What would you like to do?\n\n";
cout << "1) Go to inn\n";
cout << "2) Go to Store\n";
cout << "3) Go exploring\n";
cout << "4) Character page\n";
cout << "5) Inventory page\n";
cout << "E) Exit Program\n";
cin >> PlayerChoice;
}
if(PlayerChoice == 1)
{
Inn go;
}
elseif(PlayerChoice == 2){
Store go;
}
elseif(PlayerChoice == E || e)
{
return 0;
}
return 0;
}
#include "inn.h"
#include <iostream>
usingnamespace std;
int PlayerChoice, e, E;
char UserName[12];
Inn::Inn()
{
do{
cout << "Welcome to the Inn How may I be of service today?\n";
system("pause");
cout << "1) Rest\n";
cout << "E) Exit the inn\n";
cin >> PlayerChoice;
cout << UserName << "you are now well rested\n\n";
}
while(PlayerChoice == 1);
if(PlayerChoice == E || e){
}
}
inn.h
1 2 3 4 5 6 7 8 9 10 11
#ifndef INN_H
#define INN_H
class Inn
{
public:
Inn();
};
#endif
store.h
1 2 3 4 5 6 7 8 9 10 11
#ifndef STORE_H
#define STORE_H
class Store
{
public:
Store();
};
#endif
// function example
#include <iostream>
usingnamespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
int x = 4;
int y = 6;
z = addition (x,y);
cout << "The result is " << z;
return 0;
}
Like that. It passes two int values into the function.
How would i go about this using as extern. where i could just include the .h file to each .cpp file so i don't have to re-declare I used extern before but, it didn't work. Was I suppose to set extern in the .cpp and not in the .h but, include the .h
Seriously? If you are going to use preprocessor directives to check if #pragma once is supported, it's much better to just use normal include guards that is guaranteed to work everywhere.
@Peter, I couldn't agree more...however it is not good practice to use non-standard elements without protecting translation units from it. That directive prevents compatible compilers from opening and preprocessing files more than once. Typically, with include guards, files are opened and preprocessed, even if they've already been included. It's a cheap (usually un-noticed) trick to reduce compilation times, thus, even if include guards are used, it can still help to reduce compilation times on compatible compilers.
I personally do not use #pragma once .
* It also makes having a include guard naming collision impossible.
Final Edit, I promise...
** The boost library uses #pragma once in addition to standard include guards.
@peter you say I must define variables in a source file. I have a class called globals.cpp and .h So I would declare them in the .h and, inclue that with all my classes that those.
Ok did what you guys said. now let me be blunt we are going in a big circle lets thing before we talk. but, thank you also peter because this will save me time later and i...Wait I will post back in a moment i think i may have idea why not working. multiple defition of 'PlayerChoice'
multiple definition of `e'
multiple definition of `E'
multiple definition of `UserName'
#include "inn.h"
#include "globals.h"
#include <iostream>
usingnamespace std;
Inn::Inn()
{
do{
cout << "Welcome to the Inn How may I be of service today?\n";
system("pause");
cout << "1) Rest\n";
cout << "E) Exit the inn\n";
cin >> PlayerChoice;
cout << UserName << "you are now well rested\n\n";
}
while(PlayerChoice == 1);
if(PlayerChoice == E || e){
}
}
#include <iostream>
#include <string>
#include "inn.h"
#include "globals.h"
#include "Store.h"
usingnamespace std;
int main()
{
cout << "Welcome to Cinz\n";
system("pause");
cout << "What would you like to do?\n";
cout << "1) Play\n";
cout << "E) Exit\n";
cin >> PlayerChoice;
if(PlayerChoice == 1)
{
cout << "Many years ago when I was child my dad left he told me keep your mother safe I will be back tomorrow. I sat there at the window for days just waiting for him to return. Everytime I fell asleep all I could see was his face fading away from my eyes. It had been years before I let him go. Now everytime I here his name Rale. My body builds up constant rage. Now that he is gone I have set out to avenge her.\n\n";
system("pause");
cout << "What is your name: ";
cin >> UserName;
cout << "Hello " << UserName << " What would you like to do?\n\n";
cout << "1) Go to inn\n";
cout << "2) Go to Store\n";
cout << "3) Go exploring\n";
cout << "4) Character page\n";
cout << "5) Inventory page\n";
cout << "E) Exit Program\n";
cin >> PlayerChoice;
}
if(PlayerChoice == 1)
{
Inn go;
}
elseif(PlayerChoice == 2){
Store go;
}
elseif(PlayerChoice == E || e)
{
return 0;
}
return 0;
}
If you are going to have the global variables you don't need to have the globals class. The extern declarations should go in the header and the declarations should be in the source file, and everything should be outside class/function defintions.