C++ help?! code is provided!?
I am trying to create a program that stores contacts and enables you to search through them.
I first needed to create a txt file and store the numbers and contacts as well as house number work number fax number adress etc….
we get to store as many as 200
Heres the code. This is a beginners course so detailed explanations I would greatttllly appreciate it guys!!
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
struct contact
{ string lastName;
string firstName;
string homePhone;
string cellPhone;
string officePhone;
string pager;
string faxNumber;
};
string LastSearchName;
int choice;
int i=0;
ifstream inFile;
ofstream fout;
contact List[200];
string searchname;
int numbersearch;
cout<<"please enter the name you want for your file"<<endl;
inFile.open("filename.txt");
if (!inFile) {
cerr << "Unable to open file datafile.txt";
}
cout<<endl;
while(!inFile.eof())
{
inFile >> List[i].lastName;
inFile >> List[i].firstName;
inFile >> List[i].homePhone;
inFile >> List[i].cellPhone;
inFile >> List[i].officePhone;
inFile >> List[i].pager;
inFile >> List[i].faxNumber;
}
i=0;
cout<<"Press 0 to search by Name, Press any other number to search by Home number"<<endl;
cin>>choice;
if (choice==0)
{
cout<<"please enter the Last Name of the person you are trying to get contact of:"<<endl;
cin>>searchname;
cout<<endl;
i=0;
while (searchname!=LastSearchName)
{ LastSearchName=List[i++].lastName;
}
cout<<List[i].lastName<<" , "<<List[i].firstName<<endl;
cout<<"Home Phone: "<<List[i].homePhone<<endl;
cout<<"Office Phone: "<<List[i].officePhone<<endl;
cout<<"Pager: "<<List[i].pager<<endl;
cout<<"Fax Number: "<<List[i].faxNumber<<endl;
cout<<endl;
}
else
{cout<<"nevermind";}
inFile.close();
system ("PAUSE");
}
For some reason when I run the program I cannot get it to search through the names to get to the contact information for a specific person….ANY HELP GUYS? oh and can you guys help me in giving the user the ability to name the file its stored in??? im having problems with that… please very detailed results again I am a beginner
Tagged with: beginners course • cerr • contact list • detailed explanations • fax number • fstream • iomanip • iostream • lt • numbersearch • quot quot • searchname • struct
Filed under: Fax Number Search
I had the same problem.
C++ inputs everything from cin>> as a character array. This means that you have to turn it into a string before you can compare it to the stored name.
Since you are trying to compare string to character array, this will not work.
Instead, you need to create a strcpy loop that copies each character from the array into a string, one at a time. I dont quite remember the exact way strcpy works though, but it think its like this:
strcpy(&source[array_index], output);
You would loop this to get your string. Look up strcpy and see what it does.
As for working with filenames, im more of a sockets/networking guy myself, and have never used exterior file databases before. Good luck!
I hope this helps, add more info if it doesn’t.
Are you incrementing "i" in the while loop where you store the values?
Also, notice what happens if ‘searchname’ is blank or if it is not in the List array?
In order to accept the filename, you could do it via a "cin" statement or a program argument via argc, argv. Please read up on these.
Just so you know, since you are a beginner, please do the coding in small steps at a time and use ‘cout’ statements or a debugger to check values being stored.