| | |
helpppppp me with this program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 2
Reputation:
Solved Threads: 0
// I want to dislpay my cout in an acsending order by ssn
// here is the program
// here is the program
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <string> #include <fstream> #include <math.h> #include <ctype.h> //Ebrahim Sharif //ECE 370 //1-16-2008 using namespace std; void getPersonalInfo();//function prototypes int Str2Int(string InpStr); long Str2Long(string); void lookupPerson( char ); struct personalInfo { string name; int Age; long ssn; string Address; }; ifstream readfile; string readfilename = "a1.txt";//read in file personalInfo person[12]; int NumberOfPeople; int main() { cout<<"*********Assinment number one******"<<endl<<endl; int i,j,; bool stoploop; char lastInitial; char letter; //Step 1: Openning files readfile.open(readfilename.c_str()); if (readfile.fail())//checking if file exists { cout<<"\n chech and make sure that there is an existing input file."<<endl; system ("PAUSE"); return 0; } else { cout<<"\nfile is found and was opened"<<endl; cout<<" "<<endl; } //Step 2: finction 1 to read files getPersonalInfo(); //Step 3: cout all persons Info for (j=0;j<NumberOfPeople ; j++) { cout << person[j].name <<endl; cout << person[j].Age <<endl; cout << person[j].ssn<<endl; cout << person[j].Address << endl; cout << endl; } //Step 4: looking up or searching persons stoploop = false; do { cout<<"********** search center ***********"<<endl<<endl; cout << "Enter Last Name Initial to look up person : "; cin>>lastInitial; lookupPerson(lastInitial); cout << "Do you want to search again press any key otherwise 'N' or 'n' to quit : "; cin>>letter; if(letter == 'N' || letter == 'n') { stoploop = true;//stop searching } else { stoploop = false; //keep asking for search } }while(!stoploop); //Step : Close Files readfile.close(); system ("PAUSE"); return 0; } void getPersonalInfo() { int i; string temp; //temperary memory for convertion string space; i=0;//First Person (0th element) while (readfile) { getline(readfile,person[i].name);//get person's name getline(readfile,temp);//get age and convert to int person[i].Age = Str2Int(temp); getline(readfile,temp);//get ssn and conveert to long person[i].ssn = Str2Long(temp); getline(readfile,person[i].Address); //get persons sddress getline(readfile,space); // skip line i++;//loop for next person } NumberOfPeople = i; } int Str2Int(string InpStr) { char *str_ptr; int Strlength; int Outputage; int i; Strlength = InpStr.length();//get Length of the string str_ptr = new char[Strlength]; // allocate memory needed for the input string for (i = 0; i < Strlength; i++)//copy string to a poiunter for conversion need str_ptr[i] = InpStr[i]; Outputage = atoi (str_ptr);//(alphabetic to integer) return (Outputage); } long Str2Long(string InpStr) { char *str_ptr; int Strlength; long outputssn; int i; Strlength = InpStr.length(); str_ptr = new char[Strlength]; for (i = 0; i < Strlength; i++) { str_ptr[i] = InpStr[i]; } outputssn = atol(str_ptr); return (outputssn); } void lookupPerson(char lastInitial) { int j; bool PersonFound ; for (j=0;j<NumberOfPeople ; j++) { if(toupper(person[j].name[0]) == toupper(lastInitial)) { //display on screen cout << person[j].name <<endl; cout << person[j].Age <<endl; cout << person[j].ssn <<endl; cout << person[j].Address << endl; cout << endl; PersonFound = true; } } if(PersonFound == false) { cout<<"No one is found with a Last name initial '"<<lastInitial<<"'."<<endl<<endl; } }
Last edited by Narue; Jan 16th, 2008 at 9:35 am. Reason: Added code tags
![]() |
Other Threads in the C++ Forum
- Previous Thread: C and C++ Timesaving Tips
- Next Thread: Dangling Pointers - anyway to check?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






