944,028 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4368
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 7th, 2004
2

needed big time hw due and late

Expand Post »
: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
Reputation Points: 12
Solved Threads: 0
Newbie Poster
damionspencer is offline Offline
3 posts
since Sep 2004
Sep 8th, 2004
0

Re: needed big time hw due and late

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....
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Sep 8th, 2004
2

Re: needed big time hw due and late

I think your question is about c++ ... so it should be posted in c++ forum ... you'll get replies there.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Sep 8th, 2004
0

Re: needed big time hw due and late

Quote originally posted by Chainsaw ...
I imagine it complains about these lines:

using std::cout;
using std::cin;
Why?

Quote originally posted by Chainsaw ...
try just "using std;" instead.
ITYM
  1. 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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 8th, 2004
0

Re: needed big time hw due and late

Quote originally posted by Chainsaw ...
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
Reputation Points: 12
Solved Threads: 0
Newbie Poster
damionspencer is offline Offline
3 posts
since Sep 2004
Sep 11th, 2004
2

Re: needed big time hw due and late

Unless you HAVE TO use specific code, this works GR8!
(Sorry if its a little to late!)
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5. char name[50];
  6. char item[50];
  7. char exit[10];
  8. printf("What is your name?\n\a");
  9. gets(name);
  10. printf("What is one thing you REALLY want?\n\a");
  11. gets(item);
  12. printf("Congratulations, %s! You just won %s!\nTo collect, please send 100$ to California.\n(Press enter to exit)",name,item);
  13. gets(exit);
  14. return 0;
  15. }

Attached is the compiled exe in a zip file.
Attached Files
File Type: zip hw.zip (27.4 KB, 20 views)
Reputation Points: 12
Solved Threads: 0
Light Poster
mister-fett is offline Offline
48 posts
since Aug 2004
Sep 11th, 2004
0

Re: needed big time hw due and late

Quote originally posted by mister-fett ...
Unless you HAVE TO use specific code, this works GR8!
:rolleyesrogram for more than a couple days before you start teaching.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 12th, 2004
1

Re: needed big time hw due and late

Quote originally posted by Dave Sinkula ...
:rolleyesrogram 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/.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Sep 12th, 2004
0

Re: needed big time hw due and late

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!

Quote ...
Program for more than a couple days before you start teaching.
Reputation Points: 12
Solved Threads: 0
Light Poster
mister-fett is offline Offline
48 posts
since Aug 2004
Sep 12th, 2004
0

Re: needed big time hw due and late

Quote originally posted by alc6379 ...
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);
Quote originally posted by mister-fett ...
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.

Quote originally posted by mister-fett ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: string size problem
Next Thread in C Forum Timeline: A Little Advice Please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC