Project help

Hey everyone,

I am making a madlib program and would like a better direction or understanding on what I need to do.

I need to read a file and find the tokens(@@) and read inbetween them and then get user input and read file again then change whats inbetween the tokens with the input. But I am stuck on one part.

I have it read the file and store the tokens in the map. I had it hard coded, but he doesn't want it like that, so I have it get the tokens into the map. It is stored into the first in the map and the second part has nothing besides an empty string. He wants us to add the description when we find the token, but thats where im confused. How can we give it a description if it is stored into a variable called token to get whats inbetween the @..@ and then have it but into the map and repeat untill it has nothing else to read. But since he wants it that way. How would you put a description to the second part of the map if you don't really know what it is.

text file would be something like
To @BE@ or not to @BE@ that is the @noun@

Also my header substMap.h just has typedef of the map template.

Hopefully I made sense, but if you could just be like, hey maybe you should try this or it may work better if you move such and such, etc, but thank you if you can help! I apperciate it very much

I'll show you what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>

#include "substMap.h" 

using namespace std;


/*----------------------------------------------------------
							GETINPUT

~SETS A NEW VARIABLE CALLED "userInput" AS A STRING
	-GETS THE CONSTANT SUBSTITUE ITERATOR AND SETS IT TO
		"it" THEN IS EQUAL TO THEMAP BEGINING AND "it" IS
		NOT EQUAL TO THEMAP END FUNCTION AND "it" GOES UP
		BY ONE
		-PRINTS OUT THE FIRST PART OF THEMAP FROM "it"
~NEED TO SET THE DESCRIPTION OF EACH TOKEN STORED AND CHANGE
	FIRST TO SECOND AND GET USERINPUT
------------------------------------------------------------*/

void getInput(SubstMap& theMap)
{
	string userInput;

	for(ConstSubstIter it = theMap.begin(); it != theMap.end(); it++)
	{
		cout << it->first << endl;
	}
}


/*--------------------------------------------------------------------------
									FINDSUBS

~SETS THREE NEW VARIABLE
	-"token" TO A STRING
	-SIZE_TYPE "start"(0) TO A STRING
	-SIZE_TYPE "end" TO A STRING
		~END IS EQUAL TO ONELINE AND FIND_FIRST_OF THE "@" AND START
	-CHECKS TO SEE IF THE END IS NOT AT THE END OF THE STRING WITH "npos"
		~TOKEN IS SET TO GET THE ONELINE FROM USING SUBSTR(SUBSTRING)
			FROM START TO END MINUIS THE START
		~START IS THEN SET TO END PLUS ONE
		~THEN END IS RESET WITH THE UPDATED START POSITION
	-CHECKS THEMAP TO SEE IF IT IF FINDS A TOKEN AND IS NOT EQUAL TO THE
		TO THEMAP END FUNCTION
		~IF FINDS THE TOKEN IN THEMAP, IT DOESNT DO ANYTHING, SINCE IT IS
			ALREADY FOUND
		~IF IT DOESN'T FIND THE TOKEN, IT THEN ADDS IT TO THEMAP
----------------------------------------------------------------------------*/

void findSubs(string& oneLine, SubstMap& theMap )
{
	/* FINDS EACH SUB IN THE TEXT */
	string token;
	string::size_type start(0);
	string::size_type end;
	
	end = oneLine.find_first_of( "@", start);
	
	/* WHILE IT IS NOT AT THE END OF THE STRING */
	while (end != string::npos)
	{
		token = oneLine.substr( (start), (end - start) );
		int pos = end + 1;
		start = pos;
		end = oneLine.find_first_of("@", start);

		if (theMap.find(token) != theMap.end())
		{

		}
		else
		{
			theMap[ token ] = "";   
		}
	}
}

/*-----------------------------------------------------------------------------
					FIRSTPASS

~SETS A NEW VARIABLE "oneLine" TO A STRING
~CHECKS TO SEE IF THE STREAM IS NOT AT THE END OF FILE
	-USE GETLINE TO READ A LINE AT A TIME WITH ONELINE
	-CHECKS IF THE STREAM FAILS
		~IT THEN CLEARS THE FAIL
		~THEN RETRIES THE FIRSTPASS AGAIN
	-THEN IT CALLS THE FINDSUBS WITH ONELINE AND THEMAP AS THE PARAMETERS
-------------------------------------------------------------------------------*/

void firstPass( ifstream& strm, SubstMap& theMap )
{
	string oneLine;


	/* CHECKS TO SEE IF THE FILE IS NOT AT THE END */

	while (!strm.eof())											
	{
		getline(strm, oneLine);

		/* GETS RID OF THE ERRORS */

		if (strm.fail())											
		{
			strm.clear();
			strm.ignore(1024, '\n');							
			continue;
		}

		findSubs(oneLine, theMap);
	}
}



/*------------------------------------------------------------------ 
						MAIN

~SETS THE STREAM TO ADD IT TO THE COMMAND LINE
	-CHECKS TO SEE IF THE FILE IS NOT GOOD
		~PRINTS ERROR MESSAGE IF TRUE AND EXITS THE PROGRAM
~SETS THE SUBSTITUTION MAP TO "theMap"
~CALLS THE FIRSTPASS WITH THE STREAM AND THEMAP AS THE PARAMETERS
~THEN CALLS THE GETINPUT WITH THEMAP AS THE PARAMETERS
---------------------------------------------------------------------*/

int main(int argc, char* argv[])
{
	ifstream strm( argv[1] );
	if ( !strm.good() )
	{
		cout << "File is not good";
		exit(1);
	}

	SubstMap theMap;

	firstPass( strm, theMap );
	getInput(theMap);
	
	return 0;
}
findSubs() isn't quite right. The algorithm might better be:
findSubs:
start := find first @
end := find next @

while (end found @)
{
extract string between start and end
add string to map

start := find first @
end := find next @
}
Alright I will give that a try. But also adding the string to the map. He wanted it in the if then else, so it can add it to the map if it was not found. I'm pretty sure the algorithm could be better. Also thanks for the input!
Topic archived. No new replies allowed.