The code below suppose to ask a user the person's name and compare that name with the names in the file and if found it must display the name's details such as Age and gender and ask the user if s/he wants to edit the person's details if the user says yes then the program must accept the name, age and gender from the user and save or write to the Temp file at the end it has to destroy the Temp file and keep everything else to the info files but it just jump and close the file and attempt to remove and rename it without searching from the existing file which is "Info.dat" any help will be appreciated

void Edit()
 {
     ifstream inFile;
     ofstream TempFile;
     string searchName;
     string N, NewName;
     int A, NewAge;
     char G, NewGender;
     char Answer;

    inFile.open("Info.dat");
    if(inFile)
   {
      TempFile.open("Temp.dat");
      cout << "Enter the person name Please: ";
      cin >> searchName;

     inFile >> N;
     while(inFile)
    {
      inFile >> A;
     inFile >> G;
     if(searchName.compare(N) == 0)
     {
       cout << "Name: " << N << endl;
       cout << "Would you like to edit (y/n)? ";
       cin >> Answer;
       if((Answer == 'Y') || (Answer == 'y'))
      {
        cout << "PLease enter the new name: ";
        cin >> NewName;
      }
      else
        NewName = N;

       cout << "Age: " << A << endl;
       cout << "Would you like to edit (y/n)? ";
       cin >> Answer;
       if((Answer == 'Y') || (Answer == 'y'))
     {
        cout << "PLease enter the new age: ";
        cin >> NewAge;
     }
     else
       NewAge = A;

       cout << "Gender: " << G << endl;
       cout << "Would you like to edit (y/n)? ";
       cin >> Answer;
       if((Answer == 'Y') || (Answer == 'y'))
       {
          cout << "PLease enter the new gender: m-Male, f-Female: ";
          cin >> NewGender;
       }
       else
          NewGender = G;
      {
         TempFile << N << endl;
         TempFile << A << endl;
         TempFile << G << endl;
      }
      inFile >> N;
   }
   inFile.close();
   TempFile.close();
   int OK = remove("Info.dat");
   if(OK == 0)
   {
     cout << "Info.dat successfully deleted" << endl;
     int OK2 = rename("Temp.dat", "Info.dat");
     if(OK2 == 0)
       cout << "Successfully rename Temp.dat as Info.dat" << endl;
     else 
      cout << "could not rename Temp.dat as Info.dat" << endl;
   }
   else
     cout << "Info.dat could not be deleted" << endl;
  }
	 /*else
		 cout << "Error in oppening the file" << endl;*/
}
}

lines 19-23 can be rewritten like below because it isn't necessary to test for eof directly. while( Infile >> N >> A >> G) delete line 63 if you recode as above

Other than the above, I don't know what kind of help you want.

I have done that but now nothing is really happening, everytime I try to compile it, it asks me to make a selection, I would like to post all my code but I have 2 cpp files and one .h file so I really do not think that would be wise other than that it is so damn irritating to read 20 pages of code

you can attach them to your post. Press the Go Advanced button then the Manage Attachments button that appears towards the bottom of the page.

Thanx Dragon.......

I have attached them

Attachments
// LoanThandeka.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Person.h"
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
 using namespace std;

 void saveToFile(Person P)
 {
	 ofstream outFile;

	 outFile.open("Info.dat", ios::app);
	 outFile << P.GetName() << endl;
	 outFile << P.GetAge() << endl;
	 outFile << P.GetGender() << endl;
	 outFile.close();
 }

 void Edit()
 {
	 ifstream inFile;
	 ofstream TempFile;
	 string searchName;
	 string N, NewName;
	 int A, NewAge;
	 char G, NewGender;
	 char Answer;

	 inFile.open("Info.dat");
	 if(inFile)
	 {
		 TempFile.open("Temp.dat");
		 cout << "Enter the person name Please: ";
		 cin >> searchName;

		 while(inFile >> N >> A >> G)
		 {
			 if(searchName.compare(N) == 0)
			 {
				 cout << "Name: " << N << endl;
				 cout << "Would you like to edit (y/n)? ";
				 cin >> Answer;
				 if((Answer == 'Y') || (Answer == 'y'))
				 {
					 cout << "PLease enter the new name: ";
					 cin >> NewName;
				 }
				 else
					 NewName = N;

				 cout << "Age: " << A << endl;
				 cout << "Would you like to edit (y/n)? ";
				 cin >> Answer;
				 if((Answer == 'Y') || (Answer == 'y'))
				 {
					 cout << "PLease enter the new age: ";
					 cin >> NewAge;
				 }
				 else
					 NewAge = A;

				 cout << "Gender: " << G << endl;
				 cout << "Would you like to edit (y/n)? ";
				 cin >> Answer;
				 if((Answer == 'Y') || (Answer == 'y'))
				 {
					 cout << "PLease enter the new gender: m-Male, f-Female: ";
					 cin >> NewGender;
				 }
				 else
					 NewGender = G;
				 {
					 TempFile << N << endl;
					 TempFile << A << endl;
					 TempFile << G << endl;
				 }
			 }
			 inFile.close();
			 TempFile.close();
			 int OK = remove("Info.dat");
			 if(OK == 0)
			 {
				 cout << "Info.dat successfully deleted" << endl;
				 int OK2 = rename("Temp.dat", "Info.dat");
				 if(OK2 == 0)
					 cout << "Successfully rename Temp.dat as Info.dat" << endl;
				 else 
					 cout << "could not rename Temp.dat as Info.dat" << endl;
			 }
			 else
				 cout << "Info.dat could not be deleted" << endl;
		 }
	 /*else
		 cout << "Error in oppening the file" << endl;*/
	 }
 }
	 void Display()
	 {
		 const int Size = 100;

		 string Name;
		 int Age;
		 char Gender;
		 Person P[Size];
		 Person Temp;
		 ifstream inFile;
		 int count = 0;

		 //read file into array
		 inFile.open("Info.dat");
		 inFile >> Name;
		 while(inFile)
		 {
			 inFile >> Age;
			 inFile >> Gender;
			 P[count].Set(Name, Age, Gender);
			 count++;
			 inFile >> Name;
		 }
		 inFile.close();

		 for(int x = 0; x < (count - 1); x++)
		 {
			 for(int y = 0; y < (count - x - 1); y++)
			 {
				 if(P[y].GetName().compare(P[y+1].GetName()) > 0)
				 {
					 Temp = P[y];
					 P[y] = P[y+1];
					 P[y+1] = Temp;
				 }
			 }
		 }

		 //Display
		 cout << "Name\tAge\tGender" << endl;
		 for(int i = 0; i < count; i++)
		 {
			 cout << P[i].GetName() << "\t";
			 cout << P[i].GetAge() << "\t";
			 if((P[i].GetGender() == 'M') || (P[i].GetGender() == 'm'))
				 cout << "Male" << endl;
			 else 
				cout << "Female" << endl;
		}
	 }

	 void Menu()
	 {
		 char Choice;
		 Person P;
		 do
		 {
			 cout << "Menu: " << endl;
			 cout << "1. Add new Person information" << endl;
			 cout << "2. Edit person information" << endl;
			 cout << "3. display all information alphabetically by Name" << endl;
			 cout << "4. Exit" << endl;
			 cout << "PLease enter your choice:  ";
			 Choice = getche();
			 cout << endl;

			 switch(Choice)
			 {
			 case '1' :
				 P.GetDetails();
				 saveToFile(P);
				 break;
			 case '2':
				 Edit();
				 break;
			 case '3':
				 Display();
				 break;
			 case '4':
				 cout << "Exiting Program...bye." << endl;
				 break;
			 default: cout << "Invalid choice " << endl;
			 }
		 }while(Choice != '4');
	 }
	 int _tmain(int argc, _TCHAR* argv[])
	 {
		 Menu();
		 return 0;
	 }
#include "stdafx.h"
#include "Person.h"
#include <iostream>
#include <string>
#include <conio.h>
 using namespace std;

 void Person::GetDetails()
 {
	 system("cls");
	 cout << "PLease enter the name:  ";
	 cin >> Name;
	 cout << "PLease enter the Age:  ";
	 cin >> Age;
	 cout << "PLease enter m for Male, f for female:  ";
	 cin >> Gender;
 }
 
 void Person::Set(string N, int A, char G)
 {
	 Name = N;
	 Age = A;
	 Gender = G;
 }
 void Person::Show()
 {
	 system("cls");
	 cout << "Name: " << Name << endl;
	 cout << "Age:  " << Age << endl;
	 if((Gender == 'M') || (Gender == 'm'))
		 cout << "Gender: Male" << endl;
	 else
		 cout << "Gender: Female" << endl;
	 cout << "\nPress any key to continue...";
	 getch();
 }
#include <string>
 using namespace std;

 class Person
 {
	 private: 
		 string Name;
		 int Age;
		 char Gender;

	 public:
		 string GetName() { return Name; }
		 int GetAge() {return Age;}
		 char GetGender() {return Gender;}
		 void GetDetails();
		 void Set(string, int, char);
		 void Show();
 };

Two problems

1) don't use getche() -- instead use cin.get()

2) after receiving input for integers or a single character you are leaving the '\n' <Enter> in the keyboard buffer. That has to be flushed out because it causes great problems. The easiest way to do it is cin.ignore() But a more accurate method is described by Narue in her post at the top of this c++ board. Add that code in appropriate places and you problem will be solved.

3) You also need to change the loop in the Display() function like you did in the Edit() function.

Comments
Im sure Reputations dont excite you anymore, ur used to them

Have you ever read something and after that feel like you dont know what you thought you knew or wander if there is anything u know about the subject at hand, thats what alwayz happen when I read Naure's Posts but I will look at it, many thanx dude

This article has been dead for over six months. Start a new discussion instead.