:mad: :mad: :( :( I've been trying to write this program that is suppose to read in a text file manipulate in and give the following out put infact here is the question

Implement a program that uses a form-letter template to generate form letters. Your program should read in a form-letter template from a file. The form-letter template has "holes" in it that are to be filled in with user-entered values. The holes are represented by a pair of @ signs. For example, suppose this is read in from a form-letter template file:

Congratulations, @@! You've just won @@!
To collect, please send $100 to

And suppose the user enters these two values for the two @@ holes:

Raheel Azhar
a new house

Your program should then print this form letter:

Congratulations, Raheel Azhar! You've just won a new house!
To collect, please send $100 to
:mad:

this is what i have so far but all i get is compilation errors

#include <iostream>
#include <string>


using std::cout;
using std::cin;


class FormLetter
{
public:
string letterTemplate;


//private:
void generateLetter();
};


FormLetter::FormLetter(string fname)
{


letterTemplate = fname;


f.open("letterTemplate", ios::in);
f.close();


}


void generateLetter()
{
string input;
int count = 0;
++count;
while(count <4)
{
getline(letterTemplate,'@');
cout << "Enter insertion text: " << endl;
cin >> input << endl;
letterTemplate.insert(find_first_of '@', input);
generateLetter();
cout << letterTemplate << endl;


}
}


int main()
{
FormLetter formLetter1("letterTemplate.txt");
formLetter1.generateLetter();
return 0;
}

please help

Dani AI

Generated

A concise, practical fix for the form‑letter task (builds on points raised by , , and ).

The original submission mixes a number of problems: incorrect use of getline (it was called with wrong parameters), misuse of string::insert/find_first_of, an in-class constructor/method not declared then defined correctly, an accidental recursive call to generateLetter, and opening a file without the proper stream object. ’s C variant demonstrates the behavior wanted, but uses gets which is unsafe (as noted). A small, robust C++ pattern avoids all of these issues.

Recommended approach: read the entire template into a single std::string (use std::ifstream + std::ostringstream), then loop: find the next "@@" with find, prompt for replacement with std::getline(std::cin, ...), and call replace(pos, 2, input). Continue searching from pos + input.size() until no more placeholders. Do not recurse for replacements; use a simple loop. Optionally count placeholders before prompting (with repeated find calls) so prompts can be labeled or to handle mismatches cleanly. Always check that the file opened successfully.

Example (standalone program):

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main() {
    std::ifstream in("letterTemplate.txt");
    if (!in) { std::cerr << "Cannot open template file\n"; return 1; }
    std::ostringstream buf;
    buf << in.rdbuf();
    std::string tmpl = buf.str();

    std::size_t pos = tmpl.find("@@");
    while (pos != std::string::npos) {
        std::cout << "Enter insertion text: ";
        std::string value;
        std::getline(std::cin, value);
        tmpl.replace(pos, 2, value);
        pos = tmpl.find("@@", pos + value.size());
    }

    std::cout << "\nFinal letter:\n" << tmpl << '\n';
    return 0;
}

Notes: in a class version, declare FormLetter(const std::string& filename); in the class and implement FormLetter::generateLetter() as above. Decide how literal "@@" should be escaped in templates (preprocess an escape like \@@ if needed).

Recommended Answers

All 12 Replies

What kinds of compile errors?

I imagine it complains about these lines:

using std::cout;
using std::cin;

try just "using std;" instead.

and you make a constructor like this:

FormLetter::FormLetter(string fname)

but it isn't in the class definition; try adding "FormLetter(string fname);" after "public:" in the FormLetter class.

void generateLetter()

needs to be like this:

void FormLetter::generateLetter()

and so on; the compiler should be pointing out the line number that has a problem and a general idea of what the problem is....

I think your question is about c++ ... so it should be posted in c++ forum ... you'll get replies there.

I imagine it complains about these lines:

using std::cout;
using std::cin;

Why?

try just "using std;" instead.

ITYM

using namespace std;

Except in 'toy' programs, you really don't want to grab the whole namespace -- doesn't that kind of defeat the purpose of the namespace?

What kinds of compile errors?

I imagine it complains about these lines:

using std::cout;
using std::cin;

try just "using std;" instead.

and you make a constructor like this:

FormLetter::FormLetter(string fname)

but it isn't in the class definition; try adding "FormLetter(string fname);" after "public:" in the FormLetter class.

void generateLetter()

needs to be like this:

void FormLetter::generateLetter()

and so on; the compiler should be pointing out the line number that has a problem and a general idea of what the problem is....

thanks for the reply I will try this I am more familiar with java
so thats why I am having a few headaches

i am also a new member to this site
so I appreciate any help i can get

Unless you HAVE TO use specific code, this works GR8!
(Sorry if its a little to late!) :cool:

#include <stdio.h>
#include <string.h>
int main()
{
	char name[50];
	char item[50];
	char exit[10];
	printf("What is your name?\n\a");
	gets(name);
	printf("What is one thing you REALLY want?\n\a");
	gets(item);
	printf("Congratulations, %s! You just won %s!\nTo collect, please send 100$ to California.\n(Press enter to exit)",name,item);
	gets(exit);
	return 0;
}

Attached is the compiled exe in a zip file.

Unless you HAVE TO use specific code, this works GR8!

:rolleyes:Program for more than a couple days before you start teaching.

:rolleyes:Program for more than a couple days before you start teaching.

Hey now. Be nice.

If there's something wrong with what he's posted, wouldn't it be more helpful to point out what's the problem/.

Come on, I was just trying to help. I have been programming in various languages for about three years, I am just new to C (I have been learning it for a month, but I got the compiler a week ago. I have known Dr Scheme for about nine months, PBasic for three years, and have used National Instruments Labview for two years.)
Besides, it works, doesn't it?
I know there is a better way to do it, but the way I described and compiled works.

I do understand, though, that my way does not fit the parameters set by the assignment. I will read more carefully next time. Although it completes the task, my program does not use files or forms. Sorry!

Program for more than a couple days before you start teaching.

Hey now. Be nice.

If there's something wrong with what he's posted, wouldn't it be more helpful to point out what's the problem/.

There is one function that should be avoided at all costs -- gets. Millions of Google hits can tell you why. But I can provide links if you'd like. Any program that contains one of these constructs stinks of a C newbie:


  • void main()
  • gets(string);
  • fflush(stdin);

Unless you HAVE TO use specific code, this works GR8!
(Sorry if its a little to late!)

#include <stdio.h>
     #include <string.h>
     int main()
     {
     	char name[50];
     	char item[50];
     	char exit[10];
     	printf("What is your name?\n\a");
     	gets(name);
     	printf("What is one thing you REALLY want?\n\a");
     	gets(item);
 	printf("Congratulations, %s! You just won %s!\nTo collect, please send 100$ to California.\n(Press enter to exit)",name,item);
     	gets(exit);
     	return 0;
 }

The original code was C++, for which different methods are used for input and output. And this code uses the most dangerous function in the C standard library as an "improvement". And the last one also uses a reserved identifier -- the name of a standard library function.

Besides, it works, doesn't it?
I know there is a better way to do it, but the way I described and compiled works.

That is the most dangerous way to code: it "works". (By accident, not by design.)

It is never safe, but it "works".

Sometimes the only way to capture someone's attention is to be a little bit of an ass. Try not to take it personally. Try to take it as constructive criticism without wiggle room.

You got my attention. Dont worry, I wont be posting anything that implies my knowledge of anything for a few months now, except maybe for DrScheme. Thanks! :mrgreen:

edit: BTW, what should I use instead? :?:

instead of

gets(whatever string/char?);

What command should I use?
(I forgot to be specific in previous reply, and I already used my edit on it.)

instead of

gets(whatever string/char?);

What command should I use?
(I forgot to be specific in previous reply, and I already used my edit on it.)

Generally, fgets. But you will notice that this may leave an unwanted newline in the input string -- there are a number of ways to remove it.

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.