| | |
fstream isnt getting the data right
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 16
Reputation:
Solved Threads: 0
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
This is the file that it is reading from
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
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
•
•
•
•
Originally Posted by Narue
>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.
Hah j/k •
•
Join Date: Mar 2005
Posts: 16
Reputation:
Solved Threads: 0
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"
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...
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...
I'm here to prove you wrong.
•
•
Join Date: Mar 2005
Posts: 16
Reputation:
Solved Threads: 0
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
heres a pic
here
000013 Farsio Kottab E 90013 CA 90755 40 20.00 Last edited by Dave Sinkula; May 9th, 2005 at 7:10 pm. Reason: The/This
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Mar 2005
Posts: 16
Reputation:
Solved Threads: 0
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.
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.
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.
C++ Syntax (Toggle Plain Text)
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; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: New To Daniweb
- Next Thread: Using data i read from *.* files
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






