Quantcast

Maximum PC

It is currently Thu Apr 17, 2014 12:48 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Need Help On This C++ Probem..My Code Won't Work :(
PostPosted: Fri Oct 29, 2004 1:10 pm 
Coppermine
Coppermine
User avatar

Joined: Sat Jul 03, 2004 10:56 am
Posts: 709
Here's the details of what the program is suppose to do and my code is at the bottom. I have no clue on why it will not work. I'm hoping one of you C++ experts can look through it and tell me.



The purpose of this assignment is to create a program that works like a simple html file converter. Your program will read a text file and will produce a html file that can be viewed using a web browser, like Netscape Communicator, Microsoft Internet Explorer, or Apple Safari.

Write a C++ code that converts an input file into a simple Hyper Text Markup Language (HTML) file. In order to do this, you will read the input file from the beginning to the end and will create the output file by following the instructions given below. Note that the output file you create must have .html as its file extension.

1) Every time you run your code, the first thing your program will do is to write the following 5 lines at the beginning of the output file before you start reading the input file. This is a very minimum header for your html file. You will use a function called write_header to do this part of the program.

<Html>
<Title>
This is my C++ html converter
</Title>
<Body>

2) From this point on, you will read the contents of the input file, and will copy it to the output file. However, as the input file is read, you will write a "<br>" (without the ") into the output file each time a newline is reached. The <br> in html means the same thing as the "\n" in C++. Continue reading and copying the rest of the file. We will assume that any time you encounter 2 dashes, the author intends to use italics until the next "\n" is encountered. Place the html style tag <i> and </i> in your output file surrounding the text that is to be bold. (Hint: while there are many ways to do this, you might consider the "putback" function.) The <I> and </I> style tags tell the browser when to turn bold on and off. Finally, any time you encounter two newline characters in a row in the input file, draw a line to separate the section of text. Do this by writing the "horizontal rule" html tag to the output file between 2 line break tags <br>. The horizontal rule tag is <hr>. Repeat this as needed until you are done reading the entire file.

For example, suppose your input file has the following lines of text:


Well, you wonder why I always dress in black,
why you never see bright colors on my back?
And why does my appearance seem to have a somber tone?

Well, there's a reason for the things that I have on.
I wear the black for the poor and the beaten down
living in the hopeless, hungry side of town.
I wear it for the prisoner who has long paid for his crime,
but is there because he's a victim of the times.

--Johnny Cash

As you have noticed, at the end of each line there is at least one invisible "\n". So, once you reached that, your program will write <br> into the output file. Thus, the first three lines in the output file will look like this:

Well, you wonder why I always dress in black,<br>
why you never see bright colors on my back?<br>
And why does my appearance seem to have a somber tone?<br><hr><br>

3) After reading the input file and copying its entire contents into the output file, you will write the following two lines at the end of the output file. You will use a function called write_footer to write these lines.

</Body>
</Html>

The output that you will generate after you run the program will be an html file. If you have done everything correctly, you can open it using a web browser like Netscape or Internet Explorer.

A sample input file:

Hi my name is David.
I wrote this program that uses the file I/O to create html files from text files.

Sample output:
<Html>
<Title>
This is my C++ html converter, David.
</Title>
<Body>

Hi my name is David. <br>
I wrote this program that uses the file I/O to create html files from text files. <br>

</Body>
</Html>

In addition to main, your program MUST use the following functions. Failure to follow these directions will result in a grade penalty.
void instructions( )
//Displays instructions on how to run the program and what the input is.

void get_test_files(ifstream& in_s, ofstream& out_s)
// This function asks users to enter the input and output file names from the keyboard. It will attempt to connect to the files (using "open"). Once successfully connected, it returns the input and output streams to the main function. If it fails, display an error message and ask to user to re-enter a correct file name.

void write_header(ofstream& out_s)
// This function will write the 5 lines of header into the output file.

void write_footer(ofstream& out_s)
// This function will write the last 2 lines, the footer of the html file.

void copy_text(ifstream& in_s, ofstream& out_s)
// This file copies the entire contents of the input file into the output file, line-by- line, by considering the given instruction for the newlines, italics, and section lines (horizontal rules).



What to turn in (stapled together):

Print out of your source program code (cpp file).
A printed copy of your flow chart for the copy_text function (only).
A printed copy of the input file you will use to test the program. It must have the contents listed in step 3 below.
Print out of the HTML output file that your program creates given an input file with the following contents:

Thinking back over my life, it seems to me that there are different ways of looking out and trying to understand the world around us. There's a very clear scientific window. And it does enable us to understand an awful lot about what's out there. There's another window, it's the window through which the wise men, the holy men, the masters, of the different and great religions look as they try to understand the meaning in the world.
--Jane Goodal

Superstition is to religion what astrology is to astronomy; the mad daughter of a wise mother.
--Voltaire

Science without religion is lame, religion without science is blind.
--Albert Einstein

There remains something subtle, intangible and inexplicable. Veneration for this force beyond anything that we can comprehend is my religion.
--Albert Einstein

My religion is simple, my religion is kindness.
--Dalai Llama

Note:

You can test your output file by opening it with a browser such as Internet Explorer. For example, using Internet Explorer, select the "Open" item from the "File" menu. Using "Browse", locate your output file.




Code:
# include <iostream>
# include <fstream>
using namespace std;

void instructions(); //declaring functions
void get_test_files(ifstream& in_s, ofstream& out_s);
void write_header(ofstream& out_s);
void write_footer(ofstream& out_s);
void copy_text(ifstream& in_s, ofstream& out_s);

int main (void)
{
   ifstream input; //calling functions
   ofstream output;
   instructions();
   get_test_files(input, output);
   write_header(output);
   copy_text(input, output);
   write_footer(output);
   input.close();
   output.close();
   return 0;
}

void instructions() //defining instructions fn
{
   cout<<"This is Megan's HTML converter. \n\n";
   cout<<"When asked please enter the input and output file names. \n";
}

void get_test_files(ifstream& in_s, ofstream& out_s) //defining get_test_files
{
   char testin[30],testout[30];
   cout<<"Enter name of input file.";
   cin>>testin; //assigning user's file to testin
   cout<<"Enter name of output file.";
   cin>>testout; //assigning user's file to testout
   in_s.open(testin);
   if(in_s.fail()) //testing to see if input file exists
   {
      cout<< "Input file opening failed.";
      exit(1);
     }
   out_s.open(testout); //testing to see if output file exists
   if(out_s.fail())
   {
      cout<< "Output file opening failed.";
      exit(1);
   }
}

void write_header(ofstream& out_s)
{
   out_s<<"<Html>\n"; //sending headers to output file
   out_s<<"<Title>\n";
   out_s<<"This is my HTML Converter";
   out_s<<"</Title>";
   out_s<<"<Body>";
}

void write_footer(ofstream& out_s)
{
   out_s<<"</Body>"; //sending footers to output file
   out_s<<"</HTML>";
}

void copy_text(ifstream& in_s, ofstream& out_s)
{
      while(!in_s.eof());
   {
      char x;
      in_s.get(x);

   if (x=='\n') // when x equals \n in the input file
      out_s<< "<br>"; //output <br> to output file
   else
      out_s<<x;

   if (x=='-')// when x equals -- in the input file
      out_s<< "<I>";
   else
      out_s<<x;

   if (x== '\n')// when x equals \n\n in the input file
      out_s<< "<hr>";
   else out_s<<x;
   }
}





There it is...Thanks all for any help.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Oct 29, 2004 8:56 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Code:
void copy_text(ifstream& in_s, ofstream& out_s)
{
      while(!in_s.eof());
   {
      char x;
      in_s.get(x);

   if (x=='\n') // when x equals \n in the input file
      out_s<< "<br>"; //output <br> to output file
   else
      out_s<<x;

   if (x=='-')// when x equals -- in the input file
      out_s<< "<I>";
   else
      out_s<<x;

   if (x== '\n')// when x equals \n\n in the input file
      out_s<< "<hr>";
   else out_s<<x;
   }
}


Hey Megan,

Looks like you might have one to many semi-colons in this block of code. Question, what does the following code do....


Code:
//print out a sequence of integers
cout << "enter a number" << endl;
int n;
cin >> n;

int i = 0;
for (; i < 11; i++);
    cout << (n+i) << endl;


...and what do you think I intended.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Oct 30, 2004 9:24 am 
Coppermine
Coppermine
User avatar

Joined: Sat Jul 03, 2004 10:56 am
Posts: 709
:idea: How did you know that was Megan's code? I posted it for her on this forum since nobody could figure out why her code doesn't work....

Do you know her?


Top
  Profile  
 
 Post subject:
PostPosted: Sat Oct 30, 2004 10:09 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Everyone knows Megan.

Hell, even I knew that was Megan's code.

(I'm not kidding about the second part.)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Oct 30, 2004 5:40 pm 
Coppermine
Coppermine
User avatar

Joined: Sat Jul 03, 2004 10:56 am
Posts: 709
I'm assuming Gadget must be a friend of Megan's and she gave the link to him after I gave her the link.

But it's weird since Gadget is not a new member because of his post count. I didn't think I'd find somebody whose a member of this forum that knows somebody I knows....strange


Top
  Profile  
 
 Post subject:
PostPosted: Sat Oct 30, 2004 5:56 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Gigahertz17 wrote:
I'm assuming Gadget must be a friend of Megan's and she gave the link to him after I gave her the link.

But it's weird since Gadget is not a new member because of his post count. I didn't think I'd find somebody whose a member of this forum that knows somebody I knows....strange

Small world... :)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Oct 30, 2004 5:56 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Jipstyle wrote:
Everyone knows Megan.

Hell, even I knew that was Megan's code.

(I'm not kidding about the second part.)

We should apply for jobs in the symphony...


Top
  Profile  
 
 Post subject:
PostPosted: Sat Oct 30, 2004 9:36 pm 
Coppermine
Coppermine
User avatar

Joined: Sat Jul 03, 2004 10:56 am
Posts: 709
Do you go to Penn State?


Top
  Profile  
 
 Post subject:
PostPosted: Sun Oct 31, 2004 5:03 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Gigahertz17 wrote:
Do you go to Penn State?

Nope, but I do play the fiddle. Wanna hear?

Code:
void instructions() //defining instructions fn
{
   cout<<"This is Megan's HTML converter. \n\n";

   cout<<"When asked please enter the input and output file names. \n";
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Oct 31, 2004 6:18 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Damn it, Gadg ... we could have kept this up for a while longer. ;)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 01, 2004 1:22 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Jipstyle wrote:
Damn it, Gadg ... we could have kept this up for a while longer. ;)

I was starting to feel bad... 8)

It was just too damn easy. I was playing violin - how about you?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 01, 2004 7:29 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
I was giggling like a schoolgirl.

Err .. maybe I shouldn't admit that. :P


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group