- #1
yungman
- 5,755
- 293
Hi
I am integrating my program into using different Classes. I have been trouble shooting for a while and I am stuck on this error and I don't understand the problem even reading online. I am using two Specification files with no Implementation files. I just want to make it easier with less files. I have one header file for working with read and write file. Another header file to work with add, sort, delete and display content of the vector DirV[].
This is fileManage.h for read and write to file.
This is vectorEdit.h for add, delete, sort and display content of vector DirV[].
This is the main():
I have been working on the integration since yesterday. This is really pushing beyond my knowledge. I spent 3 hours troubleshooting just to find I missed a ';' at the very end of the header file! The error showed was "using namespace std;" in main() doesn't work and I had to put std:: on every single cout, cin, setw(), left, right!The error messages are:
What is error C3867? I read on line and I don't understand. I don't know what I did wrong.
Please help. Also please comment on how I write the program. First time doing this, no idea whether this is right or not. I know I have to split up the vectorEdit.h as it's very long.
Thanks
I am integrating my program into using different Classes. I have been trouble shooting for a while and I am stuck on this error and I don't understand the problem even reading online. I am using two Specification files with no Implementation files. I just want to make it easier with less files. I have one header file for working with read and write file. Another header file to work with add, sort, delete and display content of the vector DirV[].
This is fileManage.h for read and write to file.
C++:
#ifndef fileManage_H
#define fileManage_H
#include <vector>
#include <fstream>
#include <iostream>
#include "vectorEdit.h"
class fileClass
{ private:
const static int nameLen = 25, phoneLen = 12;
public:
vectorClass vectC;
bool newF, failOpen;
std::fstream file;
bool testFile()//
{ failOpen = false;
file.open("Directory.dat", std::ios::in | std::ios::binary);
if (file.fail())//1st attempt to open file
{ file.open("Directory.dat", std::ios::in | std::ios::binary);
if(file.fail())//2nd attempt to open file
{ createFile();//file does not exist, create file
file.open("Directory.dat", std::ios::in | std::ios::binary);
if (file.fail()) failOpen = true;//If fail, there is a problem
else failOpen = false;//Successfully created file, no read into DirV[].
}//end 2nd attempt
else { failOpen = false; readFile(); }//else, 2nd attempt successfuel, read into DirV[]
}//End if 1st attempt.
else { failOpen = false; readFile(); }//else open file successfully, read into DirV[]
file.close();
return failOpen;
}//End testFile()
void createFile()
{ file.open("Directory.dat", std::ios::out | std::ios::binary | std::ios::app);
file.close();
}
void readFile()//Assume file is OPEN already
{while (!file.eof())
{ file.read(reinterpret_cast<char*>(&(vectC.Dir)), sizeof(vectC.Dir));
if (file.eof()) { break; }
vectC.DirV.push_back(vectC.Dir);
}
}
void writeFile()//Assume file is closed, open file to write DirV[] into file.
{
int ct2 = 0;
file.open("Directory.dat", std::ios::out | std::ios::binary);
do//write the updated data in vector DirV into the file before closing
{ file.write(reinterpret_cast<char*>(&vectC.DirV[ct2]), sizeof(vectC.DirV[ct2]));
ct2++;
} while (ct2 < vectC.DirV.size());
file.close();
}//End of writeFile
};//End of fileClass
#endif
C++:
#ifndef vectorEdit_H
#define vectorEdit_H
#include <vector>
#include <fstream>
#include <iomanip>
class vectorClass
{
private:
const static int nameLen = 25, phoneLen = 12;
public:
struct Directory
{
char lastName[nameLen]; char firstName[nameLen];
char phone[phoneLen]; int element;
};
std::vector<Directory>DirV;
Directory Dir, Temp; bool newF;
void addName(int& newName)
{ int size, selCase;
DirV.push_back(Dir);//Push to next available element of DirV.
size = DirV.size();// DirV[midPt].element = midPt; newName = midPt;
int startPt = 0, endPt = size - 2, midPt = (endPt - startPt) / 2;
if ((size == 1) || ((strcmp(DirV[endPt].lastName, Dir.lastName) <= 0) & (size >= 2)))
selCase = 0;//Don't have to sort or anything, Dir is in last element already
//Case 0 for first entry when size = 1 OR when Dir.lastName is
// >= to last element of DirV(Dir.lastName >= DirV[endPt].lastName.
else
{ if ((strcmp(Dir.lastName, DirV[startPt].lastName) <= 0) && (size > 1))
selCase = 1;//If Dir smaller than DirV[0] AND (size >1)
else
{ startPt = 0, endPt = size - 2; midPt = (endPt - startPt) / 2;
do { if (strcmp(Dir.lastName, DirV[midPt].lastName) <= 0)
{ endPt = midPt; midPt = (startPt + endPt) / 2;}
else { startPt = midPt; midPt = (startPt + endPt) / 2; }
} while (startPt != midPt);
selCase = 2;
}//else selCase = 2
}//End if((strcmp(Dir.lastName, DirV[startPt].lastName) <= 0) && (size > 1))
switch (selCase)
//(size = 1) AND Dir.lastName => DirV[endPt].lastName
{ case 0: { DirV[size - 1].element = size - 1; newName = size - 1; break; }
case 1://Dir.lastName <= DirV[0].lastName
{ int i;
for (i = 1; i <= (size - 1); i++)
{ DirV[size - i] = DirV[size - i - 1];
DirV[size - i].element = size - i;
DirV[size - i - 1].element = size - i - 1;
}//End for
DirV[size - i] = Dir; newName = size - i;
DirV[size - i].element = size - i;
break;
};//End case 1
case 2:
{ int j;
for (j = 1; j <= (size - endPt - 1); j++)
{ DirV[size - j] = DirV[size - j - 1];
DirV[size - j].element = size - j;
DirV[size - j - 1].element = size - j - 1;
}
DirV[size - j] = Dir; newName = size - j;
DirV[size - j].element = size - j;
break;
};//end case 2
};//End switch
}
void sort_firstName(int& newName)//DirV[newName] is the newly added name.
{ int beginLname = newName, endLname = newName;//start out all equal
int bubbleDown, bubbleUp;
Temp = DirV[newName];//Temporary structure variable
bool doneUp = false, doneDown = false, moveUp = false;
bubbleUp = newName; bubbleDown = newName;
while ((bubbleUp > 0) & !doneUp)//Prevent bubbleUp going negative
{if ((strcmp(DirV[newName].lastName, DirV[newName - 1].lastName) == 0)
& (strcmp(DirV[newName].firstName, DirV[newName - 1].firstName) < 0))
{ DirV[newName] = DirV[newName - 1]; DirV[newName].element = newName;
DirV[newName - 1] = Temp; DirV[newName - 1].element = newName - 1;
newName--; bubbleUp--; moveUp = true;
}
else (doneUp = true);
}//END while((bubbleUp > 0)& !doneUp)
if (moveUp == false)
{ while ((bubbleDown < (DirV.size() - 1)) & !doneDown)
{ if ((strcmp(DirV[newName].lastName, DirV[newName + 1].lastName) == 0)
& (strcmp(DirV[newName].firstName, DirV[newName + 1].firstName) > 0))
{ DirV[newName] = DirV[newName + 1]; DirV[newName].element = newName;
DirV[newName + 1] = Temp; DirV[newName + 1].element = newName + 1;
newName++; bubbleDown++;
}
else doneDown = true;
}//END while (decrement < bubbleUp)
}
}
void showRange(char selName[25])
{ int size = DirV.size(), stpt = 0, edpt = size - 1;
int mdpt = (stpt + edpt) / 2, index, comp;
do //search to within range using 2 characters in selName
{ comp = strncmp(selName, DirV[mdpt].lastName, 2);
if (comp != 0)
{ if (comp > 0) { stpt = mdpt; mdpt = (stpt + edpt) / 2; }
else { edpt = mdpt; mdpt = (stpt + edpt) / 2; }
}
} while ((comp != 0) && (stpt + 2 <= edpt));//Matching DirV[mdpt]
for (index = -2; index <= 3; index++)
{ if (((mdpt + index) >= 0) && ((mdpt + index) <= (size - 1)))
{std::cout << " Element #" << DirV[mdpt + index].element <<
"] is: Last name: " << std::left << std::setw(10) << DirV[mdpt + index].lastName <<
" first name: " << std::left << std::setw(10) << DirV[mdpt + index].firstName << "\n\n";
}
}//End for loop to display range of names if it is valid.
}//End showRange
void deleteName(int numDelete)
{
char sure;
int edpt = DirV.size() - 1;
std::cout << " Is this what you chose?\n";
std::cout << " Element #" << DirV[numDelete].element <<
"] is: Last name: " << std::left << std::setw(10) << DirV[numDelete].lastName <<
" first name: " << std::left << std::setw(10) << DirV[numDelete].firstName << "\n\n";
std::cout << " Are you sure you want to delete this:\n\n"; std::cin >> sure;
if (tolower(sure) == 'y')
{
for (int i = 0; i < (edpt - numDelete); i++)
{ DirV[numDelete + i] = DirV[numDelete + i + 1];
DirV[numDelete + i].element = numDelete + i;
}
DirV.pop_back();
}
}
};
#endif
C++:
#include <iostream>
#include <cstring>
#include <vector>
#include <iomanip>
#include "fileManage.h"
#include "vectorEdit.h"
using namespace std;
vectorClass vectC;
fileClass file;
int main()
{
int newName, index = 0, compSize, numDelete;
char more, displayAll, choice;
char selName[25];
cout << " Welcome to the directory program. You can add name, contact\n";
cout << " informations and it will store in a file. You can display selected\n";
cout << " names and choose to delete anyone you want.\n\n";
file.testFile();
if (file.testFile() == true)
{ cout << " Problem opening file.\n\n"; return 0;}
file.readFile();
do {cout << " Please enter 'a' to add names, 's' to dispay complete list. 'r' to\n";
cout << " show range of names. 'd' to delete a specific name. 'q' to quit.\n\n";
cout << " Please enter what you want to do: "; cin >> choice; cout << "\n\n";
switch (choice)
{
case 'a': //Add new names and sort
{ do {cout << " Enter last name: "; cin >> vectC.Dir.lastName;
//std::cout << " Enter first name: "; cin >> fileC.Dir.firstName;
vectC.addName(newName);
vectC.sort_firstName(newName);
cout << " \n\nDo you want to enter another name? "; cin >> more;
} while (tolower(more) == 'y');
break;
};//End case 'a'
case 's': //Display entire list of names and info
{ index = 0;
if (vectC.DirV.size() > 0)
{do { cout << " Element #" << vectC.DirV[index].element <<
"] is: Last name: " << left << setw(10) << " first name: "
<< left << setw(10) << vectC.DirV[index].lastName <<
vectC.DirV[index].firstName << "\n\n";
index++;
} while (index < vectC.DirV.size());
}
else cout << " There is no name in the file.\n\n";
break;
};//End case 's'
case 'r':
{if (vectC.DirV.size() > 0)
{cout << " Enter the first 2 character of the last name to search.";
cin >> selName; cout << "\n\n";
vectC.showRange(selName);//Display range before and after the name.
}//End if (fileC.DirV.size() > 0)
else cout << " There is no name in the file.\n\n";
break;
};//End case 'r'
case 'd'://Delete a name.
{if (vectC.DirV.size() > 0)
{vectC.showRange(selName);
cout << " Enter the Element# of the left to be deleted: ";
cin >> numDelete;
vectC.deleteName(numDelete);
}
else cout << " There is no name in the file.\n\n";
break;
};//End case 'd'
case 'q': { cout << " Are you sure you want to quit? ";
cin >> choice; cout << "\n\n"; break;
};
default: cout << " Not a valid choice.\n";
}//End switch
} while (choice != tolower('q'));//End choice what to do
file.writeFile();
return 0;
}//End main()
What is error C3867? I read on line and I don't understand. I don't know what I did wrong.
Please help. Also please comment on how I write the program. First time doing this, no idea whether this is right or not. I know I have to split up the vectorEdit.h as it's very long.
Thanks
Last edited: