diff --git a/Lab_02/01_temperature_converter.cpp b/Lab_02/01_temperature_converter.cpp index 76eeda8..241576d 100644 --- a/Lab_02/01_temperature_converter.cpp +++ b/Lab_02/01_temperature_converter.cpp @@ -1,26 +1,49 @@ -// C++ program that converts between Celsius and Fahrenheit temperatures based on user input. - #include +#include +#include +#include +#include using namespace std; int main() { - float temp; - char unit; + float temp = 0.0f; // PRECOGS_FIX: initialize to avoid use of indeterminate value + char unit = '\0'; + + cout << "Enter the temperature: "; + string tempStr; + if (!getline(cin, tempStr)) { + cerr << "Failed to read temperature input." << endl; + return 1; + } - cout << "Enter the temperature: "; - cin >> temp; + // Validate and parse temperature string safely + { + stringstream ss(tempStr); + if (!(ss >> temp) || !(ss.eof())) { + cerr << "Invalid temperature. Please enter a numeric value." << endl; + return 1; + } + } - cout << "Enter the unit (C/F): "; - cin >> unit; + cout << "Enter the unit (C/F): "; + string unitStr; + if (!getline(cin, unitStr) || unitStr.empty()) { + cerr << "Failed to read unit input." << endl; + return 1; + } - if (unit == 'C') { - cout << "The temperature in Fahrenheit is " << (temp * 9 / 5) + 32 << "F." << endl; - } else if (unit == 'F') { - cout << "The temperature in Celsius is " << (temp - 32) * 5 / 9 << "C." << endl; - } else { - cout << "Invalid unit." << endl; - } + // Normalize and validate unit character + unit = static_cast(toupper(static_cast(unitStr[0]))); // PRECOGS_FIX: normalize to uppercase and bound to first character + if (unit == 'C') { + cout << fixed << setprecision(2); + cout << "The temperature in Fahrenheit is " << (temp * 9.0f / 5.0f) + 32.0f << "F." << endl; + } else if (unit == 'F') { + cout << fixed << setprecision(2); + cout << "The temperature in Celsius is " << (temp - 32.0f) * 5.0f / 9.0f << "C." << endl; + } else { + cout << "Invalid unit. Use 'C' or 'F'." << endl; + } - return 0; -} \ No newline at end of file + return 0; +} diff --git a/Lab_02/02_odd_even.cpp b/Lab_02/02_odd_even.cpp index 5a04183..5e0608e 100644 --- a/Lab_02/02_odd_even.cpp +++ b/Lab_02/02_odd_even.cpp @@ -1,18 +1,21 @@ -// Program to find whether given number is even or odd. - #include +#include + using namespace std; int main() { - int a; - cout << "Enter the number: "; - cin >> a; + int a = 0; // PRECOGS_FIX: initialize variable to avoid use of uninitialized memory + cout << "Enter the number: "; + if (!(cin >> a)) { + cerr << "Invalid input. Please enter a valid integer." << endl; // PRECOGS_FIX: validate extraction and fail fast on invalid input + return 1; + } - if (a % 2 == 0) - cout << "Your number is Even!" << endl; - else - cout << "Your number is Odd!" << endl; + if (a % 2 == 0) + cout << "Your number is Even!" << endl; + else + cout << "Your number is Odd!" << endl; - system("pause"); - return 0; -} \ No newline at end of file + system("pause"); + return 0; +} diff --git a/Lab_02/03_negative_positive.cpp b/Lab_02/03_negative_positive.cpp index 0bf9966..ed9deb2 100644 --- a/Lab_02/03_negative_positive.cpp +++ b/Lab_02/03_negative_positive.cpp @@ -1,6 +1,5 @@ -// C++ Program to Check Whether a Number is Positive or Negative. - #include +#include using namespace std; @@ -8,7 +7,10 @@ int main() { float number; cout << "Enter a number: "; - cin >> number; + if (!(cin >> number)) { + cerr << "Invalid input. Please enter a numeric value." << endl; + return 1; + } if (number > 0) { cout << "The number is positive." << endl; @@ -18,6 +20,10 @@ int main() { cout << "The number is zero." << endl; } - system("pause"); + // Use a safe, portable method to pause for user input instead of calling system(). + cin.ignore(numeric_limits::max(), '\n'); // PRECOGS_FIX: clear any leftover input/newline from previous extraction + cout << "Press Enter to continue..."; + cin.get(); // PRECOGS_FIX: replace system("pause") with safe std::cin.get() + return 0; -} \ No newline at end of file +} diff --git a/Lab_02/04_vowel_consonant.cpp b/Lab_02/04_vowel_consonant.cpp index 7c2421e..f826f98 100644 --- a/Lab_02/04_vowel_consonant.cpp +++ b/Lab_02/04_vowel_consonant.cpp @@ -1,22 +1,42 @@ -// C++ Program to Check Whether a Character is a Vowel or Consonant. - #include +#include +#include +#include using namespace std; int main() { - char c; + string s; cout << "Enter a character: "; - cin >> c; + if (!(cin >> s)) { + cerr << "Input error." << endl; + return 1; + } + + if (s.empty()) { + cerr << "No input provided." << endl; + return 1; + } - if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || - c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { + char c = s[0]; + if (s.size() > 1) { + cerr << "Warning: more than one character entered; using first character." << endl; + } + + if (!isalpha(static_cast(c))) { + cout << "Input is not an alphabetic character." << endl; + } else if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || + c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { cout << "The character is a vowel." << endl; } else { cout << "The character is a consonant." << endl; } - system("pause"); + // PRECOGS_FIX: Removed use of system() to avoid invoking the OS command processor; use a safe, portable pause instead + cout << "Press Enter to continue..." << flush; + cin.ignore(numeric_limits::max(), '\n'); + cin.get(); + return 0; -} \ No newline at end of file +} diff --git a/Lab_02/05_largest_number.cpp b/Lab_02/05_largest_number.cpp index 9d285a4..322afc3 100644 --- a/Lab_02/05_largest_number.cpp +++ b/Lab_02/05_largest_number.cpp @@ -1,25 +1,24 @@ -// Program to find greatest in 3 numbers - #include using namespace std; int main() { - float a, b, c; - cout << "Enter the 3 numbres: "; - cin >> a >> b >> c; - - if (a >= b && a >= c) - cout << "The greatest number is " << a << ";" << endl; - - if (b >= a && b >= c) - cout << "The greatest number is " << b << ";" << endl; + float a, b, c; + cout << "Enter the 3 numbers: "; + if (!(cin >> a >> b >> c)) { + cerr << "Invalid input. Please enter three numeric values." << endl; + return 1; + } - if (c >= a && c >= b) - cout << "The greatest number is " << c << ";" << endl; - else if (a == b == c) - cout << "All numbers are equal." << endl; + if (a == b && b == c) + cout << "All numbers are equal." << endl; + else if (a >= b && a >= c) + cout << "The greatest number is " << a << ";" << endl; + else if (b >= a && b >= c) + cout << "The greatest number is " << b << ";" << endl; + else + cout << "The greatest number is " << c << ";" << endl; - system("pause"); - return 0; -} \ No newline at end of file + // PRECOGS_FIX: Removed unsafe system() call to eliminate reliance on PATH/working-directory lookup and prevent execution of attacker-controlled binaries + return 0; +} diff --git a/Lab_02/06_leap_year.cpp b/Lab_02/06_leap_year.cpp index 7c2b41e..a3d6162 100644 --- a/Lab_02/06_leap_year.cpp +++ b/Lab_02/06_leap_year.cpp @@ -1,36 +1,41 @@ -// Program to find that entered year is leap or not. - #include +#include +#include +#include + using namespace std; int main() { - int year; - cout << "Enter a Year: "; - cin >> year; - - if (year % 4 == 0) - { - if (year % 100 == 0) - { - if (year % 400 == 0) - { - cout << "Given " << year << " year is a leap." << endl; - } - else - { - cout << "Given " << year << " year isn't a leap." << endl; - } - } - else - { - cout << "Given " << year << " year is a leap year." << endl; - } - } - else - { - cout << "Given " << year << " year isn't a leap." << endl; - } - system("pause"); - return 0; -} \ No newline at end of file + string line; + cout << "Enter a Year: "; + if (!std::getline(cin, line)) { + cerr << "Input error: no data received." << endl; + return 1; + } + + // Parse input robustly and validate range + stringstream ss(line); + long long year_ll = 0; + if (!(ss >> year_ll) || !ss.eof()) { + cerr << "Invalid input. Please enter a valid integer year." << endl; + return 1; + } + + if (year_ll < INT_MIN || year_ll > INT_MAX) { + cerr << "Year out of range." << endl; + return 1; + } + + int year = static_cast(year_ll); // PRECOGS_FIX: validate and safely parse user input into an int within bounds + + bool isLeap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); + + if (isLeap) + cout << "Given " << year << " year is a leap." << endl; + else + cout << "Given " << year << " year isn't a leap." << endl; + + // PRECOGS_FIX: removed system("pause") to avoid executing platform-specific shell commands and reduce risk + return 0; +} diff --git a/Lab_04/Source.cpp b/Lab_04/Source.cpp index 4b0092b..8e6184b 100644 --- a/Lab_04/Source.cpp +++ b/Lab_04/Source.cpp @@ -1,19 +1,24 @@ -// Lab assignment #5 -// ID:U1910049; Name: Zokirov Rustam -// Control structure - Nested Loop - #include +#include + using namespace std; + int main() { - for (int i = 1; i <= 5; i++) - { - for (int j = 1; j <= i; j++) - { - cout << "*"; - } - cout << endl; - } - system("pause"); - return 0; -} \ No newline at end of file + for (int i = 1; i <= 5; i++) + { + for (int j = 1; j <= i; j++) + { + cout << "*"; + } + cout << endl; + } + + // PRECOGS_FIX: Removed unsafe system() call and replaced with a safe, cross-platform pause using std::cin + cout << "Press Enter to continue..."; + // PRECOGS_FIX: Use std::cin.ignore to consume any leftover input and std::cin.get to wait for Enter without invoking a shell + cin.ignore(numeric_limits::max(), '\n'); + cin.get(); + + return 0; +} diff --git a/Lab_04/Source1.cpp b/Lab_04/Source1.cpp index b027509..24f53d2 100644 --- a/Lab_04/Source1.cpp +++ b/Lab_04/Source1.cpp @@ -1,8 +1,5 @@ -// Lab assignment #5 -// ID:U1910049; Name:Zokirov Rustam -// Control structure - Nested Loop - #include +#include using namespace std; int main2() { @@ -17,6 +14,9 @@ int main2() } cout << endl; } - system("pause"); + + // PRECOGS_FIX: Removed use of system() to avoid invoking the OS shell; use a safe, standard input-based pause instead. + cout << "Press Enter to continue..."; + cin.ignore(numeric_limits::max(), '\n'); return 0; -} \ No newline at end of file +} diff --git a/Lab_04/Source2.cpp b/Lab_04/Source2.cpp index eee2058..38544ab 100644 --- a/Lab_04/Source2.cpp +++ b/Lab_04/Source2.cpp @@ -1,9 +1,3 @@ -// Lab assignment #5 -// ID:U1910049; Name:Zokirov Rustam -// Control structure - Nested Loop - -#include -using namespace std; int main3() { for (int i = 1; i <= 5; i += 2) @@ -19,6 +13,9 @@ int main3() } cout << endl; } - system("pause"); + + cout << "Press Enter to continue..." << std::flush; + // PRECOGS_FIX: Avoid invoking the shell via system(); use in-process standard input to wait for the user. + std::cin.get(); // PRECOGS_FIX: Wait for Enter press in a portable, safer way than system("pause") return 0; -} \ No newline at end of file +} diff --git a/Lab_04/Source3.cpp b/Lab_04/Source3.cpp index 4300bc7..353eca3 100644 --- a/Lab_04/Source3.cpp +++ b/Lab_04/Source3.cpp @@ -1,9 +1,3 @@ -// Lab assignment #5 -// ID:U1910049; Name:Zokirov Rustam -// Control structure - Nested Loop - -#include -using namespace std; int main4() { for (int i = 1; i <= 5; i++) @@ -22,6 +16,9 @@ int main4() } cout << endl; } - system("pause"); + // PRECOGS_FIX: Replace unsafe system() invocation with a safe, portable wait for user input + cout << "Press Enter to continue..."; + cout.flush(); + std::cin.get(); return 0; -} \ No newline at end of file +} diff --git a/Lab_04/Source4.cpp b/Lab_04/Source4.cpp index a93f326..14ca5ef 100644 --- a/Lab_04/Source4.cpp +++ b/Lab_04/Source4.cpp @@ -1,9 +1,3 @@ -// Lab assignment #5 -// ID:U1910049; Name: Zokirov Rustam -// A program to add first seven terms of the following series using for loop:1 / !1 + 2 / !2 + 3 / !3 + ... - -#include -using namespace std; int main5() { float sum = 0, fact = 1; @@ -13,6 +7,8 @@ int main5() sum += i / fact; } cout << "Sum is : " << sum << endl; - system("pause"); + // PRECOGS_FIX: Remove call to system() and replace with safe, portable stdin-based pause to avoid invoking a shell + cout << "Press Enter to continue..."; + cin.get(); return 0; -} \ No newline at end of file +} diff --git a/Lab_05/Source.cpp b/Lab_05/Source.cpp index 2f54011..cc182d9 100644 --- a/Lab_05/Source.cpp +++ b/Lab_05/Source.cpp @@ -7,11 +7,17 @@ using namespace std; float area(float radius) -{ // function for calculating the area - float area; - area = (atan(1) * 4) * (pow(radius, 2)); //"atan" is the function which is finding the PI=3.14 - return area; -} // end function "area" +{ + // Validate input to avoid NaN/Inf/negative values and use double for intermediate precision. + if (!isfinite(radius) || radius <= 0.0f) { + std::cerr << "area(): invalid radius value" << std::endl; + return 0.0f; // PRECOGS_FIX: validate input to prevent NaN/Inf/negative radius propagation + } + + double pi = atan(1.0) * 4.0; + double result = pi * static_cast(radius) * static_cast(radius); + return static_cast(result); // PRECOGS_FIX: use double intermediates then cast to float to reduce precision loss +} int main1() { diff --git a/Lab_05/Source1.cpp b/Lab_05/Source1.cpp index 34c9a9e..4ca185e 100644 --- a/Lab_05/Source1.cpp +++ b/Lab_05/Source1.cpp @@ -1,18 +1,21 @@ -// Practical Lab Assignment-7(Week 9) -// ID:U1910049 Name: Rustam Zokirov -// Program to swap two values using functions - -#include -#include //C++ libriry which is calculating "pow" and PI=3.14 -using namespace std; - int main2() { - int a, b; - cout << "Enter two numbers: "; - cin >> a >> b; - swap(a, b); // C++ function to swap two numbers - cout << a << " & " << b << endl; // swap is the function which is contained in c++ ==> - //==> there is no need to open new function to swap - return 0; -} // ending the function main \ No newline at end of file + #include + using std::cout; using std::cin; using std::endl; using std::swap; + + int a = 0, b = 0; // PRECOGS_FIX: initialize variables to avoid indeterminate values if input fails + + cout << "Enter two numbers: "; + + if (!(cin >> a >> b)) { + // PRECOGS_FIX: handle invalid input by clearing state, discarding remaining input, and reporting error + cout << "Invalid input. Please enter two integers." << endl; + cin.clear(); + cin.ignore(std::numeric_limits::max(), '\n'); + return 1; + } + + swap(a, b); + cout << a << " & " << b << endl; + return 0; +} diff --git a/Lab_05/Source2.cpp b/Lab_05/Source2.cpp index cbe76ff..e2cffe4 100644 --- a/Lab_05/Source2.cpp +++ b/Lab_05/Source2.cpp @@ -6,22 +6,74 @@ using namespace std; double converting1(double hours, double minutes, double seconds) -{ // initializing the variables - return (hours * 60) + (minutes) + (seconds / 60); // returning the function "converting" +{ + #include + #include + + // Validate that inputs are finite + if (!std::isfinite(hours) || !std::isfinite(minutes) || !std::isfinite(seconds)) + { + // PRECOGS_FIX: return NaN when inputs are not finite to avoid propagating INF/NaN silently + return std::numeric_limits::quiet_NaN(); + } + + // Normalize negative or out-of-range minute/second values in a safe way + if (hours < 0.0 || minutes < 0.0 || seconds < 0.0) + { + // PRECOGS_FIX: reject negative time components by returning NaN + return std::numeric_limits::quiet_NaN(); + } + + // If seconds or minutes overflow their conventional ranges, normalize them into minutes/hours + // e.g., 90 seconds -> 1 minute + 30 seconds + double extraMinutesFromSeconds = std::floor(seconds / 60.0); + seconds = std::fmod(seconds, 60.0); + if (seconds < 0.0) seconds += 60.0; // defensive, though seconds >= 0 above + + minutes += extraMinutesFromSeconds; + double extraHoursFromMinutes = std::floor(minutes / 60.0); + minutes = std::fmod(minutes, 60.0); + if (minutes < 0.0) minutes += 60.0; + + hours += extraHoursFromMinutes; + + // Final arithmetic; inputs are finite and normalized + return (hours * 60.0) + minutes + (seconds / 60.0); } int main3() { - double hours, minutes, seconds; - cout << "Hours: "; - cin >> hours; // Prompting user for data and - cout << "Minutes: "; // reading 3 numbers for user - cin >> minutes; //! - cout << "Seconds: "; - cin >> seconds; - if (hours >= 0 && minutes >= 0 && seconds >= 0) // the program will be executed when all numbers are positive - cout << "The time in minutes is " << converting1(hours, minutes, seconds) << endl; - else // calling function to calculate the main function - cout << "Invalid inputs!" << endl; - return 0; // indicates that the program will ended successfully + #include + #include + + double hours, minutes, seconds; + + std::cout << "Hours: "; + if (!(std::cin >> hours)) // PRECOGS_FIX: validate extraction success to avoid uninitialized variable usage + { + std::cerr << "Invalid numeric input for Hours.\n"; + return 1; + } + + std::cout << "Minutes: "; + if (!(std::cin >> minutes)) // PRECOGS_FIX: validate extraction success + { + std::cerr << "Invalid numeric input for Minutes.\n"; + return 1; + } + + std::cout << "Seconds: "; + if (!(std::cin >> seconds)) // PRECOGS_FIX: validate extraction success + { + std::cerr << "Invalid numeric input for Seconds.\n"; + return 1; + } + + // Basic semantic validation: non-negative + if (hours >= 0 && minutes >= 0 && seconds >= 0) + std::cout << "The time in minutes is " << converting1(hours, minutes, seconds) << std::endl; + else + std::cout << "Invalid inputs!" << std::endl; + + return 0; } \ No newline at end of file diff --git a/Lab_05/Source3.cpp b/Lab_05/Source3.cpp index 0f007cb..2894da4 100644 --- a/Lab_05/Source3.cpp +++ b/Lab_05/Source3.cpp @@ -8,25 +8,51 @@ using namespace std; double qwerty(double n, double sum, double fact) { - for (int i = 1; i <= n; i++) - { - fact = fact * i; // calculating the factorial of the numbers - sum += (pow(i, i) / (fact)); // executing the sum of numbers - } //"pow" is raising 'i' to power 'i' - return sum; // -} // end function qwerty + // Validate the input and cap iterations to prevent resource exhaustion and numeric overflow + if (!isfinite(n) || n <= 0) + return sum; + + const int MAX_ITER = 10000; // PRECOGS_FIX: cap iterations to a safe maximum to prevent DoS + long long iter = static_cast(floor(n)); + if (iter > MAX_ITER) { + iter = MAX_ITER; // PRECOGS_FIX: enforce an upper bound on loop iterations + } + + for (int i = 1; i <= iter; ++i) + { + fact = fact * i; + double term = pow(static_cast(i), static_cast(i)) / fact; + if (!isfinite(term)) { + // Detected overflow/invalid arithmetic; stop further accumulation to avoid propagating NaN/Inf + break; // PRECOGS_FIX: stop when numeric results become non-finite + } + sum += term; + } + return sum; +} int main() { - double n; - double sum = 0; - double fact = 1; - cout << "Please ebter the number: "; // outputing and inputing the number - cin >> n; - if (n > 0) // the program will calculate the sum when numbers is positive - cout << "Sum is: " << qwerty(n, sum, fact) << endl; // calling the function qwerty - else // when the number is negative 'else' will work - cout << "INVALID INPUT!" << endl; - system("pause"); - return 0; -} // ending the program successfully \ No newline at end of file + double n; + double sum = 0; + double fact = 1; + cout << "Please ebter the number: "; // outputing and inputing the number + + // Validate the input to avoid processing invalid/tainted values + if (!(cin >> n)) { + cout << "INVALID INPUT!" << endl; // PRECOGS_FIX: validate input and exit on failure + return 1; + } + + if (n > 0) + cout << "Sum is: " << qwerty(n, sum, fact) << endl; // calling the function qwerty + else + cout << "INVALID INPUT!" << endl; + + // PRECOGS_FIX: Avoid system() to prevent OS command execution; use a portable, safe pause + cin.get(); // consume leftover newline from previous input + cout << "Press Enter to exit..."; + cin.get(); // wait for user to press Enter + + return 0; +} \ No newline at end of file diff --git a/Lab_20/Person.cpp b/Lab_20/Person.cpp index 04c1a53..16594d2 100644 --- a/Lab_20/Person.cpp +++ b/Lab_20/Person.cpp @@ -390,39 +390,46 @@ void F_Second_Program() int main() { - for (int k = 0; k < 1000; k++) - { - system("cls"); - cout << " M A I N M E N U\n" - << "-------------------\n" - << " 1. First Program\n" - << " 2. Second Program\n" - << " Your choice: \n"; - - switch (_getch()) - { - // First Program - case 49: - system("cls"); - F_First_Program(); - system("pause"); - break; - - // Second Program - case 50: - system("cls"); - F_Second_Program(); - system("pause"); - break; - - default: - cout << " Your choice is not available in Menu.\n Please try one more time\n\n"; - system("pause"); - break; - - } // switch - } // for loop - - system("pause"); - return 0; + for (int k = 0; k < 1000; k++) + { + // Replace system("cls") with a safe, non-shell-dependent clear simulation. + std::cout << std::string(100, '\n'); // PRECOGS_FIX: Removed system("cls") to avoid invoking shell + std::cout << " M A I N M E N U\n" + << "-------------------\n" + << " 1. First Program\n" + << " 2. Second Program\n" + << " Your choice: \n"; + + int ch = _getch(); + switch (ch) + { + // First Program + case '1': + std::cout << std::string(100, '\n'); + F_First_Program(); + std::cout << "Press any key to continue..." << std::endl; + _getch(); // PRECOGS_FIX: Replaced system("pause") with direct key wait using _getch() + break; + + // Second Program + case '2': + std::cout << std::string(100, '\n'); + F_Second_Program(); + std::cout << "Press any key to continue..." << std::endl; + _getch(); + break; + + default: + std::cout << " Your choice is not available in Menu.\n Please try one more time\n\n"; + std::cout << "Press any key to continue..." << std::endl; + _getch(); + break; + + } // switch + } // for loop + + std::cout << "Press any key to exit..." << std::endl; + _getch(); + return 0; } + diff --git a/Project_E-Commerce_App_V2.0/main.cpp b/Project_E-Commerce_App_V2.0/main.cpp index bdc9d71..63fad23 100644 --- a/Project_E-Commerce_App_V2.0/main.cpp +++ b/Project_E-Commerce_App_V2.0/main.cpp @@ -1021,445 +1021,388 @@ void F_Owner_Main_Menu() { void F_Owner_Products_Stotage() { + // Helper lambdas for robust, validated input + auto readDouble = [&](const std::string &prompt, double minValue) { + std::string line; + while (true) { + std::cout << prompt; + if (!std::getline(std::cin, line)) { + std::cin.clear(); + continue; + } + // Trim leading/trailing whitespace + size_t start = line.find_first_not_of(" \t\r\n"); + size_t end = line.find_last_not_of(" \t\r\n"); + if (start == std::string::npos) { std::cout << "Invalid input.\n"; continue; } + std::string trimmed = line.substr(start, end - start + 1); + try { + size_t idx = 0; + double v = std::stod(trimmed, &idx); + if (idx != trimmed.size()) { throw std::invalid_argument("extra chars"); } + if (v < minValue) { std::cout << "Value must be >= " << minValue << "\n"; continue; } + return v; // PRECOGS_FIX: robust parsing + bounds check to prevent invalid or malicious numeric input + } + catch (...) { + std::cout << "Invalid number. Please try again.\n"; + continue; + } + } + }; + + auto readInt = [&](const std::string &prompt, long minValue) { + std::string line; + while (true) { + std::cout << prompt; + if (!std::getline(std::cin, line)) { + std::cin.clear(); + continue; + } + size_t start = line.find_first_not_of(" \t\r\n"); + size_t end = line.find_last_not_of(" \t\r\n"); + if (start == std::string::npos) { std::cout << "Invalid input.\n"; continue; } + std::string trimmed = line.substr(start, end - start + 1); + try { + size_t idx = 0; + long v = std::stol(trimmed, &idx); + if (idx != trimmed.size()) { throw std::invalid_argument("extra chars"); } + if (v < minValue) { std::cout << "Value must be >= " << minValue << "\n"; continue; } + return v; // PRECOGS_FIX: robust parsing + bounds check to prevent invalid or malicious integer input + } + catch (...) { + std::cout << "Invalid integer. Please try again.\n"; + continue; + } + } + }; + for (int i = 0; i < 1000; i++) { F_Logo_Owner(); - cout << " Products List Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " 1. Potatoes, Weight Vegetables & Fruits " << Potatoes.getPrice() << "\t\t " << Potatoes.getQuantity() << endl; - cout << " 2. Yellow Carrot, Weight Vegetables & Fruits " << Carrot.getPrice() << "\t\t " << Carrot.getQuantity() << endl; - cout << " 3. Onion, Weight Vegetables & Fruits " << Onion.getPrice() << "\t\t " << Onion.getQuantity() << endl; - cout << " 4. Water Water & Beverages " << Water.getPrice() << "\t\t " << Water.getQuantity() << endl; - cout << " 5. Pepsi Water & Beverages " << Pepsi.getPrice() << "\t\t " << Pepsi.getQuantity() << endl; - cout << " 6. Nector Water & Beverages " << Nectar.getPrice() << "\t\t " << Nectar.getQuantity() << endl; - cout << " 7. Pizza Bread & Bakery Products " << Pizza.getPrice() << "\t " << Pizza.getQuantity() << endl; - cout << " 8. Burger Bread & Bakery Products " << Burger.getPrice() << "\t " << Burger.getQuantity() << endl; - cout << " 9. Potatoe Fries Bread & Bakery Products " << Fries.getPrice() << "\t " << Fries.getQuantity() << endl; - cout << " \n 0. Back\n"; - cout << " Make changes in: "; + std::cout << " Products List Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " 1. Potatoes, Weight Vegetables & Fruits " << Potatoes.getPrice() << "\t\t " << Potatoes.getQuantity() << std::endl; + std::cout << " 2. Yellow Carrot, Weight Vegetables & Fruits " << Carrot.getPrice() << "\t\t " << Carrot.getQuantity() << std::endl; + std::cout << " 3. Onion, Weight Vegetables & Fruits " << Onion.getPrice() << "\t\t " << Onion.getQuantity() << std::endl; + std::cout << " 4. Water Water & Beverages " << Water.getPrice() << "\t\t " << Water.getQuantity() << std::endl; + std::cout << " 5. Pepsi Water & Beverages " << Pepsi.getPrice() << "\t\t " << Pepsi.getQuantity() << std::endl; + std::cout << " 6. Nector Water & Beverages " << Nectar.getPrice() << "\t\t " << Nectar.getQuantity() << std::endl; + std::cout << " 7. Pizza Bread & Bakery Products " << Pizza.getPrice() << "\t " << Pizza.getQuantity() << std::endl; + std::cout << " 8. Burger Bread & Bakery Products " << Burger.getPrice() << "\t " << Burger.getQuantity() << std::endl; + std::cout << " 9. Potatoe Fries Bread & Bakery Products " << Fries.getPrice() << "\t " << Fries.getQuantity() << std::endl; + std::cout << " \n 0. Back\n"; + std::cout << " Make changes in: "; + switch (_getch()) { case '1': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Potatoes, Weight Vegetables & Fruits " << Potatoes.getPrice() << "\t\t " << Potatoes.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n"; + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Potatoes, Weight Vegetables & Fruits " << Potatoes.getPrice() << "\t\t " << Potatoes.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n"; switch (_getch()) { - case 49: - cout << " Enter a new price: "; - cin >> Ch_Price; - if (Ch_Price >= 0) { - Potatoes.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << " Enter a new quantity in storage: "; - cin >> Ch_Quantity; - if (Ch_Quantity > 0) { - Potatoes.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - - break; - case 48: + case '1': { + // Use robust numeric parsing instead of >> + double newP = readDouble(" Enter a new price: ", 0.0); + Potatoes.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt(" Enter a new quantity in storage: ", 1); + Potatoes.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '2': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Yellow Carrot, Weight Vegetables & Fruits " << Carrot.getPrice() << "\t\t " << Carrot.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 3. Go back \n Press '1' or '2' or '0'\n\n "; + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Yellow Carrot, Weight Vegetables & Fruits " << Carrot.getPrice() << "\t\t " << Carrot.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 3. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - if (Ch_Price >= 0) { - Carrot.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - - if (Ch_Quantity > 0) { - Carrot.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Carrot.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Carrot.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '3': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Onion, Weight Vegetables & Fruits " << Onion.getPrice() << "\t\t " << Onion.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Onion, Weight Vegetables & Fruits " << Onion.getPrice() << "\t\t " << Onion.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - if (Ch_Price >= 0) { - Onion.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - if (Ch_Quantity > 0) { - Onion.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Onion.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Onion.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '4': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Water, Hydrolife without gas 750ml Water & Beverages " << Water.getPrice() << "\t\t " << Water.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; - + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Water, Hydrolife without gas 750ml Water & Beverages " << Water.getPrice() << "\t\t " << Water.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - - if (Ch_Price >= 0) { - Water.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - if (Ch_Quantity > 0) { - Water.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Water.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Water.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '5': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Drink, Aloe Original 500ml Water & Beverages " << Pepsi.getPrice() << "\t\t " << Pepsi.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; - + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Drink, Aloe Original 500ml Water & Beverages " << Pepsi.getPrice() << "\t\t " << Pepsi.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - - if (Ch_Price >= 0) { - Pepsi.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - - if (Ch_Quantity > 0) { - Pepsi.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Pepsi.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Pepsi.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '6': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Nectar, Zet Apple 125ml Water & Beverages " << Nectar.getPrice() << "\t\t " << Nectar.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; - + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Nectar, Zet Apple 125ml Water & Beverages " << Nectar.getPrice() << "\t\t " << Nectar.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - if (Ch_Price >= 0) { - Nectar.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - if (Ch_Quantity > 0) { - Nectar.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Nectar.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Nectar.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '7': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Pizza Bread & Bakery Products " << Pizza.getPrice() << "\t " << Pizza.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Pizza Bread & Bakery Products " << Pizza.getPrice() << "\t " << Pizza.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - if (Ch_Price >= 0) { - Pizza.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - if (Ch_Quantity > 0) { - Pizza.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Pizza.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Pizza.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '8': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Burger Bread & Bakery Products " << Burger.getPrice() << "\t" << Burger.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Burger Bread & Bakery Products " << Burger.getPrice() << "\t" << Burger.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - if (Ch_Price >= 0) { - Burger.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - if (Ch_Quantity > 0) { - Burger.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Burger.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Burger.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '9': for (int j = 0; j < 1000; j++) { system("cls"); - cout << "\n Product Category Price In Stock\n"; - cout << "____________________________________________________________________________________________________ \n"; - cout << " Potatoe Fries Bread & Bakery Products " << Fries.getPrice() << "\t" << Fries.getQuantity() << endl; - cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; + std::cout << "\n Product Category Price In Stock\n"; + std::cout << "____________________________________________________________________________________________________ \n"; + std::cout << " Potatoe Fries Bread & Bakery Products " << Fries.getPrice() << "\t" << Fries.getQuantity() << std::endl; + std::cout << "\n 1. Change price \n 2. Change the quantity in storage\n 0. Go back \n Press '1' or '2' or '0'\n\n "; switch (_getch()) { - case 49: - cout << "Enter a new price: "; - cin >> Ch_Price; - - if (Ch_Price >= 0) { - Fries.price = Ch_Price; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Price cannot be negative! Please check one more time.\n"; - Sleep(0700); Sleep(0700); - } - break; - case 50: - cout << "Enter a new quantity in storage: "; - cin >> Ch_Quantity; - if (Ch_Quantity > 0) { - Fries.quantity = Ch_Quantity; - cout << " Successfully changed!\n"; - Sleep(0700); Sleep(0700); - } - else { - cout << " Quantity cannot be negative\n"; - Sleep(0700); Sleep(0700); - } - break; - case 48: + case '1': { + double newP = readDouble("Enter a new price: ", 0.0); + Fries.price = newP; + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '2': { + long newQ = readInt("Enter a new quantity in storage: ", 1); + Fries.quantity = static_cast(newQ); + std::cout << " Successfully changed!\n"; + Sleep(700); Sleep(700); + } break; + case '0': j = 1000; break; - }// 'switch' - }// 'for' loop + } + } break; + case '0': { // Back to Menu system("cls"); i = 1000; F_Owner_Main_Menu(); } break; - case 'i' || 'I': { // User info + case 'i': + case 'I': { // User info system("cls"); - cout << "\n\t\t\t User Information:" << endl;; - cout << "\t\t _______________________________" << endl << endl;; - cout << "\t\t User Name : " << Name_Memory << endl; - cout << "\t\t Telephone : " << TellNum_Memory << endl; - cout << "\t\t Login : " << Login_Memory << endl; - cout << "\t\t Password : " << Parol_Memory << endl << endl << endl; + std::cout << "\n\t\t\t User Information:" << std::endl; + std::cout << "\t\t _______________________________" << std::endl << std::endl; + std::cout << "\t\t User Name : " << Name_Memory << std::endl; + std::cout << "\t\t Telephone : " << TellNum_Memory << std::endl; + std::cout << "\t\t Login : " << Login_Memory << std::endl; + // Do NOT reveal the password in plaintext; show masked/hidden instead + { + std::string pwd = Parol_Memory; + std::string mask(pwd.size(), '*'); + std::cout << "\t\t Password : " << mask << " (hidden)" << std::endl << std::endl << std::endl; // PRECOGS_FIX: mask password output to prevent plaintext disclosure + } system("pause"); } - break; + break; - default: { cout << "\n\t\t Your choice is not available in Menu" << endl; - cout << "\t\tPlease press any keyboard to continue program\n" << endl; + default: { + std::cout << "\n\t\t Your choice is not available in Menu" << std::endl; + std::cout << "\t\tPlease press any keyboard to continue program\n" << std::endl; system("pause"); } break; } // switch - } // for loop for products in stock + } // for } // Customer List void F_Owner_Customers_List() { cout << endl; - ifstream in; + std::ifstream in("User_Info.txt"); int Num = 1; - string Info; + std::string username, phone, login, password; + + if (!in.is_open()) { + std::cerr << "\t Unable to open user info file.\n"; + return; // PRECOGS_FIX: added explicit file-open check and early return to avoid further processing + } + + while (true) { + // Read complete record (4 lines). If any read fails, stop to avoid partial/inconsistent output. + if (!std::getline(in, username)) break; + if (!std::getline(in, phone)) break; + if (!std::getline(in, login)) break; + if (!std::getline(in, password)) break; - in.open("User_Info.txt"); - while (in) { cout << "\t " << Num << "." << endl; cout << "\t-------------------------" << endl; - getline(in, Info); - cout << "\t User Name: " << Info << endl; - getline(in, Info); - cout << "\t Phone : " << Info << endl; - getline(in, Info); - cout << "\t Login : " << Info << endl; - getline(in, Info); - cout << "\t Password : " << Info << endl; + cout << "\t User Name: " << username << endl; + cout << "\t Phone : " << phone << endl; + cout << "\t Login : " << login << endl; + // Do NOT display plaintext passwords. Show redacted information only. + cout << "\t Password : [REDACTED] (length " << password.length() << ")" << endl; // PRECOGS_FIX: redact plaintext password output Num++; cout << endl; } cout << "\t-------------------------" << endl; in.close(); -} \ No newline at end of file +}