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(&currentTime);
	
	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(&currentTime))<< 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

>It is skipping some of the datas.
Which parts of the data are being skipped? Help us to help you by narrowing down the problem as much as possible. Otherwise we'll just ignore you.

>It is skipping some of the datas.
Which parts of the data are being skipped? Help us to help you by narrowing down the problem as much as possible. Otherwise we'll just ignore you.

Or talk about you on IRC :) Hah j/k

I'm sorry for being dimwitted. Ok the first problem is that when I go in to my menu to choose read option it would skip the 0's in the first column for example
000001 Moshiur Ahmed I CA 90805 40 12.50
fstream would get only
1 Moshiur Ahmed I CA 90805 40 12.50.

My second problem is that fstream would only read and get up to or so
1 Moshiur Ahmed I CA 90805 40 12.50
2 Tasmia Amaat J CA 90706 35 25.60
3 Victor Barboza K CA 90706 42 30.00
4 Carlos Detorres I CA 90804 50 20.45
5 Tyree Ek O CA 90755 60 15.60
6 Amine El-Maziati M CA 90815 20 9.75
7 Loubna El-Maziati M CA 90815 25 10.50
8 Theresa Eyssallenne L CA 90715 40 20.00
9 Craig Griffith I CA 90804 40 45.00
10 Rodney Hallett H CA 90808 43 25.25
11 Andrew Hegstrom P CA 90808 45 13.50
12 Corey James D CA 90815 40 20.00


After this it would replace everything with 0's. Is it becasue there is something wrong with how I set it up or maybe the arrays are wrong.

Oh if you would like to run my program the ID code that it will asks for is "lbcc"

>it would skip the 0's in the first column
Well that's easy. Leading zeros are meaningless on an integer, so they're ignored. If you want the whole string, read it as a string instead of an integer.

>After this it would replace everything with 0's.
Well, your array is of size 100 and there are only 30 records in the file you gave us...

<unconstructive>
haha
</unconstructive>

Yeah I know there is only 30 record but Its only reading 13 records. At first it was at a array of 30 but it would read up to 13 records and still had 0's so change it to 100 but still the samething
heres a pic
here

000013 Farsio Kottab E 90013 CA 90755 40 20.00

This gets you into a fail state.

ohhhh. Im an idiot. Thanks

Hi everyone,
I ran into another problem. Im trying to sort the employees records by using the bubble sort with arrays and I just got the names arrays down but how would I get the rest of the information of that employees to go next to the names.

void sortLastname(int a)
{
	string temp = string();
	char pick = char();

	for(int i=0; i<30; i++) 
	{
		for (int j=0; j<30-i; j++)
		{
			if (lname[j] > lname[j+1])	// compare the two neighbors 
			{  
				temp = lname[j];         
				lname[j] = lname[j+1];	// swap lname[j] and lname[j+1]      
				lname[j+1] = temp;
			}	
		}
		cout << temp <<endl;
		
		//cout << setw(5) << left <<  ID[i] << setw(10) << left << fname[i] << setw(12) << lname[i] << setw(2) << middlei[i] << setw(7) << state[i] << setw(5) << right << zip[i] << setw(6) << right << hrWork[i] << setw(8) << right << ratePhour[i]  << endl;
	}

Would I have to use another bubble sort in the bubble sort? Why is it that I cant seem to edit my first post. I like to paste my updated code.

When you swap the name array, you'll have to swap all of the other arrays along with it, since each array is a unique object. A good alternative is to stuff everything in an structure and then sort an array of structs:

struct record {
  string name;
  // ...
} rec[N];

That would save you a lot of work because you can treat a record as a single unit rather than an index in a bunch of arrays.

But I think I need to use the arrays because there are 30 records. I need to sort the records by names. I got the Last name down but what should I do to get that person other records along with his last name?

I try this but it would'nt display the first name

void sortLastname(int a)
{
	string temp = string();
	string temp2 = string();
	char pick = char();

	for(int i=0; i<30; i++) 
	{
		for (int j=0; j<30-i; j++)
		{
			if (lname[j] > lname[j+1])	// compare the two neighbors 
			{  
				temp = lname[j];         
				lname[j] = lname[j+1];	// swap lname[j] and lname[j+1]      
				lname[j+1] = temp;
				[B]for(int q=0; q<30; q++) 
				{
					for (int w=0; w<30-i; w++)
					{
						if (fname[w] > fname[w+1])	// compare the two neighbors 
						{  
						temp = fname[j];         
						fname[w] = fname[w+1];	// swap fname[w] and fname[w+1]      
						fname[w+1] = temp2;
						}
					}
				}[/B]
			}	
		}
		cout << temp << " " <<temp2 <<endl;
		
		//cout << setw(5) << left <<  ID[i] << setw(10) << left << fname[i] << setw(12) << lname[i] << setw(2) << middlei[i] << setw(7) << state[i] << setw(5) << right << zip[i] << setw(6) << right << hrWork[i] << setw(8) << right << ratePhour[i]  << endl;
	}

You're sorting by name, and name only. Just because a name goes in one spot doesn't mean the corresponding data goes in the same spot if you sort it too. This is basically what you're doing:

#include <algorithm>
#include <iostream>
#include <iterator>

template <typename T>
void bubble(T list[], int sz)
{
  for (int i = 0; i < sz; i++) {
    for (int j = i; j < sz; j++) {
      if (list[j] < list[i])
        std::swap(list[i], list[j]);
    }
  }
}

int main()
{
  int a[] = {9,8,7,6,5,4,3,2,1,0};
  int b[] = {6,7,5,8,9,0,4,3,2,1};

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';

  bubble(a, 10);

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
}

To get it to work, you need to pass the other arrays and swap them too, but only use name in the test:

#include <algorithm>
#include <iostream>
#include <iterator>

template <typename T>
void bubble(T list1[], T list2[], int sz)
{
  for (int i = 0; i < sz; i++) {
    for (int j = i; j < sz; j++) {
      if (list1[j] < list1[i]) {
        std::swap(list1[i], list1[j]);
        std::swap(list2[i], list2[j]);
      }
    }
  }
}

int main()
{
  int a[] = {9,8,7,6,5,4,3,2,1,0};
  int b[] = {6,7,5,8,9,0,4,3,2,1};

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';

  bubble(a, b, 10);

  copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
}

Or you can lump everything into a record structure and avoid that hassle and keep everything together automatically:

#include <algorithm>
#include <iostream>
#include <iterator>

struct record {
  int a;
  int b;
};

void bubble(record list[], int sz)
{
  for (int i = 0; i < sz; i++) {
    for (int j = i; j < sz; j++) {
      if (list[j].a < list[i].a)
        std::swap(list[i], list[j]);
    }
  }
}

int main()
{
  record list[10] = {
    9,6,8,7,7,5,6,8,5,9,4,0,3,4,2,3,1,2,0,1
  };

  for (int i = 0; i < 10; i++)
    std::cout << '(' << list[i].a << ',' << list[i].b << ')' << '\n';

  std::cout << '\n';
  bubble(list, 10);

  for (int i = 0; i < 10; i++)
    std::cout << '(' << list[i].a << ',' << list[i].b << ')' << '\n';
}

Ah Thanks you help me a lot!
Thank you

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.