BASIC but pleaseeeee C++ HELP due tomorrow!!

May 13, 2010 at 18:13:34
Specs: XP
okay ive been cracking my brain trying to figure out how to do this program.i have tried and tried...but heres my assignment.We have to display a text file,then ask end user to select a word they would like to insert into the file.After that,they enter the word (in the text) they would like to insert this desired word after.So right now all my program does is display the text and i am losssstt.the program was due yesterday but she gives us two days to turn it in late..pleaseee help me!! i am so lost..alittle code would you>? thanks

See More: BASIC but pleaseeeee C++ HELP due tomorrow!!

Report •

May 14, 2010 at 02:16:13
If you present another persons work as your own, your tutor may think that you actually understand the assignment and move you onto something more difficult which again you cannot do. It's unfair to you for anyone to do your work for you, if you don't understand the requirements front up and ask your tutor for assistance, that's what you're given assignments for, to test your capability and understanding.

Good luck.

Report •

May 14, 2010 at 09:39:43
If it's plain text and the file is fairly small, it *might* be easier to create an array of strings and read the file into the array, Prompt for your string to insert and then search your array for the word to insert after. Lastly, write out the first part of the array, the word to insert, and then the rest of the array.

Use the fstream object... when you read the file, it will set the end of file bit, so you'll need to reset it before you can rewrite its contents.

fstream fileobj;"some file name", ios::in | ios::out);

// read and output the file; request words

fileobj.seekg(0, ios::beg);

// write beginning of array, word, end of array

Now if the file is large, it's *probably* better to create a temp file and two file stream objects - ifstream (read existing) and ofstream (write temp).

It's pretty much the same as before... read the input file, rewind its stream, ask for the word to insert and where, and then as you re-read the input file, output its data to the temp file instead of the screen up until the selected word is reached, then the word, the rest of the input file.

Lastly, you can use two standard C functions remove() and rename() to delete the input file and rename the temp to that of the input file. Both require <cstdio> or stdio.h and take C style strings as file names, so if you're using the string class, remember to use string_obj.c_str()

Obviously, if your input file has multiple occurances of the selected word, you'll have to account for that; similarly, if case is an issue, then you can use isupper/islower or similar.


Report •

May 14, 2010 at 12:52:00
so nobodys gonna help me?? i understand what your saying Wahine, but ive been working on this project for 6 days,yes 6, and i still cant get it working.If my tutor was of help, I wouldnt been on here and to further,the only way I've ever been able to learn is by looking at code not written by me and from there teaching myself and making sense of it. Anything would be valuable right outline..anything.. im at risk of failing this class

Report •

Related Solutions

May 14, 2010 at 12:55:09
and shutat... our teacher told us specifically not to use arrays,read the file in as a string..and get each line and from know complete the project and the txt file is nothing but a small paragraph

okay...what i really dont understand is how im going to find the index of the word from the paragraph and then insert the desired word after it./..what would the syntax look like?

Report •

May 14, 2010 at 18:58:31
I'd just create a temp file - I don't know how you'd be able to insert a word without having a way to compare it to something.

fstream infile("input.txt", ios::in | ios::out);
int ch;

while((ch = infile.peek()) != EOF) {
   infile >> file_buffer;
   if(file_buffer == insert_after_word) {
      temp << file_buffer << " " << word_to_insert << " ";
   } else {
      temp << file_buffer << " ";

/* clear the input eof bit and reset the cursor
    to beginning for writing; clear the temp file
    eof bit and reset the cursor for reading */

infile.seekp(0, ios::beg);
temp.seekg(0, ios::beg);

while((ch = temp.peek()) != EOF) {
   getline(temp, file_buffer);
   infile << file_buffer;


remove("temp file name");

Something like that; it doesn't check case nor does it deal with punct chars at the end of words. Breaking the file around whitespace would be a bit eaiser to test than reading an entire line and parsing it for the target insertion word.

Hope that helps.

Report •

May 15, 2010 at 07:49:15
thank you so much! im gonna try this.ill be able to handel the desk checking, thanks alot!

Report •

May 15, 2010 at 09:53:55
By the way, if you are reading the file into a std::string object,
you can use the find() method to search the string read from the
file for the substring containing the word in the file entered by the

Report •

May 15, 2010 at 11:51:08
thanks klint

Report •

Ask Question