Reputation: 19
Need a little help. I have a code that I have written out for a project. I get confused on how to separate the code into separate .h and .cpp files. Most of the searches come up with using a class. I don't have a class in this program. I am only using a map and a vector. I'm not entirely sure how to do this and I would like to know how too!
the code is:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
map<int, string> Broncos_Roster;
Broncos_Roster[88] = " Demaryius Thomas, Georgia Tech, WR ";
Broncos_Roster[72] = " Garret Bolles , Utah, LT ";
Broncos_Roster[76] = " Max Garcia , Florida, LG ";
Broncos_Roster[61] = " Matt Paradis, Bosie State, C ";
Broncos_Roster[65] = " Ron Leary, Memphis, RG ";
Broncos_Roster[75] = " Menelik Watson, Florida State, RT ";
Broncos_Roster[85] = " Virgil Green, Nevada, TE ";
Broncos_Roster[10] = " Emmanuel Sanders, Southern Methodist ";
Broncos_Roster[22] = " C.J. Anderson, California, RB ";
Broncos_Roster[13] = " Trevor Siemian, North Western, QB ";
Broncos_Roster[32] = " Andy Janovich, Nebraska, FB ";
Broncos_Roster[95] = " Derek Wolfe , Cincinnati, DE ";
Broncos_Roster[94] = " Domata Peko Sr. , Michigan State, NT ";
Broncos_Roster[93] = " Jared Crick, Nebraska, DE ";
Broncos_Roster[58] = " Von Miller, Texas A & M, WLB ";
Broncos_Roster[54] = " Brandon Marshall, Nevada, ILB ";
Broncos_Roster[51] = " Todd Davis, Sacramento State, ILB ";
Broncos_Roster[21] = " Aqib Talib, Kanas, LCB ";
Broncos_Roster[27] = " Brendan Langley, Lamar, RCB ";
Broncos_Roster[31] = " Justin Simmons , Boston College, SS ";
Broncos_Roster[26] = " Darian Stewart , South Carolina , FS ";
Broncos_Roster[8] = " Brandon McManus, Temple, PK ";
Broncos_Roster[9] = " Riley Dixon , Syracuse, P ";
Broncos_Roster[42] = " Casey Kreiter , Iowa, LS ";
Broncos_Roster[14] = " Cody Latimer , Indiana KR ";
Broncos_Roster[84] = " Isaiah Mckenzie, Georgia, PR ";
for (auto &it : Broncos_Roster)
cout << it.first << " => " << it.second << '\n';
vector<string> phoneticAlphabet;
cout << " This is the Phonetic Alphabet" ;
phoneticAlphabet.emplace_back("Alpha") ;
phoneticAlphabet.emplace_back("Bravo") ;
phoneticAlphabet.emplace_back("Charlie") ;
phoneticAlphabet.emplace_back("Delta") ;
phoneticAlphabet.emplace_back("Echo") ;
phoneticAlphabet.emplace_back("Foxtrot") ;
phoneticAlphabet.emplace_back("Golf") ;
phoneticAlphabet.emplace_back("Hotel") ;
phoneticAlphabet.emplace_back("India") ;
phoneticAlphabet.emplace_back("Juliett") ;
phoneticAlphabet.emplace_back("Kilo") ;
phoneticAlphabet.emplace_back("Lima") ;
phoneticAlphabet.emplace_back("Mike") ;
phoneticAlphabet.emplace_back("November") ;
phoneticAlphabet.emplace_back("Oscar") ;
phoneticAlphabet.emplace_back("Papa") ;
phoneticAlphabet.emplace_back("Quebec") ;
phoneticAlphabet.emplace_back("Romeo") ;
phoneticAlphabet.emplace_back("Sierra") ;
phoneticAlphabet.emplace_back("Tango") ;
phoneticAlphabet.emplace_back("Uniform") ;
phoneticAlphabet.emplace_back("Victor") ;
phoneticAlphabet.emplace_back("Whiskey") ;
phoneticAlphabet.emplace_back("Xray") ;
phoneticAlphabet.emplace_back("Yankee") ;
phoneticAlphabet.emplace_back("Zulu") ;
cout << " Vector size = " << phoneticAlphabet.size() << endl;
for (auto &itt : phoneticAlphabet)
cout << itt << "\n";
string query = " ";
cout << " Please select a letter to see the ! ";
//cin.ignore();
getline(cin, query);
string letters[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z"};
cout << "you entered a " << query ;
int index = 0;
for (int i = 0; i < 26; i++)
{
if (query == letters[i])
{ index = i; }
};
cout << " phonetic letter = " << phoneticAlphabet[index] << endl;
system("pause");
}
I appreciate any help!
Upvotes: 0
Views: 147
Reputation: 92
Since you don't want a class
, If Broncos_Roster
and phoneticAlphabet
can be public, just put them in a header file -- say myHeader.h
; or else, put their contents into a cpp file myHeader.cpp
, and myHeader.h
just keep the interfaces.
Of-course, you can use OOP design concepts like:
Use namespace: namespace MyData { ... }
Use methods/functions to get the map and vector, in myHeader.h
like:
const map<int, string>& get_Broncos_Roster() { // return Broncos_Roster; }
If the map and vector in your code are constant, it's better to use C++ new feature to make them constant, like:
const map<int, string> Broncos_Roster = {
{ 88, " Demaryius Thomas, Georgia Tech, WR " },
{ 72, " Garret Bolles , Utah, LT " },
... ...
{ 14, " Cody Latimer , Indiana KR " },
{ 84, " Isaiah Mckenzie, Georgia, PR " },
};
// if it's static and const
static const vector<string> phoneticAlphabet = {
"Alpha",
"Bravo",
... ...
"Yankee",
"Zulu",
};
By the way, to replace the map and vector to constant:
msr -p your.cpp -t "Broncos_Roster\s*\[([^\]]+)\]\s*=\s*(.+?)\s*;" -o "{ $1, $2 }," -R
msr -p your.cpp -t "phoneticAlphabet.emplace_back\((.+)\);" -o "$1," -R
See screen-shots below: ( msr.exe
/ msr.gcc*
is a single exe tool in my open project : https://github.com/qualiu/msr in tools
directory.)
Upvotes: 0
Reputation: 542
Considering you wrote everything in your main it is not really necessary to separate your code in source and header files. Your header file would be empty since there's no functions to prototype, and your main IS in a sourcefile.
If you have to for some reason then you would have to divide your main into separate functions first and put those in another source file with it's own header file.
Upvotes: 0
Reputation: 73366
You don't need to separate your code.
But, if you are obliged to, then:
Use a function to initialize your map and one for initializing your vector. Place these two functions in a source file, and provide a header file for it too.
Include that header file in main()
, and replace the initialization code with two calls to these functions and you are done.
I would not separate my code in files, if I had the choice, but use the functions described above.
Upvotes: 1
Reputation: 693
In your case, it is not really necessary to separate the code into header and source files.
But if you wanted to (say, for learning or practice), the general idea is to place function declarations in the .h file and the function implementations in the .cpp file. So, if you were to create a function, populateRoster(), you would place the one-line declaration of the function in the header file:
void populateRoster();
And then the complete function text in any source file:
void populateRoster()
{
// do something...
}
The source file must #include the header file. Then you could call populateRoster() from main() or from any other function that includes the header file.
Of course, you could still call it from main() even without creating the header file if you just define it in the same .cpp file as main(), above main().
Upvotes: 2