I need help with inputing the file.
The file has the "lastName" "firstName" "phoneNumber" "email" then a new line. When i read in all 4 rows it says the first line in all 4 rows.


the infile is in main

#include <iostream>
#include <fstream>
using namespace std;

void mainMenu();
void save(char[][32],char[][9],char[][32],char[][32],int);
int delet(char[][32],char[][9],char[][32],char[][32],int);
void add(char[][32],char[][9],char[][32],char[][32],int);
void list(char[][32],char[][9],char[][32],char[][32],int);
void find(char[][32],char[][9],char[][32],char[][32],int);
int main()
{
	int i;
	const int PEOPLE=20;
	char firstName[PEOPLE][32];
	char lastName[PEOPLE][32];
	char phoneNumber[PEOPLE][9];
	char email[PEOPLE][32];
	int count=0;

	ifstream inFile;
	inFile.open("book.txt");

	while(inFile)
	{
		count++;
		inFile >> lastName[count];
		inFile >> firstName[count];
		inFile >> phoneNumber[count];
		inFile >> email[count];
	}	

	bool cheese=false;
	while(!cheese)
	{
		mainMenu();
		char menu;
		cin  >> menu;

		if (menu == 'A'||menu == 'a')
		{
			if (count<PEOPLE)
			{
				add(firstName,phoneNumber,email,lastName,count);
				count++;
			}
		}
		if (menu == 'D'||menu == 'd')
		{
			count=delet(firstName,phoneNumber,email,lastName,count);
		}
		else if (menu == 'F'||menu == 'f')
		{
			find(firstName,phoneNumber,email,lastName,count);
		}
		else if (menu == 'L'||menu == 'l')
		{
			list(firstName,phoneNumber,email,lastName,count);
		}
		else if (menu == 'S'||menu == 's')
		{
			save(firstName,phoneNumber,email,lastName,count);
		}
		else if (menu == 'E'||menu == 'e')
		{
			inFile.close();
			return 0;
		}
	}
		return 0;
}


void mainMenu()
{
	cout << "----Main Menu----\n";
	cout << "A - Add Person\n";
	cout << "D - Delete Person\n";
	cout << "F - Find and Display Person\n";
	cout << "L - List All People\n";
	cout << "S - Save List\n";
	cout << "E - Exit\n";
}

void add(char firstName[][32],char number[][9],char email[][32],char lastName[][32],int count)
{
	int i;
	int length;
	char domain[5];

	cout << "enter their name\n";
	cin  >> firstName[count];
	cin  >> lastName[count];
	cout << "enter their number\n";
	cin  >> number[count];
	cout << "enter their email\n";
	cin  >> email[count];
	length=strlen(email[count]);
	if(length>=4)
	{
		for(i=0; i<4; i++)
		{
			domain[i]=email[count][length-(i+1)];
			domain[4]=NULL;
		}
	}
	if (strcmp(domain,"moc.")==0 || strcmp(domain,"gro.")==0 || strcmp(domain,"ude.")==0)
	{
	}
	else if(length <7)// for txt+@+txt+.com=7   that is the min for an email
	{
		cout << "bad email, please give me a good one! OR ELSE!!!\n";
		cin  >> email[count];
	}
	else
	{
		cout << "bad email, please give me a good one! OR ELSE!!!\n";
		cin  >> email[count];
	}
}

int delet(char firstName[][32],char number[][9],char email[][32],char lastName[][32],int count)
{
	int i;
	int j;
	int error;
	char name[32];

	cout << "what is the person's last name?\n";
	cin  >> name;
	
	for (i=0; i<count; i++)
	{
		j=i;
		if (strcmp(name,lastName[i])==0)
		{
			for (;j<count;j++)
			{
				strcpy(lastName[j],lastName[j+1]);
				strcpy(firstName[j],lastName[j+1]);
				strcpy(number[j],number[j+1]);
				strcpy(email[j],email[j+1]);
			}
			count=count-1;
			error=count;
		}
	}
	if (error != count)
	{
		cout << "ERROR!!!that person does not exist!!!";
	}
	return count;
}

void find(char firstName[][32],char number[][9],char email[][32],char lastName[][32],int count)
{
	char name[32];
	int i;
	char answer;
	cout << "what is the persons last name of whom you want to find?\n";
	cin  >> name;

	for (i=0; i<count; i++)
	{
		if(strcmp(name,lastName[i])==0)
		{
			cout << firstName[i] << " ";
			cout << lastName[i] << " ";
			cout << number[i] << " ";
			cout << email[i] << endl;
			cout << "enter y if you would like to open outlook to email them\n";
			cin  >> answer;
			if (answer == 'y' || answer == 'Y')
			{
				char cmdline[128] = "\"C:/Program Files/Microsoft Office/Office12/Outlook.exe\" /c ipm.note /m ";
				strcat(cmdline,email[i]);
				system(cmdline);
			}
		}
		else
		{
		cout << "ERROR!!! that person does not exist!\n";
		}
	}
}

void list(char firstName[][32],char number[][9],char email[][32],char lastName[][32],int count)
{
	bool voldemort=false;
	char last[32];
	char first[32];
	char phone[9];
	char mail[32];
	int i;

	
	while(!voldemort)
	{
		voldemort = true;
		for(i=0; i<count; i++)
			{
				if (strcmp(lastName[i], lastName[i+1])>0)
				{
					strcpy(last,lastName[i]);
					strcpy(lastName[i],lastName[i+1]);
					strcpy(lastName[i+1],last);

					strcpy(first,firstName[i]);
					strcpy(firstName[i],firstName[i+1]);
					strcpy(firstName[i+1],first);

					strcpy(phone,number[i]);
					strcpy(number[i],number[i+1]);
					strcpy(number[i+1],phone);

					strcpy(mail,email[i]);
					strcpy(email[i],email[i+1]);
					strcpy(email[i+1],mail);
					voldemort = false;
				}
			}
	}
	for(i=0; i<count; i++)
	{
		cout << firstName[i] << " ";
		cout << lastName[i] << " ";
		cout << number[i] << " ";
		cout << email[i] << endl;
	}
}

void save(char firstName[][32],char number[][9],char email[][32],char lastName[][32],int count)
{
	int i;
	ofstream outputFile;
	outputFile.open("book.txt");
	for (i=0; i<count; i++)
	{
		outputFile << lastName[i];
		outputFile << " ";
		outputFile << firstName[i];
		outputFile << " ";
		outputFile << number[i];
		outputFile << " ";
		outputFile << email[i];
		outputFile << "\n";
	}
	outputFile.close();
}

Recommended Answers

All 3 Replies

couple things:

You are using cstring library functions, without explicitly including the <cstring> header. If it works, hey that's fine. Just letting you know.

By putting your array counter at the beginning, you'll always skip over element[0]... I think this is why you are reading the same thing over and over whenever you try to access the first element of your arrays:

while(inFile)
	{
		count++;
		inFile >> lastName[count];
		inFile >> firstName[count];
		inFile >> phoneNumber[count];
		inFile >> email[count];
	}

I would put your counter at the end, so your first loop iteration will result in count equal to zero.

it is thanks for help. figured it out right before i read that... i feel dumb :P

I think clinton paris is onto something here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.