this is my first program ever, my email address is in the program. tell me if anything is too complicated or there are bugs and if you can tell me how to fix them. this program is me playing with fstream and it writes to a txt file or creates a new one if it does not exist, it also allows you to read a file by typing the full path of the file or putting the text file into the same folder and just type out the file name.

keep in mind this is my first real program so be gentle kay?

Attachments
#include <fstream>
#include <iostream>
using namespace std;

int write();
int read();
int main();
int haha();

int haha()
{
    string options;
    
    system("cls");
    
    cout << "to exit type q" << endl << "to read a file type r" << endl << "to write a file type w" << endl;
    cin >> options;
    
    if (options == "q")
    return 0;
    
    else if (options =="r")
    return read();
    
    else if (options == "w")
    return write();
}

int main()
{
    cout << "welcome to text editor, this is my first program so if you see any bugs or you dont understand something, email me at bubbahaley2@yahoo.com. hope you enjoy it" << endl << endl;
    
    string option;
    cout << "to read a text file, type read" << endl << "to write to a text file type write" << endl;
    cin >> option;
    
    if (option == "write")
    return write();
    
    else if (option == "read")
    return read();
}

int write()
{
    system("cls");
    
    char text[20000];
    char thing[100];
    
    ofstream file;
    
    cin.ignore();
    
    cout << "which file would you like to write to?" << endl;
    cin.getline (thing, 100);
    
    cout << "what would you like to write? (up to two thousand characters)" << endl;
    cin.getline (text,20000);
    
    file.open(thing);
    file<<text;
    file.close();
    
    cout << "press enter to continue";
    
    cin.ignore();
    
    return haha();
}

int read()
{
    system("cls");
    
    ifstream file;
    char thing[200];
    char output[20000];
    
    cin.ignore();
    
    cout << "what file would you like to be read?" << endl;
    cin.getline (thing, 200);
    
    
    system("cls");
    
    file.open(thing,ios::in|ios::out|ios::binary);
    
    file.getline (output, 20000);
    cout << output << endl << endl << endl << "press enter to continue" << endl;
    
    cin.ignore();
    
    return haha();
}

First program? Really?

Function main( ) does not need a prototype.

Function haha( ) is quite clever, sort of. It's really a bit confusing to be calling this to keep the program looping through the options. What's going on is a growing pile of functions on the stack - like recursion, but harder to follow. Keep this up long enough and you'll probably crash the program.

Why not just put a loop in main( ) that continues till quit is chosen?

Clarity is important.

First program? Really?

Function main( ) does not need a prototype.

Function haha( ) is quite clever, sort of. It's really a bit confusing to be calling this to keep the program looping through the options. What's going on is a growing pile of functions on the stack - like recursion, but harder to follow. Keep this up long enough and you'll probably crash the program.

Why not just put a loop in main( ) that continues till quit is chosen?

Clarity is important.

ok thanks, I'll make that improvement. but what i wanted to avoid was the user having to go through the beginning prompt.

Put the intro outside the loop. More or less like this structure:

int main()
{
    string option;

    cout << "welcome to text editor, ...blah blah" << endl << endl;
    
     do
    {
        cout << "to read a text file, type read" << endl 
              << "to write to a text file type write" << endl
              << "to end program type quit" << endl;
         cin >> option;
    
        if (option == "write")
               write();
    
         else if (option == "read")
                read();

        else if ( option == "quit" )
                goodbye_msg( );
        else
                invalid_choice_msg( );
     }while ( option != "quit" );

      return 0;
}

Wow, a zig-zag flow of control.
But is definitely not a wow. Sit down with a pencil-paper and design a flow chart of your program.
The flow of program is not efficient.
main calls read/write which calls haha which can in turn call read/write and so on.
This is definitely not feasible.
Construct a loop as vmanes told you and remove the haha function.
Don't use system(). file.open(thing,ios::in|ios::out|ios::binary); Why did you opened in binary mode?

This article has been dead for over six months. Start a new discussion instead.