Click here to Skip to main content
15,883,606 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
 could use help getting my code to work.  Here is the code so far:

 

Main.cpp 

 

#include <iostream>  

 #include <iomanip>  

 #include <fstream>  

 #include <string>  

 #include "VMC.h" 

 using namespace std;  

 // Functions 

   

int main(){             //Main Function  

 system("cls");  

 bool run=true;  

 do{  

 int Option;           //Main menu  

     cout << "----------------------Address Book-----------------------------" << endl;  

     cout << "\n";  

    cout << "Choose an option" << endl;  

    cout << "1) Add Contact" << endl;  

    cout << "2) Edit Contact" << endl;  

    cout << "3) Delete Contact" << endl;  

    cout << "4) View All Contacts" << endl;  

 //    cout << "5) Search Address Book" << endl;  

     cout << "6) Exit" << endl << endl;  

   cout << "Choose an option: ";  

     cin >> Option;  

     cin.ignore();  

 switch (Option){  

     case 1:  

         Vmc.addPerson();  

         break;  

    case 2:  

         Vmc.editPerson();  

         break;  

     case 3:  

         Vmc.deletePerson();  

         break;  

    case 4:  

         Vmc.viewPerson();  

         break;  

 //   case 5:    //Need to make 

 //       Vmc.searchPerson();  

 //       break;  

     case 6:  

         run = false;  

         break;  

     default:  

         cout << "Please Choose between 1 to 6" <<endl;  

         int main();  

 }  

 } while(run);  

 cout << "End of Program";  

 } 

 

 

 

VMC.h 

 

  

class Vmc  

{ // initializes variables and functions 

    private:  

    //string Fname,Lname,Address1,Phone;   

    std::string Fname; 

    std::string Lname; 

    std::string Address1; 

    std::string Phone; 

     

    public:  

     

        Vmc (string Fname,string Lname,string Address1,string Phone);  

        //char Fname[20],Lname[20],Address1[30],Phone[20]; 

         

void addPerson();  

void viewPerson();  

void searchPerson();  

void editPerson();  

void deletePerson();  

  

};  

#endif 

 

VMC.cpp 

 

 

  

#include <iostream> 

  

int Vmc::addPerson() // Adds a person to the database 

 {  

 cout << "Enter First Name: "; 

getline(cin, Fname); 

 

cout << "Enter Last Name: "; 

getline(cin, Lname); 

 

cout << "Enter Address: "; 

getline(cin, Address1); 

 

cout << "Enter Contact Number: "; 

getline(cin, Phone); 

  

}  

   

int Vmc::viewPerson() // Views an Entry 

  

{  

  

system("cls"); 

  

//  string Fname, Lname, Address1, Phone; 

// ifstream address("address.txt"); 

  

cout << endl; 

system ("pause"); 

system ("cls"); 

   

}  

   

//int searchPerson() //need help to make 

  

//{  

  

//std::cout << "Enter an integer: ";  

  

//int x{};  

  

//std::cin >> x;  

  

//return x;  

  

//}  

   

int Vmc::editPerson() // Edits an entry 

  

{  

  

system("cls"); 

int choice; 

double counter, number; 

string Fname, Lname, Address1, Phone, Fname2, Lname2, Address2, Phone2, choice2,choice3; 

ifstream edit("address.txt"); 

ofstream temp("Temp.txt", ios::app); 

cout << "Please type the Entry number that you wish to delete: "; 

cin >> choice; 

cout << endl; 

 

while (edit >> counter >> Fname2 >> Lname2>> Address2 >> Phone2){ 

if (counter==choice){ 

cout << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Phone2 << endl<<endl; 

cout << "Is this the contact that you wish to delete? (y or n) "; 

cin >> choice3; 

cout << endl; 

} 

if (choice3=="n") { 

main(); 

} 

if (counter<choice){ 

temp << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl; 

} 

if (counter > choice){ 

temp << counter - 1 << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl; 

} 

} 

 

edit.close(); 

temp.close(); 

 

  

if (remove("address.txt")==0){ 

cout << "Succesful Removing File" << endl; 

}else{ 

cout << "Error removing"<< endl; 

} 

if(rename("Temp.txt", "address.txt")==0){ 

cout << "Succesful Renaming file"<< endl;  

}else{ 

cout << "Error renaming"<<endl; 

} 

system("pause"); 

system("cls"); 

} 

   

}  

    

int Vmc::deletePerson() //Deletes the person 

  

{  

  

system("cls"); 

int choice; 

double counter, number; 

string Fname, Lname, Address, Phone, Fname2, Lname2, Address2, Phone2, choice2,choice3; 

ifstream edit("address.txt"); 

ofstream temp("Temp.txt", ios::app); 

cout << "Please type the Entry number that you wish to delete: "; 

cin >> choice; 

cout << endl; 

 

while (edit >> counter >> Fname2 >> Lname2>> Address2 >> Phone2){ 

if (counter==choice){ 

cout << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Phone2 << endl<<endl; 

cout << "Is this the contact that you wish to delete? (y or n) "; 

cin >> choice3; 

cout << endl; 

} 

if (choice3=="n") { 

main(); 

} 

if (counter<choice){ 

temp << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Phone2 << endl; 

} 

if (counter > choice){ 

temp << counter - 1 << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Phone2 << endl; 

} 

} 

 

edit.close(); 

temp.close(); 

 

  

if (remove("address.txt")==0){ 

cout << "Succesful Removing File" << endl; 

}else{ 

cout << "Error removing"<< endl; 

} 

if(rename("Temp.txt", "address.txt")==0){ 

cout << "Succesful Renaming file"<< endl;  

}else{ 

cout << "Error renaming"<<endl; 

} 

system("pause"); 

system("cls"); 

} 

  

 } 


What I have tried:

I am trying to use classes and header files to generate a list of entries.
Posted
Updated 22-Feb-22 10:58am
Comments
Patrice T 15-Oct-21 3:57am    
And you can describe the problems you encounter ?
Richard MacCutchan 15-Oct-21 5:45am    
You need to understand the difference between static and non-static methods in a class, and how to call them. Also you have the following statement in your do/while loop:
int main();

That makes no sense, so you should remove it.

And your code would be considerably easier to understand if you used single spacing more.

We have no idea what you code is meant to do, how to use it, what problems you are meeting, or any access to your data for testing - there isn't a lot we can do to help you as a result!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
You better start with some Learn C++ tutorial to learn about the language and its tools.
It really would help you not only to understand and solve the problem, and last but not least: ask the correct questions.

Tip: learn to use the debugger
 
Share this answer
 
Hello,
I could not find entry point of that code. Please try to write at least one entry point for that i advice you try to create Console Application Project Template -Hello World! example- via Wizard. I may be look like fllowing one.

#include ....
...

int main() {
/*
Body of your main routine-entry point
*/
return 0;
}

After that step by step try add some definitions variables , function etc. Do not forget to compile & debug at each step.

Good luck,

Regards,
IM
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900