Write a C++ program that uses a structure to store the following inventory information in a file:
⦁ Item description, Quantity on hand, Wholesale
cost, Retail cost, and Date added to inventory.
⦁ Use a char array for item description and date.
⦁ The program should have a menu that allows the user
to perform the following tasks:
i. Add a new record at the end of the file.
ii. Display any record in the file.
iii. Change any record in the file.
iv. Display all records in the file.
v. Exit the program (otherwise repeat menu)
⦁ Each of the above tasks should open/use/close the
file
⦁ Input validation: The program should not accept
quantities, or wholesale or retail costs < 0.
⦁ Do not forget to add a record will need to modify the
open function to include append mode (ios::binary | ios::app).
This is what I have done so far, but I keep getting infinite loops due to the cin buffer.
// begin program #include <iostream> #include <fstream> #include <cstring> //Structure definition struct Inventory { char item[20]; int qty; double wcost; double rcost; char date[10]; }; void print_itemInfo(Inventory inv) { std::cout << "Item :" << inv.item << std::endl; std::cout << "Quantity :" << inv.qty << std::endl; std::cout << "Wholesale Cost:" << inv.wcost << std::endl; std::cout << "Retail Cost :" << inv.rcost << std::endl; std::cout << "Date :" << inv.date << std::endl; } //Function prototypes void displayMenu(); char getChoice(char); void addRecord(); void displayRecord(); void editRecord(); void displayAll(); const char filename[] = "data.dat"; // Main function int main() { // variable declaration const char MAX_CHOICE = '5'; char choice; do { displayMenu(); choice = getChoice(MAX_CHOICE); switch (choice) { case '1': addRecord(); break; case '2': displayRecord(); break; case '3': editRecord(); break; case '4': displayRecord(); default: std::cout << "invalid choice\n"; } } while (choice != '5'); return 0; } /************************************************************************ * displayMenu * * This function displays the user's menu on the screen * ************************************************************************/ void displayMenu() { // Menu choice std::cout << "\nMENU\n\n"; std::cout << "1. Add Record\n"; std::cout << "2. Display Record\n"; std::cout << "3. Edit Record\n"; std::cout << "4. Display All Records\n"; std::cout << "5. Exit the program\n\n"; std::cout << "Enter your choice: "; } /************************************************************************* * getChoice * * This function gets, validates, and returns the user's choice. * * ***********************************************************************/ char getChoice(char max) { char choice = std::cin.get(); std::cin.ignore(); // Bypass the '\n\ in the input buffer while (choice < '1' || choice > max) { std::cout << "Choice must be between 1 and " << max << ". " << "Please re-enter choice: "; choice = std::cin.get(); std::cin.ignore(); // Bypass the '\n\ in the input buffer } return choice; } // addRecord function void addRecord() { Inventory inv; std::fstream outfile(filename, std::ios::out | std::ios::app | std::ios::binary); // Get data from keyboard std::cout << "Enter item description\n"; std::cin.ignore(); std::cin.getline(inv.item, 20); std::cout << "Enter quantity\n"; std::cin >> inv.qty; std::cout << "Enter wholesale cost\n"; std::cin >> inv.wcost; std::cout << "Enter Retail cost\n"; std::cin >> inv.rcost; std::cout << "Enter date: \n"; std::cin.ignore(); std::cin.getline(inv.date, 10); std::cin.get(); outfile.write(reinterpret_cast<char *>(&inv), sizeof(inv)); outfile.close(); std::cin.clear(); } void displayRecord() { Inventory inv; int recNum; std::fstream rwfile(filename, std::ios::in | std::ios::out | std::ios::binary); // Move to the desired record and display std::cout << "\nEnter the record number you wish to see: \n"; std::cin >> recNum; rwfile.seekg(recNum * sizeof(inv, std::ios::beg)); rwfile.read(reinterpret_cast<char *> (&inv), sizeof(inv)); std::cout << "This is the information for record: " << recNum << std::endl; print_itemInfo(inv); std::cin.clear(); } // displayAll function void displayAll() { std::fstream infile(filename, std::ios::in | std::ios::binary); Inventory inv; infile.read(reinterpret_cast<char *>(&inv), sizeof(inv)); while (!infile.fail() || !infile.eof()) { print_itemInfo(inv); infile.read(reinterpret_cast<char *>(&inv), sizeof(inv)); } infile.clear(); // Close the file infile.close(); } // Edit function definition void editRecord() { Inventory inv; int recNum; std::fstream rwfile(filename, std::ios::in | std::ios::out | std::ios::binary); // Move to the desired record and display std::cout << "\nEnter the number of record to edit\n"; std::cin >> recNum; rwfile.seekg(recNum * sizeof(inv, std::ios::beg)); rwfile.read(reinterpret_cast<char *> (&inv), sizeof(inv)); std::cout << "This is the record you want to edit: \n"; std::cout << "\nDescription\t"; std::cout << inv.item; std::cout << "\nQuantity\t"; std::cout << inv.qty; std::cout << "\nWholesale Cost\t"; std::cout << inv.wcost; std::cout << "\nRetail Cost\t"; std::cout << inv.rcost; std::cout << "\nDate\t"; std::cout << inv.date; //std::cout << "\nEnter new data\n"; std::cout << "\nEnter item description\n"; std::cin.ignore(); std::cin.getline(inv.item, 20); std::cout << "Enter quantity\n"; std::cin >> inv.qty; std::cout << "Enter wholesale cost\n"; std::cin >> inv.wcost; std::cout << "Enter retail cost\n"; std::cin >> inv.rcost; std::cout << "Enter date: "; std::cin.getline(inv.date, 10); rwfile.seekg(recNum * sizeof(inv), std::ios::beg); std::cin.get(); std::cin.clear(); //Close the file rwfile.close(); }
code:
#include <iostream>
#include <fstream>
#include <cstring>
//Structure definition
struct Inventory
{
char item[20];
int qty;
double wcost;
double rcost;
char date[10];
};
void print_itemInfo(Inventory inv)
{
std::cout <<
"Item :"
<< inv.item << std::endl;
std::cout <<
"Quantity :" << inv.qty
<< std::endl;
std::cout << "Wholesale Cost:" <<
inv.wcost << std::endl;
std::cout << "Retail Cost :"
<< inv.rcost << std::endl;
std::cout <<
"Date :"
<< inv.date << std::endl;
}
//Function prototypes
void displayMenu();
char getChoice(char);
void addRecord();
void displayRecord();
void editRecord();
void displayAll();
const char filename[] = "data.dat";
// Main function
int main()
{
// variable declaration
const char MAX_CHOICE = '5';
char choice;
do {
displayMenu();
choice =
getChoice(MAX_CHOICE);
switch (choice)
{
case '1':
addRecord();
break;
case '2':
displayRecord();
break;
case '3':
editRecord();
break;
case '4':
displayAll();
break;
default:
std::cout << "invalid choice\n";
break;
}
} while (choice != '5');
return 0;
}
/************************************************************************
*
displayMenu
*
* This function displays the user's menu on the
screen
*
************************************************************************/
void displayMenu()
{
// Menu choice
std::cout << "\nMENU\n\n";
std::cout << "1. Add Record\n";
std::cout << "2. Display Record\n";
std::cout << "3. Edit Record\n";
std::cout << "4. Display All
Records\n";
std::cout << "5. Exit the
program\n\n";
std::cout << "Enter your choice: ";
}
/*************************************************************************
*
getChoice
*
* This function gets, validates, and returns the user's
choice. *
*
***********************************************************************/
char getChoice(char max)
{
char choice;
std::cin>>choice;
std::cin.ignore();
// Bypass the '\n\ in the input buffer
while (choice < '1' || choice >
max)
{
std::cout <<
"Choice must be between 1 and " << max << ". "
<< "Please re-enter choice: ";
std::cin>>choice;
std::cin.ignore(); // Bypass
the '\n\ in the input buffer
}
return choice;
}
// addRecord function
void addRecord()
{
Inventory inv;
std::fstream outfile(filename, std::ios::out |
std::ios::app | std::ios::binary);
// Get data from keyboard
std::cout << "Enter item
description:\t";
std::cin.ignore();
std::cin.getline(inv.item, 20);
std::cout << "Enter quantity:\t";
std::cin >> inv.qty;
std::cout << "Enter wholesale
cost:\t";
std::cin >> inv.wcost;
std::cout << "Enter Retail cost:\t";
std::cin >> inv.rcost;
std::cout << "Enter date:\t";
std::cin.ignore();
std::cin.getline(inv.date, 10);
std::cin.get();
outfile.write(reinterpret_cast<char
*>(&inv), sizeof(inv));
outfile.close();
std::cin.clear();
}
void displayRecord()
{
Inventory inv;
int recNum;
std::fstream rwfile(filename, std::ios::in |
std::ios::out | std::ios::binary);
// Move to the desired record and
display
std::cout << "\nEnter the record number
you wish to see: \n";
std::cin >> recNum;
rwfile.seekg(recNum * sizeof(inv,
std::ios::beg));
rwfile.read(reinterpret_cast<char *>
(&inv), sizeof(inv));
std::cout << "This is the information for
record: " << recNum << std::endl;
print_itemInfo(inv);
std::cin.clear();
}
// displayAll function
void displayAll()
{
std::fstream infile(filename, std::ios::in |
std::ios::binary);
Inventory inv;
infile.read(reinterpret_cast<char
*>(&inv), sizeof(inv));
while (!infile.fail() || !infile.eof())
{
print_itemInfo(inv);
infile.read(reinterpret_cast<char *>(&inv),
sizeof(inv));
}
infile.clear();
// Close the file
infile.close();
}
// Edit function definition
void editRecord() {
Inventory inv;
int recNum;
std::fstream rwfile(filename, std::ios::in |
std::ios::out | std::ios::binary);
// Move to the desired record and
display
std::cout << "\nEnter the number of record
to edit\n";
std::cin >> recNum;
rwfile.seekg(recNum * sizeof(inv,
std::ios::beg));
rwfile.read(reinterpret_cast<char *>
(&inv), sizeof(inv));
std::cout << "This is the record you want
to edit: \n";
std::cout << "\nDescription\t";
std::cout << inv.item;
std::cout << "\nQuantity\t";
std::cout << inv.qty;
std::cout << "\nWholesale Cost\t";
std::cout << inv.wcost;
std::cout << "\nRetail Cost\t";
std::cout << inv.rcost;
std::cout << "\nDate\t";
std::cout << inv.date;
//std::cout << "\nEnter new data\n";
std::cout << "\nEnter item
description\n";
std::cin.ignore();
std::cin.getline(inv.item, 20);
std::cout << "Enter quantity\n";
std::cin >> inv.qty;
std::cout << "Enter wholesale
cost\n";
std::cin >> inv.wcost;
std::cout << "Enter retail cost\n";
std::cin >> inv.rcost;
std::cout << "Enter date: ";
std::cin.getline(inv.date, 10);
rwfile.seekg(recNum * sizeof(inv),
std::ios::beg);
std::cin.get();
std::cin.clear();
//Close the file
rwfile.close();
}
Note: I have modified the code and not getting infinite loops. Record numbers are storing with same number change it you will get the correct functionality of code.
Output:
Write a C++ program that uses a structure to store the following inventory information in a...
Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...
This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...
(Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth. **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
-can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...
C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record { int age; string name; }; int check(record r[], int n, string nm) { for (int i = 0; i < n;...