So I'm working on an assignment for my intro to programming class. I've been given a .txt document containing the following:
>EBC;Iit_is a CaPital Mistake tO_theorize Before_one has DatA123.
The assignment is to make a program that reads this file and then does the following:
-Add ten to the ASCII value of the first six characters, and display them.
-edit the rest of the characters so that they're all lower case, the underscores have been replaced with spaces, and the numbers have been replaced with asterisks.
-output all the characters (minus the first six) to a new text file
-count the number of numbers converted to asterisks and the number of characters output to the new text file, and display these values.
I have this mostly complete, except for one small, really weird issue. The period at the end does not get output to the new text file. What makes this stranger is that if you count up all the characters after >EBC;I, including the period, you get 59. This is the number that my program lists as the number of characters being output to the new text file. So it's counting the period, but not actually sending it for some reason.
This is what I have so far. Obviously it's messy and I need to add some text and such but for the time being I'm just concerned with it producing the right answer.
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
char codeChar, Character;
int Code = 0, Asterisk = 0, CharCount = 0;
ifstream fin;
ofstream fout;
fin.open("textIn.txt");
fout.open("textOut.txt");
if (fin.fail())
{
cout << "The file failed to load.";
exit (1);
}
while (Code < 6)
{
fin.get(codeChar);
static_cast<int>(codeChar);
codeChar = codeChar + 10;
static_cast<char>(codeChar);
cout << codeChar;
Code ++;
}
cout << endl << endl;
while (! fin.eof())
{
fin.get(Character);
if (isdigit(Character))
{
cout << "*";
fout << "*";
Asterisk ++;
}
else
{
if (isalpha(Character))
{
if(isupper(Character))
{
cout << static_cast<char>(tolower(Character));
fout << static_cast<char>(tolower(Character));
}
else
{
cout << Character;
fout << Character;
}
}
else
{
if (Character = '_')
{
cout << " ";
fout << " ";
}
else
{
cout << Character;
fout << Character;
}
}
}
if (! fin.eof())
CharCount ++;
}
fin.close();
fout.close();
cout << endl << endl << Asterisk << " " << CharCount;
system("pause");
return 0;
}
any help would be appreciated!
Posts
should be
if (Character == '_')
Currently it's always resolving as true, since it's just an assignment. So for any character that's not a number, alphabetic, or the first 6, it would just output a space.
it's good to know I gave this thread an apt title.
http://www.audioentropy.com/
Pseudo code example:
{
if(digit)
{
character = '*';
asterisk++;
}
else if(isalpha)
{
character = lower(character);
}
else if(is _)
{
character = ' ';
}
cout << character;
fout << character;
charcount++;
}
I'm running into another issue. Now instead of not outputting a period at all, it's outputting two. I've split it up now so here's just the relevant function:
{
char Character;
while (! in_stream.eof())
{
in_stream.get(Character);
if (isdigit(Character))
{
Character = '*';
Asterisk ++;
}
else if (isalpha(Character))
{
Character = static_cast<char>(tolower(Character));
}
else if (Character == '_')
{
Character = ' ';
}
cout << Character;
out_stream << Character;
CharCount ++;
}
CharCount and Asterisk are global values.
EDIT: I've fixed it by making it a do while loop that terminates when Character = '.', but I'm still curious about where that second period is coming from.
http://www.audioentropy.com/
If the next character fails (because there are no more), you run through everything one extra time.
http://www.audioentropy.com/
should be
That way, if you ever accidentally use assignment instead of equals, the program won't compile and you'll know exactly where you made the mistake.