Hi,

Im a beginning C++ student and having a very tuff time, but I have been trying for a few hours, and wanted come to you guys with my questions.

I have been working on this school project and have figured out every step but two.

Can anyone please help me.

Write a program that will read in an unknown number of words(of diff lengths) from a file words.txt program must do:

1.REad and display the word and move to tabs to the right.
2. insert "***" in the middle of the word
3. display the changed word and move two tabs to the right.
4. If the changed word is more than 8 characters long remove the characters that are past the 8th place, so it is exactly 8 characters. If the word is less that 8 characters, add enough letter "B"'s to the end so that it is exactly 8 characters long.
5. Display the changed word and move to tabs to the right.
6.Change the next to last character to a # sign and then put an ! point at the end of the word.
7.Display changed word.
8.Do this for all words read in.
9.Skip one line and display a count of all words read in.

So far I have everything done, and my stumbles are on #4 to add as many "B"'s necessary to the words that are less than 8 characters so they become exactly 8. Also on #6 I can add the explanation point to the end of the words that are longer that 8 characters but not to the ones that are shorter. When I add it into the program and starts to compile then stops. This is what I have so far.

Thanks, For the help all!

string word;
int count = 0;
int middle;
char fill;
char letter = 'B';

if (!infile)
    {
       cout << "An error has occurred while opening file" << endl;
       exit (1);
    } 
     while (!infile.eof())
    {

       infile >> word;
       cout << word << "\t\t";
       //print << word << endl;
       middle = (word.length()/2);
       word.insert(middle,"***");
       cout << word << "\t\t";

            if (word.length()>8)
            {
            word.erase (8,20);
            cout << word << "\t\t";
            word.replace(6,1, "#"),word.insert(8,"!");
            cout << word << endl;
            }
            else if (word.length()<=8)
            {
            word.insert (8 - word.length(),"B");
            cout << word << "\t\t";
            word.replace(6,1,"#");
            cout << word << endl;
            }
            count ++;

  }
cout << count << endl;

Recommended Answers

All 3 Replies

use substring.

>I have been working on this school project and have figured out every step but two.
>
>So far I have everything done, and my stumbles are on #4
>
>Also on #6 I can add the explanation point to the end of the words that
>are longer that 8 characters but not to the ones that are shorter.
I'm seeing slight contradictions here.

>word.erase (8,20);
Where does 20 come into the picture? The second argument defaults to npos, so you can just say word.erase ( 8 ) and the string class will do the right thing.

>word.replace(6,1, "#"),word.insert(8,"!");
Um, overkill? Why not just say word[6] = '#'; ? And append would be a better choice than insert here. Of course, since this is the last operation for the line, you can just print a bang character and call it good without modifying the string. Also, this is an extremely poor use of the comma operator. Separate operations should be on separate lines:

word.replace(6,1, "#");
word.insert(8,"!");

Also, you only append the bang in this condition, but the requirements seem to want it all of the time.

>else if (word.length()<=8)
If the word is exactly eight characters, you don't need to append with 'B's. Your logic is broken here, but the body of the condition hides it from you.

>word.insert (8 - word.length(),"B");
This doesn't do what you think it does. It inserts the string "B" at the location 8 - word.length() . It doesn't append 8 - word.length() occurrences of the string "B". To do that you need:

word.append ( 8 - word.size(), 'B' );

Compare and contrast:

#include <iostream>
#include <string>

int main()
{
  std::string word;

  while ( std::cin>> word ) {
    std::cout<< word <<"\t\t";

    // 2. insert "***" in the middle of the word
    std::cout<< word.insert ( word.size() / 2, "***" ) <<"\t\t";

    if ( word.size() > 8 ) {
      // 4a. If the changed word is more than 8 characters long remove the
      //     characters that are past the 8th place, so it is exactly 8 characters.
      word.erase ( 8 );
    }
    else if ( word.size() < 8 ) {
      // 4b. If the word is less that 8 characters, add enough letter "B"'s to the
      //     end so that it is exactly 8 characters long.
      word.append ( 8 - word.size(), 'B' );
    }

    std::cout<< word <<"\t\t";

    // 6.Change the next to last character to a # sign and
    //   then put an ! point at the end of the word.
    word[6] = '#';

    std::cout<< word <<"!\n";
  }
}
commented: お疲れ様でした。 +8
commented: What does that mean wolfpack? the chinese letters? +3
commented: Those are Japanese. +6

Narue,

Thank You very much for your help, I ended up getting it working with your help.

Thanks Alot for the Help
Radskate360

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.