Plz этот код не может записывать данные в файл txt на рабочем столе и totalsalary дает мне неверное значение
Эй, все
этот код управление данными сотрудника (дата приема на работу,нет оклада,итого-зарплата)
файл, который я пишу, ничего не отображает?!
// This program manages employee data // It reads a file of employee information and offeres // several difference sorting options #include <iostream> #include <fstream> #include <iomanip> using namespace std; void GetList(ifstream& fileIn, int hiredate[], int employee[], int salary[], int& totalRecs); void menuaction(int hiredate[], int employee[], int salary[], int totalRecs); void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs); void sortarraybyID(int hiredate[], int employee[], int salary[], int totalRecs); void showarray(int hiredate[], int employee[], int salary[], int totalRec); int sum(int salary[], int totalRecs); void Insert(int hiredate[], int employee[], int salary[], int& totalRecs); void Delete(int hiredate[], int employee[], int salary[], int& totalRecs); const int MAX_LIST_SIZE = 25; int main() { // Array of list elements int hiredate[MAX_LIST_SIZE]; int employee[MAX_LIST_SIZE]; int salary[MAX_LIST_SIZE]; int totalRecs = 0; // Set up file ifstream fileIn; string inFileName = "C:\\Users\\MOHAMED\\Desktop\\xx.txt"; fileIn.open(inFileName); //Open The File if (fileIn.fail()) // Test for file existence { cout << "Problem opening file"; exit(-1); } ifstream theData; GetList(fileIn, hiredate, employee, salary, totalRecs); menuaction(hiredate, employee, salary, totalRecs); } // This function reads integers from a file and stores the values in // an array. It returns the loaded array and the number of elements // in the array void GetList(ifstream& InFile, int hiredate[], int employee[], int salary[], int& totalRecs) { int i = 0; // Priming read InFile >> hiredate[i] >> employee[i] >> salary[i]; while (!InFile.eof()) { i++; // Add one to pointer // continuation reads InFile >> hiredate[i] >> employee[i] >> salary[i]; } totalRecs = i; } // This Fuction is the Menu Action void menuaction(int hiredate[], int employee[], int salary[], int totalRecs) { int choice, Sum; do { cout << "\n\t\tEmployee Data Menu\n\n"; cout << "1. List by hiredate\n"; cout << "2. List by employee number\n"; cout << "3. Write total of salaries\n"; cout << "4. Add employee\n"; cout << "5. Delete employee\n"; cout << "6. TERMINATE SESSION\n"; cout << "Enter your choice: "; cin >> choice; if (choice >= 1 && choice <= 5) { switch (choice) { case 1: DisplayList(hiredate, employee, salary, totalRecs); break; case 2: sortarraybyID(hiredate, employee, salary, totalRecs); showarray(hiredate, employee, salary, totalRecs); break; case 3: Sum = sum(salary, totalRecs); cout << "Sum = " << Sum << endl; break; case 4: Insert(hiredate, employee, salary, totalRecs); DisplayList(hiredate, employee, salary, totalRecs); break; case 5: Delete(hiredate, employee, salary, totalRecs); DisplayList(hiredate, employee, salary, totalRecs); break; } } else if (choice != 6) { cout << "The valid choices are 1 through 6.\n"; cout << "Please try again.\n"; } } while (choice != 6); } //This function writes a list to console output void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs) { for (int i = 0; i < totalRecs; i++) cout << hiredate[i] << " " << employee[i] << " " << salary[i] << " " << endl; cout << endl; } // This functions Lists the employees by number using sorting void sortarraybyID(int hiredate[], int employee[], int salary[], int totalRecs) { int temp, temp2, temp3, end; for (end = totalRecs - 1; end >= 0; end--) { for (int i = 0; i < end; i++) { if (employee[i] > employee[i + 1]) { temp = employee[i]; temp2 = hiredate[i]; temp3 = salary[i]; employee[i] = employee[i + 1]; hiredate[i] = hiredate[i + 1]; salary[i] = salary[i + 1]; employee[i + 1] = temp; hiredate[i + 1] = temp2; salary[i + 1] = temp3; } } } } void showarray(int hiredate[], int employee[], int salary[], int totalRecs) { for (int i = 0; i < totalRecs; i++) cout << hiredate[i] << " " << employee[i] << " " << salary[i] << endl; } // This functions writes the total of the salaries int sum(int salary[], int totalRecs) { int sum = 0; for (int i = 0; i <= totalRecs; i++) sum += salary[i]; return sum; } // This function receives an integer, an array containing // an unordered list, and the size of the list. It inserts // a new integer item at the end of the list void Insert(int hiredate[], int employee[], int salary[], int& totalRecs) { int newint, newint2, newint3; cout << "Insert New Employee HIREDATE:" << endl; cin >> newint; hiredate[totalRecs] = newint; cout << "Insert New Employee ID NUMBER:" << endl; cin >> newint2; employee[totalRecs] = newint2; cout << "Insert New Employee's Salary:" << endl; cin >> newint3; salary[totalRecs] = newint3; totalRecs++; // Increment size of list } // This function receives an integer, an array containing // an unordered list, and the size of the list. It locates // and deletes the integer fromt he list void Delete(int hiredate[], int employee[], int salary[], int& totalRecs) { int empnum = 0, ptr = 0; cout << " Which employee do you wish to delete" << endl; cin >> empnum; // Scan list for deletion target while (empnum != employee[ptr] && ptr < totalRecs) ptr++; if (ptr < totalRecs) // If target found, then { employee[ptr] = employee[totalRecs - 1]; // Last list item to overwrite target hiredate[ptr] = hiredate[totalRecs - 1]; salary[ptr] = salary[totalRecs - 1]; totalRecs--; // Decrement size of list } } What I have tried: i tried to edit path but fail! plz if someone try to help me
[no name]
Научитесь пользоваться отладчиком.
Philippe Mori
А также писать код по маленькой порции за раз и тестировать его.
Я не вижу кода, который записывается в файл. Где находится такой код, чего вы ожидаете и в каком файле?
mhassan083
это досье сэр
строка inFileName = "C:\\Users\\MOHAMED\\Desktop\\xx.txt";
fileIn. open(inFileName);