In my test program (main) I am asking the user to give me a filename in case 2:
Code:
#include <iostream>
#include <string>
#include "Tree.h" // Tree class definition
#include "AddressBook.h"
using namespace std;
int main()
{
//Tree< string > stringTree;
AddressBook entry;
string someName, someAddress, somePhone;
string val;
string nameOfFile;
int choice;
int num = 0;
cout << "What would you like to do? "<< endl;
cout << "1=insert" << endl;
cout << "2=Read in a file: " << endl;
cout << "3=print the nodes" << endl;
cout << "4=delete a node" << endl;
cout << "5=Write out the File" << endl;
cout << "9=exit "<< endl;
cout << endl;
cin >> choice;
cin.ignore(100, '\n');
while(choice !=9)
{
switch (choice)
{
case 1: cout << "Enter a name for the list: " << endl;
entry.addPerson(someName, someAddress, somePhone);
break;
case 2: cout << "Enter the name of the file you want to read: " << endl;
getline(cin, nameOfFile);
entry.readFromFile(nameOfFile);
break;
case 3: cout << "Here is the List:"<<endl;
entry.printAddressBook();
break;
case 4: cout << "Which name would you like to delete?" << endl;
getline(cin, val);
entry.deletePerson(val);
break;
case 5: cout << "File will now be saved." << endl;
entry.writeToFile(nameOfFile);
break;
default: cout << "Invalid Selection" << endl;
}//end switch
cout << endl;
cout << "What would you like to do? "<< endl;
cout << "1=insert" << endl;
cout << "2=Read in a file:" << endl;
cout << "3=print the nodes" << endl;
cout << "4=delete a node" << endl;
cout << "5=Write out the file " << endl;
cout << "9=exit "<< endl;
cout << endl;
cin >> choice;
cin.ignore(100, '\n');
}//end while
cout << endl;
return 0;
} // end main
That filename is (supposed to be) passed to a function readFromFile() that looks like this:
Code:
void AddressBook::readFromFile(string fileName)
{
ifstream infile;
string newName;
string newAddress;
string newPhone;
infile.open("fileName.txt");
if(!infile)
{
cout<<"The input file does not exist."<< endl;
}
getline(infile, newName);
while(infile)
{
getline(infile, newAddress);
getline(infile, newPhone);
const Person *newPerson = new Person(newName, newAddress, newPhone);
addressTree->insertNode(*newPerson);
getline(infile, newName);
}//end while
infile.close();
//open the file
}//end readFromFile
for some reason, if I try to pass the filename it doesn't work. It says the file doesn't exist. So, in an effort to figure out what is wrong, I've tried debug and can't locate the problem. So, through the process of elimination I've tried the following:
I've tried it with the quotes, without .txt with the user putting in the .txt, and neither work. If I remove the quotes and leave the .txt I get an error message:
c:\program files\microsoft visual studio\myprojects\bst addressbook\addressbook.cpp(46) : error C2039: 'txt' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
If I remove the quotes and remove the .txt I get this error message:
c:\program files\microsoft visual studio\myprojects\bst addressbook\addressbook.cpp(46) : error C2664: 'void __thiscall std::basic_ifstream<char,struct std::char_traits<char> >:: open(const char *,int)' : cannot convert parameter 1 from 'class std::b
asic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I am happy to post the entire program, which is very lengthy, if needed. It is a parameter of the assignment that I pass a file name this way, so I can't do it the easy way (which works) that is by simply putting the name of the file instead of the variable.
Any ideas?
Thank you for your help!