my out put file comes out like 0105000011500970099001120011600108001 and so on for a while. any one help me figure out wat is wrong with my code.

oh yea and what is in the input file is

it-is a-CaPital Mistake tO-theorize Before-one has DatA123.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std; 
void editFile(ifstream& infile, ofstream& outfile);
int main()
{
	ifstream infile;
	ofstream outfile;
	cout << "Editing file will now begin.\n";
	//textin file
	infile.open("textin.dat");
	if (infile.fail())
	{
		cout << "Input file has failed to open, now exiting.\n";
		system("pause");
		exit(1);
	}
	//textout file
	outfile.open("textout.dat");
	if (outfile.fail())
	{
		cout << "Output file has failed to open, now exiting.\n";
		exit(1);
	}
	editFile(infile, outfile);
	
	infile.close( );
	outfile.close( );
	
	cout << "Editing file will now end.\n";
	system("pause");
	return 0;
}

void editFile(ifstream& infile, ofstream& outfile)
{
int countAlpha = 0;
int countSpace = 0;
while (! infile.eof()) 

	{
	char ch;
	infile.get(ch);
	//Change dashes to spaces.
	if (ch == '-');
		{
		outfile << (ch == ' ');
		countSpace = countSpace + 1;
		}
	//Change all characters to lowercase.
	if(isalpha(ch))
		{
		outfile << (tolower(ch));
		countAlpha = countAlpha + 1;
		}
	//Change digits to * (asterisks)
	if (isdigit(ch));
	{
	outfile << (ch == '*');
	}
	
	
	
	infile.get(ch);
	}
cout <<"The amount of spaces written to the output file is "<<countSpace<<endl;
cout <<"The amount of alphabetic characters written to the output file is "<<countAlpha<<endl;
}

Recommended Answers

All 5 Replies

It would be nice if you explained what this program is supposed to do. Given the input file what is the output file supposed to look like ?

the input file is wat i said at the top there

input:"it-is a-CaPital Mistake tO-theorize Before-one has DatA123."

output should look like "it is a capital mistake to theorize before one has data***."

theses things must do to get there, Change dashes to spaces, all characters to lowercase, Change digits to asterisks and
Count all spaces written to the output file, Count all alphabetic characters written to the output file.

the quotes are not apart of the files

line 42: that's not the right way to write that loop. eof() doesn't work like that.

while( infile.get(ch) )
{

}

line 50: all that is going to output is the boolian value 0 for false or 1 for true. And that is why your output file is incorrect. Change the == boolean operator to = assignment operator.

line 67: delete that line.

i made those correction and it outputs
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


heres the code again

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std; 
void editFile(ifstream& infile, ofstream& outfile);
int main()
{
	ifstream infile;
	ofstream outfile;
	cout << "Editing file will now begin.\n";
	//textin file
	infile.open("textin.dat");
	if (infile.fail())
	{
		cout << "Input file has failed to open, now exiting.\n";
		system("pause");
		exit(1);
	}
	//textout file
	outfile.open("textout.dat");
	if (outfile.fail())
	{
		cout << "Output file has failed to open, now exiting.\n";
		exit(1);
	}
	editFile(infile, outfile);
	//while (! infile.eof()) 
	//{
	//char ch;
	//infile.get(ch);
	////Change dashes to spaces.
	//if (ch == '-' );
	//	{
	//	outfile << (ch == ' ');
	//	}
	////Change all characters to lowercase.
	//if(isalpha(ch))
	//	{
	//	outfile << (tolower(ch));	
	//	}
	////Change digits to * (asterisks)
	//if (isdigit(ch));
	//{
	//outfile << (ch == '*');
	//}
	////Count all spaces written to the output file.

	////Count all alphabetic characters written to the output file
	//infile.get(ch);
	//}
	infile.close( );
	outfile.close( );
	
	cout << "Editing file will now end.\n";
	system("pause");
	return 0;
}

void editFile(ifstream& infile, ofstream& outfile)
{
int countAlpha = 0;
int countSpace = 0;
char ch;
	while( infile.get(ch) ) 
	{
		infile.get(ch);
		//Change dashes to spaces.
		if (ch == '-');
			{
			outfile << (ch = ' ');
			countSpace = countSpace + 1;
			}
		//Change all characters to lowercase.
		if(isalpha(ch))
			{
			outfile << (tolower(ch));
			countAlpha = countAlpha + 1;
			}
		//Change digits to * (asterisks)
		if (isdigit(ch));
		        {
		         outfile << (ch == '*');
		        }
		
		
	}
cout <<"The amount of spaces written to the output file is "<<countSpace<<endl;
cout <<"The amount of alphabetic characters written to the output file is "<<countAlpha<<endl;
}

this works

void editFile(ifstream& infile, ofstream& outfile)
{
int countAlpha = 0;
int countSpace = 0;
char ch;

while( infile.get(ch) ) 
{
    //Change dashes to spaces.
    if (ch == '-')
    {
        outfile << ' ';
        countSpace = countSpace + 1;
    }
    //Change all characters to lowercase.
    else if(isalpha(ch))
    {
        ch = tolower(ch);
        outfile << ch;
        countAlpha = countAlpha + 1;
    }
    //Change digits to * (asterisks)
    else if (isdigit(ch))
    {
        outfile << '*';
    }
    else
        outfile << ch;
    }
cout <<"The amount of spaces written to the output file is "<<countSpace<<endl;
cout <<"The amount of alphabetic characters written to the output file is "<<countAlpha<<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.