.h file

I'm trying to make this into a .h file and a new object, a storyWordManager object, instantiated from a StoryWordManager class. I made each class with its own .cpp file.

// tc jobs.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;



string& askText(const string& promptRef);
int askNumber(const string& promptRef);
void tellStory(string& a, string& b, int& c, string& d, string& e);
int askNumber(const string& promptRef);

int _tmain(int argc, _TCHAR* argv[])
{
cout << "Welcome to Mad Lib.\n\n";
cout << "Answer the following questions to help create a new story.\n";
string name = askText("Please enter a name: ");
string noun = askText("Please enter a plural noun: ");
int number = askNumber("Please enter a number: ");
string bodyPart = askText("Please enter a body part: ");
string verb = askText("Please enter a verb: ");
tellStory(name, noun, number, bodyPart, verb);
system ("pause");
return 0;
}
string& askText(const string& promptRef)
{
string text;
cout << promptRef;
cin >> text;
return text;
}
int askNumber(const string &promptRef)
{
int num;
cout << promptRef;
cin >> num;
return num;
}
void tellStory(string& a, string& b, int& c, string& d, string& e)
{
cout << "\nHere's your story:\n";
cout << "The famous explorer ";
cout << a;
cout << " had nearly given up a life-long quest to find\n";
cout << "The Lost City of ";
cout << b;
cout << " when one day, the ";
cout << b;
cout << " found the explorer.\n";
cout << "Surrounded by ";
cout << c;
cout << " " << b;
cout << ", a tear came to ";
cout << a << "'s ";
cout << d << ".\n";
cout << "After all this time, the quest was finally over. ";
cout << "And then, the ";
cout << b << "\n";
cout << "promptly devoured ";
cout << a << ". ";
cout << "The moral of the story? Be careful what you ";
cout << e;
cout << " for.\n\n";
}
First, INDENT ALL CODE. You don't know how annoying it is to read mashed code that has no indentations.
Second, USE CODE TAGS.

Onto your problem...
Place all of your functions in a separate file with a .h extension.

Add #include <iostream> , #include <string> , and using namespace std; to the top of the .h file.

Or in other words... add all the needed include files and the proper namespace declarations to the top of the file ABOVE your functions.

Now, delete all of the function code from your main .cpp file.

Include the new .h file at the top of your main .cpp file. (NOTE: Make sure the .h file is in the same directory as the main .cpp file or specify the file name)

Example : #include "myfile.h" or
#include "C:\My Programs\myfile.h"

Example of your program:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <cstdlib>
#include "myfile.h"

using namespace std;

int main()
{
    /* Do something */
    return 0;
}


To make the function into a class, enclose the functions within a class and instantiate and object.

1
2
3
4
5
6
7
8
9
10
11
12
class storyWordManager
{
    public:
        //Place your functions here
};


int main()
{
    storyWordManager wm; 
    //Do stuff here
}




Last edited on
should I remove const from the string? My ident button doesn't work.

// i. : Defines the entry point for the console application.
//

#include <iostream>
#include <string>
#include <cstdlib>
#include "madlib.h"

using namespace std;

int main()
{
int main()
{
askguess()
}

}

class storyWordManager
{
public:
inline bool match(char letter, string word)
{
return(word.find(letter)!= string::npos);
}

I call it within the program and follow that by what I'm supposed to do with the return value like this:

cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
match(guess, soFar);

while (match(guess, soFar))

{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
return guess;

};


int main()
{
.
.
.
string theWordSoFar(THE_WORD.size(), '-');
soFar = theWordSoFar;
.
.
.
}


}

string& askText(const string& promptRef);
int askNumber(const string& promptRef);
void tellStory(string& a, string& b, int& c, string& d, string& e);
int askNumber(const string& promptRef);

int _tmain(int argc, _TCHAR* argv[])
{
cout << "Welcome to Mad Lib.\n\n";
cout << "Answer the following questions to help create a new story.\n";
string name = askText("Please enter a name: ");
string noun = askText("Please enter a plural noun: ");
int number = askNumber("Please enter a number: ");
string bodyPart = askText("Please enter a body part: ");
string verb = askText("Please enter a verb: ");
tellStory(name, noun, number, bodyPart, verb);
system ("pause");
return 0;
}
string& askText(const string& promptRef)
{
string text;
cout << promptRef;
cin >> text;
return text;
}
int askNumber(const string &promptRef)
{
int num;
cout << promptRef;
cin >> num;
return num;
}
void tellStory(string& a, string& b, int& c, string& d, string& e)
{
cout << "\nHere's your story:\n";
cout << "The famous explorer ";
cout << a;
cout << " had nearly given up a life-long quest to find\n";
cout << "The Lost City of ";
cout << b;
cout << " when one day, the ";
cout << b;
cout << " found the explorer.\n";
cout << "Surrounded by ";
cout << c;
cout << " " << b;
cout << ", a tear came to ";
cout << a << "'s ";
cout << d << ".\n";
cout << "After all this time, the quest was finally over. ";
cout << "And then, the ";
cout << b << "\n";
cout << "promptly devoured ";
cout << a << ". ";
cout << "The moral of the story? Be careful what you ";
cout << e;
cout << " for.\n\n";
}
What's the problem? Next time place code tags around your code. It makes for much easier reading.
const doesn't have to be removed in your functions because no modifications have to be made to the string. It also ensures that nothing can happen to the string.
Last edited on
closed account (z05DSL3A)
How to: Put code into your postings
http://www.cplusplus.com/forum/articles/1624/
Topic archived. No new replies allowed.