d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Me Out > Please :)
Prev12
Add Reply New Topic New Poll
Member
Posts: 10,884
Joined: Jul 7 2007
Gold: 316.00
Jun 11 2012 01:22pm
inFile >> noskipws >> ch;
while ( !inFile.eof() )
{
if ( prevchar == '.' || prevchar == '?' || prevchar == '!' || prevchar == ' ')
{
ch=toupper(ch);
}
else
{
ch=tolower(ch);
}
outputFile << ch;
inFile >> ch;
}

can someone tell me why this isn't working for me it just changes everything to lowercase
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 11 2012 06:16pm
For starters, i recommend following "prevchar" and watching it change.

learn to debug.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 11 2012 07:08pm
Prevchar isn't being set to anything during or before your loop
Member
Posts: 10,884
Joined: Jul 7 2007
Gold: 316.00
Jun 12 2012 03:17pm
I took a new approach and I want there to be a new line but its giving me more than 1 new line for some reason and I cannot figure out why

#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;

int main()
{

ifstream inFile;
ofstream outputFile;
char ch;
char prevchar = '.';
bool flag = 0;
bool flag2 = 0;
int num_char = 0;

inFile.open ( "sometext.dat.dat" ); //Opening the file
outputFile.open ( "output.txt" ); //Creating Output.txt

while ( inFile >> noskipws >> ch)
{
if ( prevchar == '.' || prevchar == '?' || prevchar == '!')
{
flag = true;
if (!isspace(ch))
{
outputFile << ' ';
num_char++;
}
}
else
{
ch=tolower(ch);
}

if (flag && !isspace(ch))
{
ch=toupper(ch);
flag = false;
}
if (num_char >= 50)
{
flag2 = true;
}
if (flag2 && isspace(ch))
{
outputFile << '\n';
flag2 = false;
num_char = 0;
}

outputFile << ch;
num_char++;

prevchar = ch;

}


inFile.close();
outputFile.close();
return 0;
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 13 2012 12:32am
heres the logic. take a look and try to understand what is happening.

Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
   char c;

   int sentence = 0;
   int words    = 0;
   int avgwords = 0;
   int cap_flag = 1;


   FILE *infile = fopen("input.txt", "r");
   FILE *outfile = fopen("output.txt", "w");

   if(infile != NULL){
       while(c != EOF){

           c = fgetc(infile);

           if(c == ' '){
               words++;
           }

           if(c == '.' || c == '?' || c == '!'){
               sentence++;
               cap_flag = 1;
               fputc(c, outfile);  //my butt fuck way around not trying to capp the period and space after the period lol
               c = fgetc(infile);
               fputc(c, outfile);
               c = fgetc(infile);
           }

           if(cap_flag){
               if((int)c >= (int)'a' && c <= (int)'z'){
                   c -= 32;
               }

               cap_flag = 0;
           }
           else{
               if((int)c >= (int)'A' && (int)c <= (int)'Z'){
                   c += 32;
               }
           }

           fputc(c, outfile);
           printf("%c", c);
       }
   }

   printf("\n\ntotal words = %d\n", words);
   printf("total sentences = %d\n", sentence);
   printf("average words per sentence = %d\n", words / sentence);

   fclose(infile);
   fclose(outfile);
   return 0;
}


input
Code
ThIs Is sOME Test Data. We WilL Use this to Test Our aPliCaton. Why shAlL we Use This? BecauSe I Say So.


output
Code
This is some test data. We will use this to test our aplicaton. Why shall we use this? Because i say so.ÿÿ


This post was edited by AbDuCt on Jun 13 2012 12:38am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 13 2012 11:59pm
meh fixed code so you dont "assume" there is always a space after the period.
Code

if(c == '.' || c == '?' || c == '!'){
             sentence++;
             cap_flag = 1;
             fputc(c, outfile);
             c = fgetc(infile);

             if(c == ' '){
                             fputc(c, outfile);
             }
             else{
                             fputc(' ', outfile);
             }
             c = fgetc(infile);
}


or something like that

to lazy to test it out
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll