Thread: output string

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    output string

    the program is designed to read in from a file and stop at the end of file. the file consists of about 45 words, that we are asked to reformat, count the punctuation marks, and consonants, etc.. the problem i am having is when we are asked to display the content of the file with exactly 1 space between each word and a maximum of 50 characters per line (we cannot split a word between lines and we must come as close to 50 characters per line as possible). i have no idea what to do here. i have tried several things, and just cannot get it. should i try to setup another function, that does this, or is that not necessary? i appreciate any help, thank you. we are asked not to use arrays or structs, if that matters.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, post what you've got, and we can perhaps help you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    Code:
    #include<iomanip>
    #include<string>
    #include<cctype>
    #include<iostream>
    using namespace std;
    
    string format(string, int);    //format word, first letter capital, rest lower
    int findconsonants(string,int);  //used to count consonants
    int findpuncmarks(string, int);  //used to count punctuation marks
    
    
    int main()
    {
      string word;        //not using all of these, some are left over, from trial and error
      int wordcount=0;
      int consonants=0;
      int wordlength=0;
      double avgword;
      int puncmarks=0;
      string longestword="";
      string changedword="";
      string previousword="";
      
    
    cin >> word;
    
      cout << fixed << showpoint << setprecision(3);
     // cout << left << setw(50) << endl;  
    
    while (!cin.eof())
        {
          wordcount++;  //count words
    
          if(word.length() > longestword.length())  //find longest word
            longestword= word;
    
    
          word = format(word, word.length());  //call for function to reformat word
    
          puncmarks= puncmarks+findpuncmarks(word, word.length());
          consonants= consonants+findconsonants(word, word.length())-findpuncmarks(word,word.length());
    
    
    
          wordlength= wordlength+word.length();
    
    
          changedword= changedword+ word+' ';
    
    
    
          cin >> word;
        }
    
      cout << changedword << endl; //basic output
    
      avgword= (wordlength*1.0)/(wordcount*1.0);
    
      cout << endl << endl;
      cout << "# words: " << wordcount <<endl;
      cout << "# consonants: " << consonants-1 <<endl;
      cout << "ave word length: " << avgword <<endl;
      cout << "# punctuation marks: " << puncmarks <<endl;
      cout << "longest word: " << longestword << endl;
    
    
    
    
    
      return 0;
    }

    everything else works except the output of the words in the file. obviously the way it is now it just prints the words with a space between, until the line ends. but ive tried using if else stmts and other things... i just don't know how to separate the string changedword, without seperating every word.... is this enough of the code, or would like the functions too?
    Last edited by Salem; 11-09-2007 at 04:58 PM. Reason: Remember to use [code][/code] tags, not find other ways to get around the problem

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    consonants= consonants+findconsonants(word, word.length())-findpuncmarks(word,word.length());
    ????
    Why do you subtract the punctuation marks from the consonants?

    Code:
    avgword= (wordlength*1.0)/(wordcount*1.0);
    how about:
    Code:
    avgword= static_cast<double>wordlength/wordcount;
    That's really what you actually want to do anyways, right - just convert the expression to double.


    Code:
    cin >> word;
    ....
    while (!cin.eof())
    {
    ...
    cin >> word;
    }
    Whilst this is entirely correct - including checking the EOF immediately after the cin >> word, I feel that it would be better to use only one cin >> word, something like this:
    Code:
    ...
    while(cin >> word) 
    {
    ...
    }
    Neater, don't you think.

    So, changedword is your output is it? Where do you do anything to break the output line into 50 char lengths?

    Edit: And next time, can you post with code-tags, so the code looks a bit more decent?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    the function findconsonants, actually finds vowels, and returns the (length minus the vowels found)... the value that is returned includes consonants and punctuation marks, so i subtracted them.

    yeah that is neater.. thanks..

    i don't have anything to break up the output... i didn't know what to do with that...

    i was thinking of using an if stmt that checks the location 50 in the string to see if it is white space. now, if it isn't whitespace, i need to check the previous position until i find whitespace, and implement and end of line feed. is this possible?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    the function findconsonants, actually finds vowels, and returns the (length minus the vowels found)... the value that is returned includes consonants and punctuation marks, so i subtracted them.
    I think this function needs fixing then... A function that does not do what its name promises is not a good thing.

    i was thinking of using an if stmt that checks the location 50 in the string to see if it is white space. now, if it isn't whitespace, i need to check the previous position until i find whitespace, and implement and end of line feed. is this possible?
    If you have separate words, you might check first before adding it to the output line, if it would make the line longer than 50 characters (including the preceeding space). If it does, output the line and start with an empty one.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are two methods to break lines when you have a line of words:
    1. Find a space at or before position 50. Insert a newline at this point.
    2. See if adding the next word makes the string longer than fifty (lenght now + length current word > 50), then insert newline before new word.

    I think #2 works well here.

    So, if your functionc called "findconsonants()" actually finds vowels and subtracts that from the length, and you have another that counts punctuations, you should really subtract the vowels AND punctuations inside you findconsonants.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also, what if there are numbers? Or characters that don't fall into any category?

    I guess you might really implement the findConsonants properly. Or another idea: make a more generic function that accepts another argument: a string of characters to count in the input string. This way one and the same function could handle vowels, consonants, punctuation, numbers and what not at the same time.

    Speaking of parameters, if you pass a string object to a function, there is no need to pass the length as well: the length of the string is always available and size() or length() are cheap functions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    thanks, i forgot to check for numbers, we have a short list of characters to worry about...

    thanks for all the input, i at least have some direction now. i'll get back to you with an update.

    thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 4
    Last Post: 04-03-2007, 05:57 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM