One last question.....
I fixed the code (below) and now the input file won't open for reading (in the menu section, when it's read to the screen).... I can't figure this out for the life of me why it's doing this, especially since the file is closed earlier. Any thoughts?
Again-- Thanks a ton!!
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
fstream input;
input.open("input.txt", ios::in);
fstream hexwrite;
hexwrite.open("hex.txt", ios::out | ios::in | ios::trunc);
fstream vowel;
vowel.open("vowel.txt", ios::out | ios::in | ios::trunc);
fstream consonant;
consonant.open("consonant.txt", ios::out | ios::in | ios::trunc);
if (input.fail())
{
cout << "Cannot open input.txt";
return -1;
}
if (hexwrite.fail())
{
cout << "Cannot open hex.txt";
return -1;
}
if (vowel.fail())
{
cout << "Cannot open vowel.txt";
return -1;
}
if (consonant.fail())
{
cout << "Cannot open consonant.txt!";
return -1;
}
char read;
char reader[2];
int hexconvert;
int choice=0;
int a;
while (!input.eof())
{
input >> read;
if (read == 'a' || read == 'e' || read == 'i' || read == 'o' || read == 'u' || read == 'y' ||
read == 'A' || read == 'E' || read == 'I' || read == 'O' || read == 'U' || read == 'Y')
{
vowel << read;
}
else
{
consonant << read;
}
hexconvert = read;
hexwrite << hex << hexconvert << " ";
}
input.close();
hexwrite.close();
vowel.close();
consonant.close();
while (choice != 5)
{
cout << "Menu:" << endl
<< "1) Display contents of input.txt" << endl
<< "2) Display contents of vowel.txt" << endl
<< "3) Display contents of consonant.txt" << endl
<< "4) Display contents of hex.txt" << endl
<< "5) Quit" << endl
<< "Choice: ";
cin >> choice;
switch (choice)
{
case (1):
{
input.open("input.txt", ios::in);
if(input.fail())
{
cout << "failed to open input" << endl;
}
while (!input.eof())
{
input >> read;
cout << read;
}
cout << endl;
input.close();
continue;
}
case (2):
{
if (vowel.fail())
{
cout << "failed to open vowel" << endl;
}
vowel.open("vowel.txt", ios::in);
while (!vowel.eof())
{
vowel >> read;
cout << read;
}
cout << endl;
vowel.close();
continue;
}
case (3):
{
consonant.open("consonant.txt", ios::in);
while (!consonant.eof())
{
consonant >> read;
cout << read;
}
cout << endl;
consonant.close();
continue;
}
case (4):
{
hexwrite.open("hex.txt", ios::in);
while (!hexwrite.eof())
{
/********************
for (a=0;a<2;a++)
{
reader[a] = '\0';
}
hexwrite.get(reader, 2, '\0');
cout << reader << " ";
********************/
hexwrite >> hexconvert;
cout << hex << hexconvert << " ";
}
cout << endl;
hexwrite.close();
continue;
}
}
}
return 0;
}