Project 1
#include <iostream>
#include <fstream>
#include <string>
const int MAX_ROWS = 3;
const int MAX_COLS = 5;
void enterCommodities(int** commodities) {
std::cout << "Enter the number of commodities to be purchased for each person:" << std::endl;
for (int i = 0; i < MAX_ROWS; ++i) {
commodities[i] = new int[MAX_COLS];
std::cout << "Person " << (i + 1) << std::endl;
std::cout << "Laptop: ";
std::cin >> commodities[i][0];
std::cout << "Mouse: ";
std::cin >> commodities[i][1];
std::cout << "Headset: ";
std::cin >> commodities[i][2];
std::cout << "Laptop Bag: ";
std::cin >> commodities[i][3];
std::cout << "Adapter: ";
std::cin >> commodities[i][4];
}
}
void enterVendorQuotations(int** vendors) {
std::cout << "Enter the quotations for the vendors:" << std::endl;
for (int i = 0; i < MAX_COLS; ++i) {
vendors[i] = new int[MAX_COLS];
std::cout << "Vendor " << (i + 1) << std::endl;
std::cout << "Laptop: ";
std::cin >> vendors[i][0];
std::cout << "Mouse: ";
std::cin >> vendors[i][1];
std::cout << "Headset: ";
std::cin >> vendors[i][2];
std::cout << "Laptop Bag: ";
std::cin >> vendors[i][3];
std::cout << "Adapter: ";
std::cin >> vendors[i][4];
}
}
void printData(int** data) {
for (int i = 0; i < MAX_ROWS; ++i) {
for (int j = 0; j < MAX_COLS; ++j) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
void saveDataToFile(int** data) {
std::ofstream file("data.txt");
if (file.is_open()) {
for (int i = 0; i < MAX_ROWS; ++i) {
for (int j = 0; j < MAX_COLS; ++j) {
file << data[i][j] << " ";
}
file << std::endl;
}
file.close();
std::cout << "Data saved to file successfully." << std::endl;
} else {
std::cout << "Failed to open the file." << std::endl;
}
}
int main() {
int** commodities = new int*[MAX_ROWS];
int** vendors = new int*[MAX_COLS];
int choice;
do {
std::cout << "Menu:\n"
<< "1. Enter Commodities to be purchased\n"
<< "2. Enter Vendor quotations\n"
<< "3. Exit\n"
<< "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
enterCommodities(commodities);
break;
case 2:
enterVendorQuotations(vendors);
break;
case 3:
saveDataToFile(commodities);
saveDataToFile(vendors);
break;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
} while (choice != 3);
// Free allocated memory
for (int i = 0; i < MAX_ROWS; ++i) {
delete[] commodities[i];
}
delete[] commodities;
for (int i = 0; i < MAX_COLS; ++i) {
delete[] vendors[i];
}
delete[] vendors;
return 0;
}