mercredi 1 juillet 2015

How do I limit my output to 40 characters per line without truncating a word in the string?

I am working on an assignment and I have pretty much figured it all out but keep in mind I am not allowed to use arrays nor streams for this program. I must input string data from a file and display 40 characters per line of the data in the file without cutting off any of the words. This part of the problem has stumped me. I am also stumped on how to figure out how to capitalize the first letter of a word if it has symbols attached to the front. I was thinking that the 40 characters per line would work best in my void function for print_text and the capitalization should go in format_text. I have tried my best and was able to successfully capitalize the first letter and make the last letters of each word lower case, but I cannot figure out how to do it if the word has symbols in front (ex. **running)

What I have so far

#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
void get_text(string&);
void format_text(string&);
void print_text(string);
int alphacount(string);
double charcount(string);
int consonantcount(string);
int sentencecount(string);
int main()

 {
  string filetext; // Text data input from file                                                                                                                                                                        
  int letters = 0; // amount of letters input from file                                                                                                                                                                
  int words = 0; // amount of words input from file                                                                                                                                                                    
  int sentences = 0; // amount of sentences input from file                                                                                                                                                            
  int consonants = 0;// amount of consonants input from file                                                                                                                                                           
  double amtchar  = 0.0;// amount of characters in file                                                                                                                                                                
  double average = 0.0; // average length of words in file                                                                                                                                                             
  cout << fixed << setprecision (3);
  get_text(filetext); // reads text in file                                                                                                                                                                            
  while (cin)
    {
      words++;
      sentences = sentencecount(filetext) + sentences;
      letters = alphacount(filetext) + letters;
      amtchar = charcount(filetext) + amtchar;
      consonants = ( consonants +   consonantcount(filetext));
      average = amtchar/ words;
      format_text(filetext);// formats text in file correctly                                                                                                                                                          
      print_text(filetext);// outputs data from file                                                                                                                                                                   
      get_text(filetext);
    }

  cout << "# of words: " << words << endl;
  cout << "# of sentences; " << sentences  << endl;
  cout << "# of letters: " << letters << endl;
  cout << "# of consonants: " << consonants << endl;
  cout << "Average word length: " << average << endl;
  return 0;
}

void get_text(string& ftext)
// Given a string of data from a file the function reads the strings and passes                                                                                                                                        
// them back to the calling function.                                                                                                                                                                                  
{
  cin >> ftext;
}

void print_text(string ftext)
// Given the string of text from the file the function displays the contents                                                                                                                                           
// of the file on screen.                                                                                                                                                                                              
{
  cout << setw(40) << ftext << endl;
}

void format_text(string& ftext)
  // Give the string of text from the file the function displays the contents                                                                                                                                          
  // so that the first letter of each word is capitalized and each follow word                                                                                                                                         
  // is lower case.                                                                                                                                                                                                    
{
  int wlength;// length of the word inputted from the file                                                                                                                                                             
  wlength = ftext.length();
  if (isalpha(ftext[0]))
  ftext[0] = toupper(ftext[0]);
  for ( int i=1; i < wlength; i++)
    ftext[i] = tolower(ftext[i]);

}

int alphacount(string filetext)
// will count the amount of alphabetical letters in the data file                                                                                                                                                      
{
  int lettertotal = 0; // total amount of letters in the datafile                                                                                                                                                      
  for (int i = 0; i < filetext.length(); i++)
      if ( isalpha(filetext[i]))
         lettertotal++;
         return lettertotal;
}


double charcount(string filetext)
// will calculate the amount of characters within the file and return to                                                                                                                                               
// main file                                                                                                                                                                                                           
{
  int numofchars = 0;// number of characters within file                                                                                                                                                               
  int sum = 0;// variable to determine exact amount of characters                                                                                                                                                      
  for (int i = 0; i < filetext.length(); i++)
    {
    if ( filetext != " ")
      numofchars++;

    }
  sum = sum + numofchars;
  return sum;
}

int consonantcount(string filetext)
// will determine amount of consonants  within the datafile and return to                                                                                                                                              
// main function                                                                                                                                                                                                       
{

char letter; // each alphabetical character in data file                                                                                                                                                             
  int vowelcounter=0;// number of vowels within data file                                                                                                                                                              
  int constotal = 0;// number of consonants within data file                                                                                                                                                           
  for (int i = 0; i < filetext.length(); i++)
    {
      letter = toupper(filetext[i]);
      switch(letter)
        {
          case 'a':
          case 'e':
          case 'i':
          case 'o':
          case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            vowelcounter++;
          break;
        }
      if  ( isalpha(filetext[i]))
        constotal++;
    }
  constotal = constotal - vowelcounter;
  return constotal;
}


int sentencecount(string filetext)
// Will count the number of sentences within the data file and return to the                                                                                                                                           
// main function                                                                                                                                                                                                       
{
  int sentcount = 0; // amount of sentences within file                                                                                                                                                                
  for (int i = 0; i < filetext.length(); i++)
    {
      if (filetext[i] == '.' || filetext[i] == '?' || filetext[i] == '!')
        sentcount++;
    }
  return sentcount;
}

Aucun commentaire:

Enregistrer un commentaire