Write a c++ program that will read in, from a file entitled "word.txt", an unknown
number of single words (no spaces ). The program will then print out the
word, move 2 tabs to the right, display the word with the first letter
removed, move 2 tabs to the right, display the adjusted word with a
percent sign ( % ) inserted between the 3rd and 4th characters, move 2
tabs to the right, and finally display the new adjusted word with a pound
sign ( #) replacing the final letter of the word. Look at my code below and tell me what i'm doing wrong.
Output is to both the screen and the printer.

Example of output:
backups ackups ack%ups ack%up#

My infile is:

rumpler
harmony
gidgets
fromage
squares
pancake
lexicon
piglets
singles
program
printer
happily
offices
windows
express
poisons

The code that I have thus far is:

#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;

	infile.open ("word.txt");
	outfile.open ("wordOutput");

	string phrase;

	while (getline(infile,phrase,'\n'))
	{
		phrase.erase(0,1)
			cout<<phrase<<endl;

		phrase.insert(4,"%");
			cout<<phrase<<endl;

		phrase.replace(7,"#")
			cout<<phrase<<endl;
	}
		return 0;
}

Recommended Answers

All 8 Replies

>>Look at my code below and tell me what i'm doing wrong.
the code you posted contains some syntax errors and missing semicolons.

with regards to the syntax errors, how could manipulate the same word without re-declaring(if there is such a word) it as another string?

I don't know what you mean. the replace method does not have the correct number of parameters. See these examples, code snipped below

// replace characters of tooth string with num (here length) 
  // characters from teeth string, beginning at index 21
  string tooth = "He ate it all with a toothpick";
  cout << tooth << endl;
  string teeth = "teethpick";
  tooth.replace( 21, teeth.length(), teeth );
  cout << tooth << endl << endl;
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;

	infile.open ("word.txt");
	outfile.open ("wordOutput");

	string phrase = "lover";

	
		phrase.erase(0,1);
			cout<<phrase<<endl;

		phrase.insert(4,"%");
			cout<<phrase<<endl;

		phrase.replace(4,"%");
		phrase.replace(7,8,"#");
			cout<<phrase<<endl;
	
		return 0;
}

Thanks for the link Ancient Dragon. Above is the modified code. Instead of making it read from the infile, i wanted to test the prgram first, then deal witha any infile problems later. The error message:

1>------ Build started: Project: wmanip, Configuration: Debug Win32 ------
1>Compiling...
1>wmanip.cpp
1>c:\users\brown\documents\visual studio 2005\projects\wmanip\wmanip\wmanip.cpp(32) : error C2661: 'std::basic_string<_Elem,_Traits,_Ax>::replace' : no overloaded function takes 2 arguments
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>Build log was saved at "file://c:\Users\Brown\Documents\Visual Studio 2005\Projects\wmanip\wmanip\Debug\BuildLog.htm"
1>wmanip - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thanks for your input.

Look here and try to find an overload of replace that takes two arguments.

Thanks for the link. If i understand correctly, it shoukd look something like this:

#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;

	infile.open ("word.txt");
	outfile.open ("wordOutput");

	string phrase = "lover";

	
		phrase.erase(0,1);
			cout<<phrase<<endl;

		phrase.insert(4,"%");
			cout<<phrase<<endl;

		phrase.replace(4,"%",7,8,"#");
			cout<<phrase<<endl;
	
		return 0;
}

I get the following error message:

1>------ Build started: Project: wmanip, Configuration: Debug Win32 ------
1>Compiling...
1>wmanip.cpp
1>c:\users\documents\visual studio 2005\projects\wmanip\wmanip\wmanip.cpp(32) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::replace(__w64 unsigned int,__w64 unsigned int,const std::basic_string<_Elem,_Traits,_Ax> &,__w64 unsigned int,__w64 unsigned int)' : cannot convert parameter 2 from 'const char [2]' to '__w64 unsigned int'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        There is no context in which this conversion is possible
1>Build log was saved at "file://c:\Users\Documents\Visual Studio 2005\Projects\wmanip\wmanip\Debug\BuildLog.htm"
1>wmanip - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

You're not really paying attention, are you? The first two arguments to replace are always going to be the offset and the count, or a beginning and ending iterator. Note that none of those options is a string or string literal. The site I linked to gives you the declaration for every overload of every function in the string class (including types), so you shouldn't be getting these errors.

Thanks you guys, problem solved. Time to go on to my other threads. see you around.

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.