GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2008, 12:52
Mrehman Mrehman is offline
New Member
 
Join Date: Mar 2008
Posts: 23
Mrehman is on a distinguished road

how to replace an a b c d letters with the tilde key?


hey guyz back again unfortunately sorry about that. it's my project and it's due tonight, and i am like stuck on this. i tried all possible ways i know but no luck. hope you guyz can help. i have uploaded 2 files. first refer to question no 1. second refers to q no 2.
my first question is in the first file out.txt how do i erase the numbers(1,2,3 ...) infront of every sentence?
my second question is in the 2nd file mc.txt how do i replace all a b c d choice with a tilde key. so instead of saying
Washington is ... a. state b. city
c. continent d. colony, i want it to be
Washington is a .... ~state ~city
~continent ~colony
here's my code
CPP / C++ / C Code:
# include <iostream>
# include <string>
# include <stdlib.h>
# include <stdio.h>
# include <fstream>

using namespace std;
void ConvertFile (ifstream &infile, ostream &outfile);

void main()
{
	char inFileName[100], outFileName[100];
	ifstream infile;
	ofstream outfile;

	cout << "Input file: ";
	cin >> inFileName;
	cout << "Output file: ";
	cin >> outFileName;

	infile.open(inFileName, ios_base::in);
	if(infile.fail())
	{
		cout << "The file did not open successfully" << endl;
		exit(1);
	}

	outfile.open(outFileName);
	if(outfile.fail())
	{
		cout << "The file did not open successfully" << endl;
		exit(1);
	}

	ConvertFile(infile, outfile);
	infile.close();
	outfile.close();
}

void ConvertFile(ifstream &infile, ostream &outfile)
{
	while(!(infile.eof())) 
	{
		string line, substr;
		int index;		
		getline(infile, line);

		{
			substr = line.substr(0, line.length());
			index = line.length();
			outfile << substr<<line[index] << endl; 
			
		}
		
	}
}
Thanks a lot
Attached Files
File Type: txt out.txt (22.6 KB, 3 views)
File Type: txt mc.txt (36.4 KB, 3 views)
  #2  
Old 11-May-2008, 15:35
Mrehman Mrehman is offline
New Member
 
Join Date: Mar 2008
Posts: 23
Mrehman is on a distinguished road

Re: how to replace an a b c d letters with the tilde key?


EDITED CODE
CPP / C++ / C Code:
# include <iostream>
# include <string>
# include <stdlib.h>
# include <stdio.h>
# include <fstream>

using namespace std;

void ConvertTrueFalseFile (ifstream &infile, ostream &outfile);
void ConvertMultipleChoiceFile (ifstream &infile, ostream &outfile);

void main()
{
	//declaring variables
	char inFileName[100], outFileName[100];
	string fileType;
	ifstream infile;
	ofstream outfile;

	//asking what file you would like to access
	cout << "What kind of file you want to convert (T/F File = tf, Mult Choice File = mc)?";
	cin >> fileType;

	//asking location of the input and output file
	cout << "Please enter the location (path) of the Input file: ";
	cin >> inFileName;
	cout << "Please enter the location (path) of the Output file: ";
	cin >> outFileName;

	//opening the input file 
	infile.open(inFileName, ios_base::in);

	//if input file fails to open then exiting
	if(infile.fail())
	{
		cout << "The file did not open successfully" << endl;
		exit(1);
	}

	//opening the output file to write in
	outfile.open(outFileName);

	//if output file fails then exiting
	if(outfile.fail())
	{
		cout << "The file did not open successfully" << endl;
		exit(1);
	}

	// if matched input file then passing it to its correct function
	if (fileType == "tf")
	{
		ConvertTrueFalseFile(infile, outfile);
	}
	//if other input matched, passing it to its correct function
	else if (fileType == "mc")
	{
		ConvertMultipleChoiceFile(infile, outfile);
	}
	//otherwise displaying the error message and exiting program
	else
	{
		cout << "Wrong File type...use 'tf' for True False or 'mc' for Multiple choice..."
			 << "Exiting Program!" << endl;
		
		infile.close();
		outfile.close();
		exit(-1);
	}

	//closing input file and output file after opening
	infile.close();
	outfile.close();
}

//funtion convert TrueFalse
void ConvertTrueFalseFile(ifstream &infile, ostream &outfile)
{
	//when pointer is not a tthe end of file 
	while(!(infile.eof())) 
	{
		//declaring variables
		string line, substr;
		int index;

		//reading in line from input file
		getline(infile, line);

		//adding a blank line if its <= 1
		if (line.length() <= 1)
		{
			outfile << line << endl;
		}
		//compeleting the rest of the program
		else
		{
			substr = line.substr(0, line.length()-2);
			index = int(line.length()-1);
			outfile << substr << "{" << line[index] << "}" << endl;	
		}	
	}
}
// funtion COnvert MultipleChoice
void ConvertMultipleChoiceFile (ifstream &infile, ostream &outfile)
{
	//when pointer is not at the end of file
	while(!(infile.eof())) 
	{
		//declaring variables
		string line;
		//reading in line
		getline(infile, line);
		outfile << line;
	}
}
  #3  
Old 11-May-2008, 17:50
TurboPT's Avatar
TurboPT TurboPT is online now
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 926
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: how to replace an a b c d letters with the tilde key?


In the true/false file, one way to eliminate the leading #'s ( and the period ) would be to change this line:
CPP / C++ / C Code:
  // the -2 portion takes two off the length of the line
  // which takes the string from the beginning up to the 
  // last two characters. 
  substr = line.substr(0, line.length()-2);
to this: (feel free to reduce/eliminate the comments -- only added
for explanation)
CPP / C++ / C Code:
        // find the first period-space occurrences, but NOTE that
        // this assumes the file is in a consistent format following
        // the leading numbers. Could (assume WILL) be problematic
        // otherwise.
        int position = line.find(". ");  // find() gives -1 when not found.

        if ( position > 0 )
	{					 
					// where the period-space 
					// was found add two for
					// the length of:
					// ". " (period space) to
					// strip it away.
			substr = line.substr( position + 2, line.length());
   // not sure if the other two lines you had after this statement
   // should be grouped here in the if (or not), but see the sample
   // output, below. [I left the other two statements outside the if]
	}
...and here's a sample from the output file with that change applied:
Code:
To discard an old computer safely, put it in a landfill.{F}{}} Application software serves as the interface between the user, the system software, and the computer hardware.{F}{}} Programmers develop programs or write the instructions that direct the computer to process data into information.{T}{}} The distinction among categories of computers is always clear-cut.{F}{}}
HTH

EDIT:
As to the 2nd Q, it'll be similar in nature to what was done above, only seeking the a. b. c. d. (followed by a tab character)
For example find("a.\t"), and then do a replacement with the tilde.
HTH^2
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 11-May-2008 at 18:54.
  #4  
Old 11-May-2008, 20:07
Mrehman Mrehman is offline
New Member
 
Join Date: Mar 2008
Posts: 23
Mrehman is on a distinguished road

Re: how to replace an a b c d letters with the tilde key?


hey thanks a lot budd that was great."To discard an old computer safely, put it in a landfill.{F}{}}

Application software serves as the interface between the user, the system software, and the computer hardware.{F}{}}

Programmers develop programs or write the instructions that direct the computer to process data into information.{T}{}}

The distinction among categories of computers is always clear-cut.{F}{}}"
in this part i have got everything done but why do you have curly braces after you have put t or f in it like {F} {}} << why this instead of {F} . i have done everything though, thanks for your help.
  #5  
Old 11-May-2008, 20:13
TurboPT's Avatar
TurboPT TurboPT is online now
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 926
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: how to replace an a b c d letters with the tilde key?


I didn't add those, that comes from this original statement:
CPP / C++ / C Code:
    // the code that is adding extra braces.
    outfile << substr << "{" << line[index] << "}" << endl;	
Glad it's all working!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 11-May-2008, 22:59
Mrehman Mrehman is offline
New Member
 
Join Date: Mar 2008
Posts: 23
Mrehman is on a distinguished road

Re: how to replace an a b c d letters with the tilde key?


thanks a lot for helping
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sunday morning Humor cable_guy_67 Open Discussion Forum 237 29-Jun-2008 05:26
Deleting elements of arrays C++ the_crazyman CPP / C++ Forum 25 30-May-2008 07:27
Batch replace text srinivasanb4u C Programming Language 2 06-Oct-2007 19:20
Checking Capital Letters against Lower Case AnnetteB C Programming Language 1 20-Mar-2005 11:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.