Need help with a program to read and write to a disc.

This is the last assignment in my programming 141 class. Sorry that there is so much code, I had it working but it isn't working anymore that I am using the menu.

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

void menu ()
{

cout << "This program is built to do 5 different functions. \nPlease choose the funcion you wish to accomplish. \n\n"

<< "1 = Takes an entered body of words and saves it to the disc. \n\n"

<< "2 = Takes a specified path and filename, read the test and tallys the frequency count of the word length. \n\n"

<< "3 = Outputs a horizontal bar chart of word-length frequency distribution. \n\n"

<< "Please press \"0\" when finished \n\n";


}


void writeDisk()
{
const int size = 81;
char line[size];
char *filename = new char[size];
ofstream fout;

cout << "To write a file to disc, enter its path and filename.extension: \n";
cin.getline(filename,size);
fout.open(filename);
if(!fout)
{
cerr << "Failure trying to create disc file: " << filename;
system("pause");
exit(0);
}

cout << "Enter lines of text." << endl;
cout << "To quit, enter <ctrl+z> by itself on the last line:\n";
while (!cin.getline(line,size).eof())
fout << line << endl;
fout.close();
cin.clear();
}

void readDisk(int freq[])
{
const int size = 81;
char filename[size];
string word;
ifstream fin;

cout <<" Enter path and filename of the file from which to read:\n";
cin.getline(filename,size);
fin.open(filename);
if(!fin)
{
cerr << "Failure to open disc file: " << filename;
system("PAUSE");
exit(0);
}
for (int i=0; i < 30; i++)
freq[i] = 0;
while(fin >> word)
freq[word.length()]++;
fin.close();
}
void histogram(int freq[])
{
string line;
cout << "\n" << "No. of Words --> ";
for(int i=0; i <=40; i+=5)
cout << i << setw(5);
line.assign(57, '-');
cout << endl << line << endl;
cout << " of Length: ";
for(int i=1; i <=21; i++)
{
cout << i;
for(int j=1; j<=freq[i]; j++)
cout << char(219);
cout << endl << setw(17);
}
}

int main()
{
int function;


menu ();

int freq[30];

do
{
cin >> function;

switch (function)
{
case 1 : writeDisk (); break;
case 2 : readDisk (freq); break;
case 3 : histogram (freq); break;
case 9 : menu (); break;
case 0 : cout << "\nThank you for using this program!\n" ; break;
default : cout << "Please enter a valid function choice.";
}


}while (function !=0);


return 0;
}
Topic archived. No new replies allowed.