Please comment on my program (Simple TXT Writer)
Author |
Message |
Bo0sT
![](http://compsci.ca/v3/uploads/user_avatars/86196945245bc23817a7f1.jpg)
|
Posted: Sun Sep 07, 2008 1:40 am Post subject: Please comment on my program (Simple TXT Writer) |
|
|
I wrote this program so that I can post it and learn from feedback that others give me. If you could please look through my program and maybe point out something that I could have done better and/or how to fix any of all the bugs I have posted in the top comments that would be cool, thanks. I commented it well so it is easy to look through, feel free to put it into your own IDE and compile it and test it, i only used standard C++ libraries so it should run on any IDE.
code: |
/*
* Simple TXT Writer, by Danny Knight
* Saturday, September 6, 2008
*
* This program was made as practice with
* many basic and standard C++ concepts
* and libraries. I commented everything
* in great detail so that others can
* learn from it.
*
* Bugs ----------------------------------
* -when writing to a file, after you have
* entered a line, you cannot go back and
* edit the line
* -cannot enter a file name greater than
* one word long
* -after adding to a text file, the new
* and old data are seperated by a blank
* line (I did not do this on purpose, I
* just couldn't figure out a quick and
* easy way to fix it)
*/
#include <iostream> // Standard C++ input / output stream
#include <string> // C++ string type
#include <fstream> // Standard C++ file stream
using namespace std; // Using standard namespace for standard C++ libraries
static void create_file(void); // Is run when the 'Create TXT' option is selected
static void open_file(void); // Is run when the 'Open TXT' option is selected
void cannot_open_file(char* title_of_file); // Displays an error message including the name of the file as an ARRAY OF CHARs to the standard output stream and exits the program returning '1'
void cannot_open_file(string title_of_file); // Displays an error message including the name of the file as a STRING to the standard output stream and exits the program returning '1'
void cannot_open_file(); // Displays an error message to the standard output stream and exits the program returning '1'
void main()
{
char option = ' '; // char being used to store the user's menu option
while (!(option == 'e' || option == 'E'))
{
cout << "Welcome to Simple Text Reader" << endl;
cout << "=============================" << endl;
cout << "A - Create TXT\nB - Open TXT\nE - Exit" << endl;
// Checks for valid input
for (;;)
{
cin >> option; // get value of option from keyboard
if (option == 'A' || option == 'a') // if option a is selected, leave a line, run option a's function and break the forever loop
{
create_file();
break;
}
else if (option == 'B' || option == 'b') // if option b is selected run option b's function and break the forever loop
{
open_file();
break;
} else if (option == 'E' || option == 'e')
exit(0);
else cout << endl <<"please enter an 'a' or a 'b' or an 'e'" << endl; // Otherwise display error message and continue looping until the user had entered valid input
}
system("cls"); // Clear the screen before the Main Menu shows
}
}
static void create_file() // Option a's function
{
string file_name; // Name of the file
string info; // Contents of the file
bool has_ext = false; // Weather or not the user put a .txt at the end of 'file_name'
ofstream output_file; // Object of the output file
system("cls"); // Clear the output window
cout << "Name your file: "; // prompt the user to name the file, initilizing 'file_name'
cin >> file_name;
for (int i = 0; i < file_name.length();i++) // run the loop up to as many times as there are letters in 'file_name'
{
if ((file_name[i] == '.') && (file_name[++i] == 't' || file_name[++i] == 'T') &&
(file_name[++i] == 'x' || file_name[++i] == 'X') &&
(file_name[++i] == 't' || file_name[++i] == 'T') && file_name[++i] == '\0') // if the user did put a .txt at the end of 'file_name', set has_ext to true and break the loop
{ has_ext = true; break; } else; // Otherwise keep looping until the end of 'file_name', (if there is no .txt, has_ext remains as false, its initial value)
}
// if has_ext is false, meaning the user did not put a .txt at the end of 'file_name', put .TXT at the end of it for them
if (has_ext == false)
file_name = file_name + ".TXT"; else; // Otherwise do nothing and continue with the program
output_file.open(file_name.c_str()); // Create the output file by calling the 'open' function on the 'ofstream' object, and pass in an object of string, casted to a primative C string using the function c_str because open doens't take C++ 'string's
if (!(output_file.is_open())) // If the file is unable to open, run the 'cannot_open_file' function
cannot_open_file(file_name); else; // Otherwise do nothing and continue with the program
// The following two lines are written to the text document created
output_file << "This document is created in Simple Text Reader" << endl;
output_file << "==============================================" << endl;
cout << "Writing to text file " << file_name << " (type $save to save)" << endl; // Prompt the user to write to the file, entering "$save" to save their work
while(!(info == "$save")) // Loop until "$save" is entered
{
getline(cin,info); // Gets a line of text from the 'cin' function and reads it into the 'info' object
if (info != "$save") // Only write 'info' to file if it is not "$save" (this condition is another way of writing the condition in the while loop)
output_file << info << endl;
}
output_file.close(); // Close output file
}
static void open_file() // option b's function
{
string file_name; // Name of the file
string info[30]; // Contents of the file
string new_info; // A string that contains the appended contents of a file if the user selects 'Edit'
bool has_ext = false; // Weather or not the user put a .txt at the end of 'file_name'
ifstream input_file; // Object of the input file
ofstream add_to_file; // This objcet opens a file as output, so that we can add the old data as well as the new stuff
char option; // Add to or Man Menu
system("cls"); // Clear the output window
cout << "Enter the name of the file to open" << endl;
cin >> file_name;
for (int i = 0; i < file_name.length();i++) // run the loop up to as many times as there are letters in 'file_name'
{
if ((file_name[i] == '.') && (file_name[++i] == 't' || file_name[++i] == 'T') &&
(file_name[++i] == 'x' || file_name[++i] == 'X') &&
(file_name[++i] == 't' || file_name[++i] == 'T') && file_name[++i] == '\0') // if the user did put a .txt at the end of 'file_name', set has_ext to true and break the loop
{ has_ext = true; break; } else; // Otherwise keep looping until the end of 'file_name', (if there is no .txt, has_ext remains as false, its initial value)
}
// if has_ext is false, meaning the user did not put a .txt at the end of 'file_name', put .TXT at the end of it for them
if (has_ext == false)
file_name = file_name + ".TXT"; else; // Otherwise do nothing and continue with the program
input_file.open(file_name.c_str());
if (!(input_file.is_open())) // If the file is unable to open, run the 'cannot_open_file' function
cannot_open_file(file_name);
else cout << "\nViewing " << file_name << "...\n" << endl; // Otherwise tell the user what file they are viewing
for (int x = 0;!(input_file.eof());x++) // Function 'eof' returns true when the file is out of data (eof stands for "end of file")
{
getline(input_file,info[x]);
cout << info[x] << endl;
}
input_file.close();
cout << "===================" << endl;
cout << "A - Add to this TXT\nB - Main Menu" << endl;
cout << "===================" << endl;
for (;;)
{
cin >> option;
switch(option)
{
case 'b':
case 'B':
return; // Ends function, returning to main
case 'a':
case 'A':
{
system("cls");
add_to_file.open(file_name.c_str());
if (!(add_to_file.is_open()))
{ cout << "Unable to open file for output" << endl; exit(1); }
for (int c = 0;c < x;c++) // The for loop incrementor is how the language got it's name, because it is an "incrementation" of the language C..
{
cout << info[c] << endl;
add_to_file << info[c] << endl;
}
while (new_info != "$save") // Another way of writing (!(new_info == "$save"))
{
getline(cin,new_info);
if (!(new_info == "$save"))
add_to_file << new_info << endl; else;
}
return;
}
}
}
}
void cannot_open_file(char* name) // See prototype
{
cout << "Unable to open file: " << name << endl;
exit(1);
}
void cannot_open_file(string name) // See prototype
{
cout << "Unable to open file: " << name << endl;
exit(1);
}
void cannot_open_file() // See prototype
{
cout << "Unable to open file" << endl;
exit(1);
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Bo0sT
![](http://compsci.ca/v3/uploads/user_avatars/86196945245bc23817a7f1.jpg)
|
Posted: Sun Sep 07, 2008 2:06 am Post subject: Re: Please comment on my program (Simple TXT Writer) |
|
|
I just tested this on Dev-C++, it gives an error message because I used a variable declared in a for loop outside of the loop's block. I wrote this in Microsoft Visual C++ v6.0 and when I was writing it I was pretty sure that would give me an error regarding scope, but I decided to go ahead and test it anyway, and it worked so i left it. If you want to test this program but used Dev-C++, I recomend using another IDE because it is late and I am tired, MS Visual Studio Express Edition isn't too bad because it's free, just google it. |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Sun Sep 07, 2008 10:53 am Post subject: RE:Please comment on my program (Simple TXT Writer) |
|
|
There definitely exists such as over-commenting, and you sir, have met it. As well, on the issue of scope, don't just leave it there and tell someone that they have to use a different IDE, just fix it ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
Zeroth
|
Posted: Sun Sep 07, 2008 11:12 am Post subject: Re: Please comment on my program (Simple TXT Writer) |
|
|
My question for the programmer is, "Why C++?" |
|
|
|
|
![](images/spacer.gif) |
Bo0sT
![](http://compsci.ca/v3/uploads/user_avatars/86196945245bc23817a7f1.jpg)
|
Posted: Sun Sep 07, 2008 11:50 am Post subject: Re: Please comment on my program (Simple TXT Writer) |
|
|
Zeroth @ Sun Sep 07, 2008 11:12 am wrote: My question for the programmer is, "Why C++?"
because that is the language that i am trying to learn |
|
|
|
|
![](images/spacer.gif) |
Bo0sT
![](http://compsci.ca/v3/uploads/user_avatars/86196945245bc23817a7f1.jpg)
|
Posted: Sun Sep 07, 2008 11:55 am Post subject: Re: RE:Please comment on my program (Simple TXT Writer) |
|
|
Clayton @ Sun Sep 07, 2008 10:53 am wrote: There definitely exists such as over-commenting, and you sir, have met it. As well, on the issue of scope, don't just leave it there and tell someone that they have to use a different IDE, just fix it ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif)
I commented it like i did so that others can learn from my program if they don't know how to do something that I did, and sorry for not fixing it but, it works in my IDE and i did it at.... must have been 1:00am, so i was tired at the time. However it is a small bug, i dont think im going to bother fixing it, doesnt look like to many people are interested anyway, but i am glad this bug did come up last minute because i was able to learn from it, which was the whole point of me doing this little project in the first place |
|
|
|
|
![](images/spacer.gif) |
Bo0sT
![](http://compsci.ca/v3/uploads/user_avatars/86196945245bc23817a7f1.jpg)
|
Posted: Sun Sep 07, 2008 12:39 pm Post subject: Re: Please comment on my program (Simple TXT Writer) |
|
|
I fixed the scope error, now the program compiles in Dev-C++ and VC++ so feel free to give it a try
code: |
/*
* Simple TXT Writer, by Danny Knight
* Saturday, September 6, 2008
*
* This program was made as practice with
* many basic and standard C++ concepts
* and libraries. I commented everything
* in great detail so that others can
* learn from it.
*
* Notes --------------------------------
* -when writing to a file, after you have
* entered a line, you cannot go back and
* edit the line
* -cannot enter a file name greater than
* one word long
* -after adding to a text file, the new
* and old data are seperated by a blank
* line (I did not do this on purpose, I
* just couldn't figure out a quick and
* easy way to fix it)
*/
#include <iostream> // Standard C++ input / output stream
#include <string> // C++ string type
#include <fstream> // Standard C++ file stream
using namespace std; // Using standard namespace for standard C++ libraries
static void create_file(void); // Is run when the 'Create TXT' option is selected
static void open_file(void); // Is run when the 'Open TXT' option is selected
void cannot_open_file(char* title_of_file); // Displays an error message including the name of the file as an ARRAY OF CHARs to the standard output stream and exits the program returning '1'
void cannot_open_file(string title_of_file); // Displays an error message including the name of the file as a STRING to the standard output stream and exits the program returning '1'
void cannot_open_file(); // Displays an error message to the standard output stream and exits the program returning '1'
void main()
{
char option = ' '; // char being used to store the user's menu option
while (!(option == 'e' || option == 'E'))
{
cout << "Welcome to Simple Text Reader" << endl;
cout << "=============================" << endl;
cout << "A - Create TXT\nB - Open TXT\nE - Exit" << endl;
// Checks for valid input
for (;;)
{
cin >> option; // get value of option from keyboard
if (option == 'A' || option == 'a') // if option a is selected, leave a line, run option a's function and break the forever loop
{
create_file();
break;
}
else if (option == 'B' || option == 'b') // if option b is selected run option b's function and break the forever loop
{
open_file();
break;
} else if (option == 'E' || option == 'e')
exit(0);
else cout << endl <<"please enter an 'a' or a 'b' or an 'e'" << endl; // Otherwise display error message and continue looping until the user had entered valid input
}
system("cls"); // Clear the screen before the Main Menu shows
}
}
static void create_file() // Option a's function
{
string file_name; // Name of the file
string info; // Contents of the file
bool has_ext = false; // Weather or not the user put a .txt at the end of 'file_name'
ofstream output_file; // Object of the output file
system("cls"); // Clear the output window
cout << "Name your file: "; // prompt the user to name the file, initilizing 'file_name'
cin >> file_name;
for (int i = 0; i < file_name.length();i++) // run the loop up to as many times as there are letters in 'file_name'
{
if ((file_name[i] == '.') && (file_name[++i] == 't' || file_name[++i] == 'T') &&
(file_name[++i] == 'x' || file_name[++i] == 'X') &&
(file_name[++i] == 't' || file_name[++i] == 'T') && file_name[++i] == '\0') // if the user did put a .txt at the end of 'file_name', set has_ext to true and break the loop
{ has_ext = true; break; } else; // Otherwise keep looping until the end of 'file_name', (if there is no .txt, has_ext remains as false, its initial value)
}
// if has_ext is false, meaning the user did not put a .txt at the end of 'file_name', put .TXT at the end of it for them
if (has_ext == false)
file_name = file_name + ".TXT"; else; // Otherwise do nothing and continue with the program
output_file.open(file_name.c_str()); // Create the output file by calling the 'open' function on the 'ofstream' object, and pass in an object of string, casted to a primative C string using the function c_str because open doens't take C++ 'string's
if (!(output_file.is_open())) // If the file is unable to open, run the 'cannot_open_file' function
cannot_open_file(file_name); else; // Otherwise do nothing and continue with the program
// The following two lines are written to the text document created
output_file << "This document is created in Simple Text Reader" << endl;
output_file << "==============================================" << endl;
cout << "Writing to text file " << file_name << " (type $save to save)" << endl; // Prompt the user to write to the file, entering "$save" to save their work
while(!(info == "$save")) // Loop until "$save" is entered
{
getline(cin,info); // Gets a line of text from the 'cin' function and reads it into the 'info' object
if (info != "$save") // Only write 'info' to file if it is not "$save" (this condition is another way of writing the condition in the while loop)
output_file << info << endl;
}
output_file.close(); // Close output file
}
static void open_file() // option b's function
{
string file_name; // Name of the file
string info[30]; // Contents of the file
string new_info; // A string that contains the appended contents of a file if the user selects 'Edit'
bool has_ext = false; // Weather or not the user put a .txt at the end of 'file_name'
ifstream input_file; // Object of the input file
ofstream add_to_file; // This objcet opens a file as output, so that we can add the old data as well as the new stuff
char option; // Add to or Man Menu
int x; // Declared up here rather than in the for loop so that it can be used outside the for loop's block
system("cls"); // Clear the output window
cout << "Enter the name of the file to open" << endl;
cin >> file_name;
for (int i = 0; i < file_name.length();i++) // run the loop up to as many times as there are letters in 'file_name'
{
if ((file_name[i] == '.') && (file_name[++i] == 't' || file_name[++i] == 'T') &&
(file_name[++i] == 'x' || file_name[++i] == 'X') &&
(file_name[++i] == 't' || file_name[++i] == 'T') && file_name[++i] == '\0') // if the user did put a .txt at the end of 'file_name', set has_ext to true and break the loop
{ has_ext = true; break; } else; // Otherwise keep looping until the end of 'file_name', (if there is no .txt, has_ext remains as false, its initial value)
}
// if has_ext is false, meaning the user did not put a .txt at the end of 'file_name', put .TXT at the end of it for them
if (has_ext == false)
file_name = file_name + ".TXT"; else; // Otherwise do nothing and continue with the program
input_file.open(file_name.c_str());
if (!(input_file.is_open())) // If the file is unable to open, run the 'cannot_open_file' function
cannot_open_file(file_name);
else cout << "\nViewing " << file_name << "...\n" << endl; // Otherwise tell the user what file they are viewing
for (x = 0;!(input_file.eof());x++) // Function 'eof' returns true when the file is out of data (eof stands for "end of file")
{
getline(input_file,info[x]);
cout << info[x] << endl;
}
input_file.close();
cout << "===================" << endl;
cout << "A - Add to this TXT\nB - Main Menu" << endl;
cout << "===================" << endl;
for (;;)
{
cin >> option;
switch(option)
{
case 'b':
case 'B':
return; // Ends function, returning to main
case 'a':
case 'A':
{
system("cls");
add_to_file.open(file_name.c_str());
if (!(add_to_file.is_open()))
{ cout << "Unable to open file for output" << endl; exit(1); }
for (int c = 0;c < x;c++) // The for loop incrementor is how the language got it's name, because it is an "incrementation" of the language C..
{
cout << info[c] << endl;
add_to_file << info[c] << endl;
}
while (new_info != "$save") // Another way of writing (!(new_info == "$save"))
{
getline(cin,new_info);
if (!(new_info == "$save"))
add_to_file << new_info << endl; else;
}
return;
}
}
}
}
void cannot_open_file(char* name) // See prototype
{
cout << "Unable to open file: " << name << endl;
exit(1);
}
void cannot_open_file(string name) // See prototype
{
cout << "Unable to open file: " << name << endl;
exit(1);
}
void cannot_open_file() // See prototype
{
cout << "Unable to open file" << endl;
exit(1);
}
|
|
|
|
|
|
![](images/spacer.gif) |
|
|