dipshit-level programming help needed

Speed RacerSpeed Racer Scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratchscritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratchRegistered User regular
edited March 2011 in Help / Advice Forum
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!

Speed Racer on

Posts

  • DaenrisDaenris Registered User regular
    edited March 2011
    if (Character = '_')
    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.

    Daenris on
  • Speed RacerSpeed Racer Scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratchRegistered User regular
    edited March 2011
    well

    it's good to know I gave this thread an apt title.

    Speed Racer on
  • khainkhain Registered User regular
    edited March 2011
    You should be trying to write compact code if possible and the one big thing I noticed is that you output characters immediately after you modify them instead of combing the statements at the end of the while loop. I also think this probably would have been a lot easier to write using the string library and getline function, but at this point I'd just continue with the getchar route.

    Pseudo code example:
    While(!eof)
    {
    if(digit)
    {
    character = '*';
    asterisk++;
    }
    else if(isalpha)
    {
    character = lower(character);
    }
    else if(is _)
    {
    character = ' ';
    }
    cout << character;
    fout << character;
    charcount++;
    }

    khain on
  • Speed RacerSpeed Racer Scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratchRegistered User regular
    edited March 2011
    That does make more sense, thanks for that.

    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:
    void textEditor(ifstream& in_stream, ofstream& out_stream)
    {
    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.

    Speed Racer on
  • InfidelInfidel Heretic Registered User regular
    edited March 2011
    You need to check for EOF immediately after attempting to read, instead you are performing your logic on the next character and then checking for EOF.

    If the next character fails (because there are no more), you run through everything one extra time.

    Infidel on
    OrokosPA.png
    Play D&D 4e? :: Check out Orokos and upload your Character Builder sheet! :: Orokos Dice Roller
    The PhalLounge :: Chat board for Critical Failures IRC! :: #CriticalFailures and #mafia on irc.slashnet.org
  • Speed RacerSpeed Racer Scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratch scritch scratchRegistered User regular
    edited March 2011
    Oh, okay. Thanks!

    Speed Racer on
  • DietCokeTinDietCokeTin Registered User
    edited March 2011
    Good practice to start is to put the result being tested before the variable in conditional statements. For example:
    if (Character == '_')

    should be
    if ('_' == Character )

    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.

    DietCokeTin on
    [SIGPIC][/SIGPIC]
  • TofystedethTofystedeth veni, veneri, vamoosi Registered User regular
    edited March 2011
    Hey that's pretty good. I've never seen that but it makes sense. (Of course I program in Python so it wouldn't compile an assignment in a conditional anyway hoooooo)

    Tofystedeth on
    steam_sig.png
Sign In or Register to comment.