I'm running into to this problem where the fstream is suppose to read data off a file to store in arrays. I cant continue with my project until the program reads it right. It is skipping some of the datas. Can I get a little help? Thanks in advance
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <iomanip>
#include <ctime>
using namespace std;
void mainMenu();
void readRecords();
void saveRecords();
void displayRecords();
int choice;
int takingPass(string);
int i;
int pass1;
char ch[13];
void wrong();
int ID[100];
string fname[100];
string lname[100]={string()};
char middlei[100]={char()};
string state[100]={string()};
int zip[100]={};
double hrWork[100]={};
double ratePhour[100]={};
void header()
{
cout << "Employee Records System (ERS)\n";
cout << "Long Beach City College\n";
cout << "4901 E. Carson Street\n";
cout << "Long Beach, CA 90808\n";
}
void IDcode()
{
string user;
cout << setw(10) << "\t\t ****************************************\n";
cout << setw(10) << "\t\t *Please Log-in before using application*\n";
cout << setw(10) << "\t\t ****************************************\n";
cout << "Please Enter Your User Name. "; // asking for user name
cin >> user;
cout << "Please Enter Your User ID Code. "; // asking for password
for (i=0; i<13; i++) //user can input max 13 character
{
ch[i] = getch(); // take in passwore
cout << "*"; //display * when password is input
if (ch[i]==13) //when 13 keys are enter
{
takingPass(user); // go to function takingPass
}
}
}
int takingPass(string name)
{
time_t currentTime;
time(¤tTime);
ofstream out;
out.open("c:\\UserLog.txt", ios::app); // file user login
ch[i] = 0;
pass1 = stricmp(ch,"lbcc"); //compare with name "lbcc" not sure what stricmp does(my cousin told me to use it)
if (pass1 == 0) // case insensitive
{
cout <<"\nID CODE CONFIRM \n";
out << name << " " << asctime(localtime(¤tTime))<< endl; // out put user login time and date
system("cls");
out.close();
mainMenu();
}
else
{
wrong(); // go wrong to display message to start over
}
return 0;
}
void wrong() //if password is not correct
{
cin.clear(); // clear cin
cout <<"\nUNKNOWN ID CODE\n";
cout<< endl;
system("cls"); // clear screen
IDcode();
}
int main()
{
IDcode(); // get password when program runs
}
void mainMenu() // Menu after correct password is enter
{
header();
cout << " Main Menu\n";
cout << "============\n";
cout << "1. Read Employee Records\n";
cout << "2. Save Processed Records - Files\n";
cout << "3. Display Records\n";
cout << "4. Search Records\n";
cout << "5. Sort Records \n";
cout << "6. Exit\n";
cin >> choice;
if (choice == 1) // go to readRecords
{
readRecords();
}
else if (choice == 2) // go to saveRecords
{
saveRecords();
}
else if (choice == 3) // go to displayRecords
{
displayRecords();
}
/*else if (choice == 4) // go to searchRecords
{
searchRecords();
}
else if (choice == 5) // go to sortRecords
{
sortRecords();
}*/
else if (choice == 6) // exit when 6 is enter
{
system("cls");
cout << "Logged Out"<<endl;
cout << "Press any key to close screen" <<endl;
exit(1);
}
else // when other than 1-6 is enter
{
cout << "Invalid choice";
system("cls");
mainMenu();
}
}
void readRecords()
{
ifstream inFile;
ofstream outFile;
inFile.open("c:\\FinalData.txt");
outFile.open("c:\\outData.txt");
for (int a=0; a<100; a++)
{
inFile >> ID[a] >> fname[a] >> lname[a] >> middlei[a] >> state[a] >> zip[a] >> hrWork[a] >> ratePhour[a];
cout << ID[a]<< " " << fname[a]<< endl;
}
//cout << "Records have been read" << endl;
//system("cls");
//displayRecords();
mainMenu();
}
void saveRecords()
{
/*
int ID[100]={};
string fname[100]={string()};
string lname[100]={string()};
char middlei[100]={char()};
string state[100]={string()};
int zip[100]={};
double hrWork[100]={};
double ratePhour[100]={};
*/
ifstream inFile;
ofstream outFile;
inFile.open("c:\\FinalData.txt");
outFile.open("c:\\outData.txt");
for (int a=0; a<30; a++)
{
//inFile >> ID[a] >> fname[a] >> lname[a] >> middlei[a] >> state[a] >> zip[a] >> hrWork[a] >> ratePhour[a];
outFile << ID[a]<< " " << fname[a] << " " << lname[a] << " " << middlei[a] << " " << state[a] << " " << zip[a] << " " << hrWork[a] << " " << ratePhour[a]<<endl;
}
outFile.close();
cout << "Records saved" << endl;
system("cls");
mainMenu();
}
void displayRecords()
{
/* int display;
if (display == 1)
{
findLowest();
}
else if (display == 2)
{
findHighiest();
}
else if (display == 3)
{
displayTotals();
}
/*else if (display == 4)
{
allRecords();
}
else if (display == 5)
{
DisplayMenu();
}
else if (display == 6) //
{
system("cls");
exit(1);
}
else
{
cout << "Invalid choice";
system("cls");
mainMenu();
} */
}
This is the file that it is reading from
000001 Moshiur Ahmed I CA 90805 40 12.50
000002 Tasmia Amaat J CA 90706 35 25.60
000003 Victor Barboza K CA 90706 42 30.00
000004 Carlos Detorres I CA 90804 50 20.45
000005 Tyree Ek O CA 90755 60 15.60
000006 Amine El-Maziati M CA 90815 20 9.75
000007 Loubna El-Maziati M CA 90815 25 10.50
000008 Theresa Eyssallenne L CA 90715 40 20.00
000009 Craig Griffith I CA 90804 40 45.00
000010 Rodney Hallett H CA 90808 43 25.25
000011 Andrew Hegstrom P CA 90808 45 13.50
000012 Corey James D CA 90815 40 20.00
000013 Farsio Kottab E 90013 CA 90755 40 20.00
000014 Peter Lon W CA 90755 40 60.00
000015 Joseph Lopes A CA 90808 50 100.00
000016 Arley Lozada O CA 90280 50 30.40
000017 Ana Moreno W CA 90805 55 14.50
000018 Chris Myers Q CA 90802 57 30.00
000019 Thanakom Paiboolsilp U CA 90805 56 45.75
000020 Leonardo Priego P CA 90703 77 23.00
000021 Evel Sanchez Q CA 90806 48 12.50
000022 Ruben Santacruz S CA 90813 40 20.00
000023 John Ung R CA 90806 40 25.00
000024 Fady Attia P CA 90706 40 28.00
000025 Sopha Dip I CA 90713 40 28.00
000026 Ricky Reed A CA 90221 40 11.50
000027 Julio Umana J CA 90221 40 60.00
000028 Rin Ran E CA 90808 47 22.45
000029 Faycal Ferhat Q CA 90814 25 65.00
000030 Habtom Tesfazghi P CA 90808 40 35.00