محمد الديب9 نشر 31 يناير أرسل تقرير نشر 31 يناير لماذا الكود الذي فوق الخط الاحمر يعطي false دائما عندما اقوم بعمل debug للكود , أفيدوني جزاكم الله خيرا 2 اقتباس
0 Mustafa Suleiman نشر 31 يناير أرسل تقرير نشر 31 يناير على الأغلب، لأن الملف الذي تحاول فتحه غير موجود، ففي السطر الذي قبله، تحاول فتح الملف "FileName" باستخدام وظيفة fstream::open(). إذا لم يكن الملف موجودًا، فسترجع الوظيفة false. في حال استمرت المشكلة أرفق الكود الكامل لتفقد المشكلة. 1 اقتباس
0 محمد الديب9 نشر 31 يناير الكاتب أرسل تقرير نشر 31 يناير الكود مرفق هنا ConsoleApplication1.sln بالمناسبة الكود يعمل في قراءة الملف ولا يعمل عند الكتابة والتعديل عليه 1 اقتباس
0 Khaled Osama3 نشر 31 يناير أرسل تقرير نشر 31 يناير الملف الذى ارسلته ناقص لا يمكن فتحه ، ارسل الكود فقط الموجود داخله. قد تبدو المشكلة انك لا ترسل المسار الخاص بالملف الذى تريد ان تقرأ منه او تكتب عليه وهذا مثال يوضح الحل ببساطة. #include <iostream> #include <fstream> using namespace std; int main() { fstream myFile; string data; myFile.open("D:\\files\\test.TXT",ios::out| ios::app); cout<<myFile.is_open(); myFile>>data; cout<<data; return 0; } اقتباس
0 محمد الديب9 نشر 31 يناير الكاتب أرسل تقرير نشر 31 يناير الكود كاملا #include <iostream> #include <fstream> #include <string> #include <vector> #include <iomanip> using namespace std; const string ClientsFileName = "clients.txt"; const string UsersFileName = "users.txt"; void ShowMainMenue(); void ShowTransactionScreen(); void Login(); struct sClient { string AccountNumber; string PinCode; string Name; string Phone; double AccountBalance; bool MarkForDelete = false; }; struct sUser { string UserName = ""; string Password = ""; string Permission = "1"; }; vector<string> SplitString(string S1, string Delim) { vector<string> vString; short pos = 0; string sWord; // define a string variable // use find() function to get the position of the delimiters while ((pos = S1.find(Delim)) != std::string::npos) { sWord = S1.substr(0, pos); // store the word if (sWord != "") { vString.push_back(sWord); } S1.erase(0, pos + Delim.length()); /* erase() until positon and move to next word. */ } if (S1 != "") { vString.push_back(S1); // it adds last word of the string. } return vString; } sClient ConvertClientLineRecord(string Line, string Seperator = "#//#") { sClient Client; vector<string> vClientData; vClientData = SplitString(Line, Seperator); Client.AccountNumber = vClientData[0]; Client.PinCode = vClientData[1]; Client.Name = vClientData[2]; Client.Phone = vClientData[3]; Client.AccountBalance = stod(vClientData[4]);//cast string to double return Client; } sUser ConvertUserLineRecord(string Line, string Seperator = "#//#") { sUser User; vector<string> vUserData; vUserData = SplitString(Line, Seperator); User.UserName = vUserData[0]; User.Password = vUserData[1]; User.Permission = vUserData[2]; return User; } string CovertClientRecordToLine(sClient Client, string Seperator = "#//#") { string stClientRecord = ""; stClientRecord += Client.AccountNumber + Seperator; stClientRecord += Client.PinCode + Seperator; stClientRecord += Client.Name + Seperator; stClientRecord += Client.Phone + Seperator; stClientRecord += to_string(Client.AccountBalance); return stClientRecord; } string CovertUserRecordToLine(sUser User, string Seperator = "#//#") { string stUserRecord = ""; stUserRecord += User.UserName + Seperator; stUserRecord += User.Password + Seperator; stUserRecord += User.Permission + Seperator; return stUserRecord; } bool ClientExistsByAccountNumber(string AccountNumber, string FileName) { vector <sClient> vClients; fstream MyFile; MyFile.open(FileName, ios::in);//read Mode if (MyFile.is_open()) { string Line; sClient Client; while (getline(MyFile, Line)) { Client = ConvertClientLineRecord(Line); if (Client.AccountNumber == AccountNumber) { MyFile.close(); return true; } vClients.push_back(Client); } MyFile.close(); } return false; } bool UserExistsByUserName(string UserName, string FileName) { vector <sUser> vUsers; fstream MyFile; MyFile.open(FileName, ios::in);//read Mode if (MyFile.is_open()) { string Line; sUser User; while (getline(MyFile, Line)) { User = ConvertUserLineRecord(Line); if (User.UserName == UserName) { MyFile.close(); return true; } vUsers.push_back(User); } MyFile.close(); } return false; } sClient ReadNewClient() { sClient Client; cout << "Enter Account Number? "; // Usage of std::ws will extract allthe whitespace character getline(cin >> ws, Client.AccountNumber); while (ClientExistsByAccountNumber(Client.AccountNumber, ClientsFileName)) { cout << "\nClient with [" << Client.AccountNumber << "] already exists, Enter another Account Number? "; getline(cin >> ws, Client.AccountNumber); } cout << "Enter PinCode? "; getline(cin, Client.PinCode); cout << "Enter Name? "; getline(cin, Client.Name); cout << "Enter Phone? "; getline(cin, Client.Phone); cout << "Enter AccountBalance? "; cin >> Client.AccountBalance; return Client; } sUser ReadNewUser() { sUser User; cout << "Enter User Name? "; // Usage of std::ws will extract allthe whitespace character getline(cin >> ws, User.UserName); while (UserExistsByUserName(User.UserName, UsersFileName)) { cout << "\nUser with [" << User.UserName << "] already exists, Enter another User Name ? "; getline(cin >> ws, User.UserName); } cout << "Enter Password? "; getline(cin, User.Password); char Choice = 'N'; cout << "Do You want to give the user full permistions ?" << endl; cin >> Choice; if (toupper(Choice) == 'Y') User.Permission = -1; return User; } vector <sClient> LoadCleintsDataFromFile(string FileName) { vector <sClient> vClients; fstream MyFile; MyFile.open(FileName, ios::in);//read Mode if (MyFile.is_open()) { string Line; sClient Client; while (getline(MyFile, Line)) { Client = ConvertClientLineRecord(Line); vClients.push_back(Client); } MyFile.close(); } return vClients; } vector <sUser> LoadUsersDataFromFile(string FileName) { vector <sUser> vUsers; fstream MyFile; MyFile.open(FileName, ios::in);//read Mode if (MyFile.is_open()) { string Line; sUser User; while (getline(MyFile, Line)) { User = ConvertUserLineRecord(Line); vUsers.push_back(User); } MyFile.close(); } return vUsers; } void PrintClientRecordLine(sClient Client) { cout << "| " << setw(15) << left << Client.AccountNumber; cout << "| " << setw(10) << left << Client.PinCode; cout << "| " << setw(40) << left << Client.Name; cout << "| " << setw(12) << left << Client.Phone; cout << "| " << setw(12) << left << Client.AccountBalance; } void PrintUserRecordLine(sUser User) { cout << "| " << setw(20) << left << User.UserName; cout << "| " << setw(10) << left << User.Password; cout << "| " << setw(20) << left << User.Permission; } void ShowAllClientsScreen() { vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName); cout << "\n\t\t\t\t\tClient List (" << vClients.size() << ") Client(s)."; cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; cout << "| " << left << setw(15) << "Accout Number"; cout << "| " << left << setw(10) << "Pin Code"; cout << "| " << left << setw(40) << "Client Name"; cout << "| " << left << setw(12) << "Phone"; cout << "| " << left << setw(12) << "Balance"; cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; if (vClients.size() == 0) cout << "\t\t\t\tNo Clients Available In the System!"; else for (sClient Client : vClients) { PrintClientRecordLine(Client); cout << endl; } cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; } void ShowAllUsersScreen() { vector <sUser> vUsers = LoadUsersDataFromFile(UsersFileName); cout << "\n\t\t\t\t\tClient List (" << vUsers.size() << ") Client(s)."; cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; cout << "| " << left << setw(20) << "UserName"; cout << "| " << left << setw(10) << "Password"; cout << "| " << left << setw(20) << "Permissions"; cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; if (vUsers.size() == 0) cout << "\t\t\t\tNo Users Available In the System!"; else for (sUser User : vUsers) { PrintUserRecordLine(User); cout << endl; } cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; } void PrintClientCard(sClient Client) { cout << "\nThe following are the client details:\n"; cout << "-----------------------------------"; cout << "\nAccout Number: " << Client.AccountNumber; cout << "\nPin Code : " << Client.PinCode; cout << "\nName : " << Client.Name; cout << "\nPhone : " << Client.Phone; cout << "\nAccount Balance: " << Client.AccountBalance; cout << "\n-----------------------------------\n"; } bool FindClientByAccountNumber(string AccountNumber, vector <sClient> vClients, sClient& Client) { for (sClient C : vClients) { if (C.AccountNumber == AccountNumber) { Client = C; return true; } } return false; } sClient ChangeClientRecord(string AccountNumber) { sClient Client; Client.AccountNumber = AccountNumber; cout << "\n\nEnter PinCode? "; getline(cin >> ws, Client.PinCode); cout << "Enter Name? "; getline(cin, Client.Name); cout << "Enter Phone? "; getline(cin, Client.Phone); cout << "Enter AccountBalance? "; cin >> Client.AccountBalance; return Client; } bool MarkClientForDeleteByAccountNumber(string AccountNumber, vector <sClient>& vClients) { for (sClient& C : vClients) { if (C.AccountNumber == AccountNumber) { C.MarkForDelete = true; return true; } } return false; } vector <sClient> SaveCleintsDataToFile(string FileName, vector <sClient> vClients) { fstream MyFile; MyFile.open(FileName, ios::out);//overwrite string DataLine; if (MyFile.is_open()) { for (sClient C : vClients) { if (C.MarkForDelete == false) { //we only write records that are not marked for delete. DataLine = CovertClientRecordToLine(C); MyFile << DataLine << endl; } } MyFile.close(); } return vClients; } void AddDataLineToFile(string FileName, string stDataLine) { fstream MyFile; MyFile.open(FileName, ios::out | ios::app); if (MyFile.is_open()) { MyFile << stDataLine << endl; MyFile.close(); } } void AddNewUser() { sUser User; User = ReadNewUser(); AddDataLineToFile(UsersFileName, CovertUserRecordToLine(User)); } void AddNewClient() { sClient Client; Client = ReadNewClient(); AddDataLineToFile(ClientsFileName, CovertClientRecordToLine(Client)); } void AddNewClients() { char AddMore = 'Y'; do { cout << "Adding New Client:\n\n"; AddNewClient(); cout << "\nClient Added Successfully, do you want to add more clients? Y/N? "; cin >> AddMore; } while (toupper(AddMore) == 'Y'); } void AddNewUsers() { char AddMore = 'Y'; do { //system("cls"); cout << "Adding New User:\n\n"; AddNewUser(); cout << "\nClient Added Successfully, do you want to add more Users? Y/N? "; cin >> AddMore; } while (toupper(AddMore) == 'Y'); } bool DeleteClientByAccountNumber(string AccountNumber, vector <sClient>& vClients) { sClient Client; char Answer = 'n'; if (FindClientByAccountNumber(AccountNumber, vClients, Client)) { PrintClientCard(Client); cout << "\n\nAre you sure you want delete this client? y/n ? "; cin >> Answer; if (Answer == 'y' || Answer == 'Y') { MarkClientForDeleteByAccountNumber(AccountNumber, vClients); SaveCleintsDataToFile(ClientsFileName, vClients); //Refresh Clients vClients = LoadCleintsDataFromFile(ClientsFileName); cout << "\n\nClient Deleted Successfully."; return true; } } else { cout << "\nClient with Account Number (" << AccountNumber << ") is Not Found!"; return false; } } bool UpdateClientByAccountNumber(string AccountNumber, vector <sClient>& vClients) { sClient Client; char Answer = 'n'; if (FindClientByAccountNumber(AccountNumber, vClients, Client)) { PrintClientCard(Client); cout << "\n\nAre you sure you want update this client? y/n ? "; cin >> Answer; if (Answer == 'y' || Answer == 'Y') { for (sClient& C : vClients) { if (C.AccountNumber == AccountNumber) { C = ChangeClientRecord(AccountNumber); break; } } SaveCleintsDataToFile(ClientsFileName, vClients); cout << "\n\nClient Updated Successfully."; return true; } } else { cout << "\nClient with Account Number (" << AccountNumber << ") is Not Found!"; return false; } } string ReadClientAccountNumber() { string AccountNumber = ""; cout << "\nPlease enter AccountNumber? "; cin >> AccountNumber; return AccountNumber; } void ShowDeleteClientScreen() { cout << "\n-----------------------------------\n"; cout << "\tDelete Client Screen"; cout << "\n-----------------------------------\n"; vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName); string AccountNumber = ReadClientAccountNumber(); DeleteClientByAccountNumber(AccountNumber, vClients); } void ShowUpdateClientScreen() { cout << "\n-----------------------------------\n"; cout << "\tUpdate Client Info Screen"; cout << "\n-----------------------------------\n"; vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName); string AccountNumber = ReadClientAccountNumber(); UpdateClientByAccountNumber(AccountNumber, vClients); } void ShowAddNewClientsScreen() { cout << "\n-----------------------------------\n"; cout << "\tAdd New Clients Screen"; cout << "\n-----------------------------------\n"; AddNewClients(); } void ShowAddNewUsersScreen() { cout << "\n-----------------------------------\n"; cout << "\tAdd New User Screen"; cout << "\n-----------------------------------\n"; AddNewUsers(); } void ShowFindClientScreen() { cout << "\n-----------------------------------\n"; cout << "\tFind Client Screen"; cout << "\n-----------------------------------\n"; vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName); sClient Client; string AccountNumber = ReadClientAccountNumber(); if (FindClientByAccountNumber(AccountNumber, vClients, Client)) PrintClientCard(Client); else cout << "\nClient with Account Number[" << AccountNumber << "] is not found!"; } void ShowEndScreen() { cout << "\n-----------------------------------\n"; cout << "\tProgram Ends :-)"; cout << "\n-----------------------------------\n"; } enum enMainMenueOptions { eListClients = 1, eAddNewClient = 2, eDeleteClient = 3, eUpdateClient = 4, eFindClient = 5, eTransactions = 6, eManageUsers = 7, eLogOut = 8 }; enum enTransactionMenueOptions { eDeposite = 1, eWithdrawal = 2, eTotalBalances = 3, eMainMenue = 4 }; enum enManageUsersMenueOptions { eUsersList = 1, eAddUser = 2, eDeleteUser = 3, eUpdateUser = 4, eFindUser = 5, eMainMenueManage = 6 }; void GoBackToMainMenue() { cout << "\n\nPress any key to go back to Main Menue..."; system("pause>0"); ShowMainMenue(); } void GoBackToTransactionMenue() { cout << "\n\nPress any key to go back to Transcraption menue..."; system("pause>0"); ShowTransactionScreen(); } short ReadMainMenueOption() { cout << "Choose what do you want to do? [1 to 8]? "; short Choice = 0; cin >> Choice; return Choice; } short ReadTransactionMenueOption() { cout << "Choose what do you want to do? [1 to 4]? "; short Choice = 0; cin >> Choice; return Choice; } void DepositBalanceToClientByAccountNumber(string AccountNumber, double Amount, vector <sClient> vClients) { char Answer = 'n'; cout << "Are you sure you wanna perform this transaction ? Y/y "; cin >> Answer; if (Answer == 'Y' || Answer == 'y') { for (sClient& C : vClients) { if (C.AccountNumber == AccountNumber) { C.AccountBalance += Amount; SaveCleintsDataToFile(ClientsFileName, vClients); cout << "\n\nDone Successfully, new Balance is: " << C.AccountBalance; } } } } void ShowDepositeScreen() { cout << "==================\n"; cout << "Deposite Screen\n"; cout << "==================\n"; sClient Client; vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName); string AccountNumber = ReadClientAccountNumber(); while (!FindClientByAccountNumber(AccountNumber, vClients, Client)) { cout << "Client with number (" << AccountNumber << ") is not exist .\n"; AccountNumber = ReadClientAccountNumber(); } PrintClientCard(Client); double Amount; cout << "Please enter deposit amount? "; cin >> Amount; DepositBalanceToClientByAccountNumber(AccountNumber, Amount, vClients); } void ShowWithDrawalScreen() { cout << "==================\n"; cout << "WithDrawal Screen\n"; cout << "==================\n"; sClient Client; vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName); string AccountNumber = ReadClientAccountNumber(); while (!FindClientByAccountNumber(AccountNumber, vClients, Client)) { cout << "Client with number (" << AccountNumber << ") is not exist .\n"; AccountNumber = ReadClientAccountNumber(); } PrintClientCard(Client); double Amount; cout << "Please enter deposit amount? "; cin >> Amount; while (Amount > Client.AccountBalance) { cout << "Amount Exceeds the balance, you can withdraw up to " << Client.AccountBalance << endl; cout << "Please,Enter another Amount: "; cin >> Amount; } DepositBalanceToClientByAccountNumber(AccountNumber, Amount * -1, vClients); } void PrintClientRecordBalanceLine(sClient Client) { cout << "| " << setw(15) << left << Client.AccountNumber; cout << "| " << setw(40) << left << Client.Name; cout << "| " << setw(12) << left << Client.AccountBalance; cout << endl; } void ShowTotalBalances() { vector <sClient> vClients = LoadCleintsDataFromFile(ClientsFileName); cout << "\n\t\t\t\t\Balance List (" << vClients.size() << ") Client(s)."; cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; cout << "| " << left << setw(15) << "Accout Number"; cout << "| " << left << setw(40) << "Client Name"; cout << "| " << left << setw(12) << "Balance"; cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; double TotalBalnces = 0; if (vClients.size() == 0) cout << "\t\tNo Clients Available In the System!"; else { for (sClient Client : vClients) { PrintClientRecordBalanceLine(Client); TotalBalnces += Client.AccountBalance; } } cout << "\n_______________________________________________________"; cout << "_________________________________________\n" << endl; cout << "\t\t\t\tTotal Balances = " << TotalBalnces << endl; } void PerformTransactionMenueOption(enTransactionMenueOptions TransactionMenueOption) { switch (TransactionMenueOption) { case enTransactionMenueOptions::eDeposite: system("cls"); ShowDepositeScreen(); GoBackToTransactionMenue(); break; case enTransactionMenueOptions::eWithdrawal: system("cls"); ShowWithDrawalScreen(); GoBackToTransactionMenue(); break; case enTransactionMenueOptions::eTotalBalances: system("cls"); ShowTotalBalances(); GoBackToTransactionMenue(); break; case enTransactionMenueOptions::eMainMenue: GoBackToMainMenue(); } } void ShowTransactionScreen() { system("cls"); cout << "===============================\n"; cout << "[1] Deposite.\n"; cout << "[2] Withdrawal.\n"; cout << "[3] Total Balances.\n"; cout << "[4] Main Menue.\n"; cout << "===============================\n"; PerformTransactionMenueOption((enTransactionMenueOptions)ReadTransactionMenueOption()); } short ReadManageUsersMenueOptions() { cout << "Choose what do you want to do? [1 to 6]? "; short Choice = 0; cin >> Choice; return Choice; } void PerformMangeUsersMenueOperations(enManageUsersMenueOptions MangeUsersOption) { switch (MangeUsersOption) { case enManageUsersMenueOptions::eUsersList: system("cls"); ShowAllUsersScreen(); GoBackToMainMenue(); break; case enManageUsersMenueOptions::eAddUser: system("cls"); ShowAddNewUsersScreen(); GoBackToMainMenue(); break; //case eDeleteUser: //break; //case eUpdateUser: //break; //case eFindUser: // break; case eMainMenueManage: system("cls"); ShowMainMenue(); break; } } void ShowManageUsersScreen() { system("cls"); cout << "===========================================\n"; cout << "\t\tManage Users Menue Screen\n"; cout << "===========================================\n"; cout << "[1] Users List\n"; cout << "[2] Add New User\n"; cout << "[3] Delete User\n"; cout << "[4] Update User.\n"; cout << "[5] Find User.\n"; cout << "[6] Main Menue.\n"; cout << "===========================================\n"; PerformMangeUsersMenueOperations((enManageUsersMenueOptions)ReadManageUsersMenueOptions()); } void PerfromMainMenueOption(enMainMenueOptions MainMenueOption) { switch (MainMenueOption) { case enMainMenueOptions::eListClients: { system("cls"); ShowAllClientsScreen(); GoBackToMainMenue(); break; } case enMainMenueOptions::eAddNewClient: system("cls"); ShowAddNewClientsScreen(); GoBackToMainMenue(); break; case enMainMenueOptions::eDeleteClient: system("cls"); ShowDeleteClientScreen(); GoBackToMainMenue(); break; case enMainMenueOptions::eUpdateClient: system("cls"); ShowUpdateClientScreen(); GoBackToMainMenue(); break; case enMainMenueOptions::eFindClient: system("cls"); ShowFindClientScreen(); GoBackToMainMenue(); break; case enMainMenueOptions::eTransactions: system("cls"); ShowTransactionScreen(); break; case enMainMenueOptions::eManageUsers: system("cls"); ShowManageUsersScreen(); break; case enMainMenueOptions::eLogOut: system("cls"); Login(); break; } } void ShowMainMenue() { system("cls"); cout << "===========================================\n"; cout << "\t\tMain Menue Screen\n"; cout << "===========================================\n"; cout << "\t[1] Show Client List.\n"; cout << "\t[2] Add New Client.\n"; cout << "\t[3] Delete Client.\n"; cout << "\t[4] Update Client Info.\n"; cout << "\t[5] Find Client.\n"; cout << "\t[6] Transactions.\n"; cout << "\t[7] Manage users\n"; cout << "\t[8] Log out\n"; cout << "===========================================\n"; PerfromMainMenueOption((enMainMenueOptions)ReadMainMenueOption()); } void ShowLoginScreen() { cout << "-----------------------------------------" << endl; cout << "\t\tLogin Screen" << endl; cout << "-----------------------------------------" << endl; } sUser ReadUserInfo() { sUser User; cout << "username "; cin >> User.UserName; cout << "password "; cin >> User.Password; return User; } bool CheckUserInfo(sUser User) { vector <sUser> vUsers = LoadUsersDataFromFile(UsersFileName); for (sUser& Us : vUsers) { if (Us.UserName == User.UserName && Us.Password == User.Password) return true; } return false; } void Login() { ShowLoginScreen(); sUser User = ReadUserInfo(); while (!CheckUserInfo(User)) { cout << "Invalid username/password ." << endl; User = ReadUserInfo(); } ShowMainMenue(); } int main() { Login(); system("pause>0"); return 0; } اقتباس
0 محمد الديب9 نشر 1 فبراير الكاتب أرسل تقرير نشر 1 فبراير يعمل مع القراءة ولا يعمل مع التعديل عليه اقتباس
السؤال
محمد الديب9
لماذا الكود الذي فوق الخط الاحمر يعطي false دائما عندما اقوم بعمل debug للكود , أفيدوني جزاكم الله خيرا
7 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.