#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
class Book
{
public:
char ISBN [5];
char Title [20];
char authorName [20];
char Price [10];
char Year [10];
char NumOfPages [10];
char delimiter = ',';
};
void AddBook ()
{
fstream file;
file.open("Records.txt", ios::out|ios::app);
Book b;
cout << "Enter ISBN: " << endl;
cin>>b.ISBN;
cin.ignore();
cout << "Enter Title: " << endl;
cin.getline(b.Title,sizeof(b.Title));
cout << "Enter Author's Name: " << endl;
cin.getline(b.authorName , sizeof(b.authorName));
cout << "Enter Price: " << endl;
cin.getline(b.Price,sizeof(b.Price));
cout << "Enter Year: " << endl;
cin.getline(b.Year,10);
cout << "Enter Number Of Pages: " << endl;
cin.getline(b.NumOfPages , sizeof(b.NumOfPages));
cin.ignore();
file.write(reinterpret_cast<char*>(&b), sizeof(b));
file.write(reinterpret_cast<char*>(&b.delimiter), sizeof(b.delimiter));
file.close();
}
void DeleteBook ()
{
}
void UpdateBook ()
{
}
void PrintBook ()
{
}
void PrintAll ()
{
ifstream file;
file.open("Records.txt", ios::in);
Book b;
while (!file.eof())
{
cout << "ISBN :" << b.ISBN <<endl;
cout << "Title :" << b.Title <<endl;
cout << "Author's Name :" << b.authorName <<endl;
cout << "Price :" << b.Price <<endl;
cout << "Year :" << b.Year <<endl;
cout << "Number of Pages "<< b.NumOfPages <<endl;
file.read(reinterpret_cast <char*> (&b), sizeof(b));
}
file.close();
}
int main()
{
int choice;
do
{
cout << "The Menu for Book Store" << endl;
cout << "1. Add Book: " << endl;
cout << "2. Delete Book: " << endl;
cout << "3. Update Book:" << endl;
cout << "4. print a Book:" <<endl;
cout << "5. print all Books " << endl;
cout << "6. Exit the program "<<endl;
cout << "Enter your choice here "<<endl;
cin >> choice;
switch (choice)
{
case 1:
AddBook();
break;
case 2:
DeleteBook();
break;
case 3:
UpdateBook();
break;
case 4:
PrintBook();
break;
case 5:
PrintAll();
break;
default:
cout << "Invalid Selection" << endl;
}
}
while
(choice != 6);
return 0;
}
The output looks strange in file and the output is being outputted twice on in strange chars and the other is good but ISBN is attached to title i need a solution please how i can fix it as it's not obvious where's the logical error here for me
Output
ISBN :
╞3╧v`≡o F╙v└²a
Title :
≡o F╙v└²a
Author's Name
:
Price :
F╙
Year :
oÇ≡o╘≡o
Number of Pages
ISBN :12345Jungle House
Title :Jungle House
Author's Name :ASad asad
Price :240
Year :2019
Number of Pages 300
You have many problems with the code you show, but the reason for the seemingly garbage output is that your printAll function you print the data in the Book object b before you read anything into it.
That means the object b isn't initialized yet, and will contain indeterminate data (which might seem like random or garbage). Using such values in any way leads to undefined behavior.
Related
(I'm just a guy trying to start programming so don't be too harsh, English is not my main language too)
I was trying to make a simple program about storing book into a library (with class since I started learning them) and the array won't show up in a different case other than the one in which I initialized it, the IDE doesn't show up any error too
Code:
#include <iostream>
using namespace std;
class MainMenu {
public:
int choice, ID, IDC;
string Name[500];
string Genre[500];
string Book[500];
void Menu() {
cout << "choose an action:" << endl;
cout << "1)\tadd a book\n2)\tsee my book" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "ID:" << endl;
cin >> ID;
cout << "name:" << endl;
cin >> Name[ID];
cout << "Genre" << endl;
cin >> Genre[ID];
cout << "> " << endl;
cin >> Book[ID];
cout << "<" << endl;
cout << "book successfully registered" << endl;
// assign a value about the book (name and genre) in each array stored
// with the id
break;
case 2:
cout << "ID?" << endl;
cin >> IDC; // IDC stands for ID Check, if ID == IDC then it should show
// the specs of the book + its content
cout << Name[IDC] << endl
<< Genre[IDC] << endl
<< "--> " << Book[IDC] << endl
<< endl;
break;
} // Switch
} // Menu
}; // Class
int main() {
int i = 0;
while (i == 0) // creating a loop to keep showing the menu without the end of
// the program
{
MainMenu giorgio;
giorgio.Menu(); // summoning menu
}
}
When you create a Object, all atributes are initialized, as a result, you create a emptiy arrays of your Object for each iteration, you can resolve this whit this code:
#include <iostream>
using namespace std;
class MainMenu {
public:
int choice, ID, IDC;
string Name[500];
string Genre[500];
string Book[500];
void Menu() {
cout << "choose an action:" << endl;
cout << "1)\tadd a book\n2)\tsee my book" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "ID:" << endl;
cin >> ID;
cout << "name:" << endl;
cin >> Name[ID];
cout << "Genre" << endl;
cin >> Genre[ID];
cout << "> " << endl;
cin >> Book[ID];
cout << "<" << endl;
cout << "book succesfully registered" << endl;
break;
case 2:
cout << "ID?" << endl;
cin >> IDC;
cout << Name[IDC] << endl
<< Genre[IDC] << endl
<< "--> " << Book[IDC] << endl
<< endl;
break;
}
}
};
int main() {
int i = 0;
MainMenu giorgio;
while (i == 0)
{
giorgio.Menu(); // summoning menu
}
}
I'm a novice coding student and trying to create a menu using structs, functions, and switch statements to make a mini database for a class assignment. I'm trying to implant the functions into the switch statements.
I'm getting errors on lines 87 and 137 and I'm not sure where I'm going wrong. Any help, explanation, or correction is much appreciated.
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
// Jaret Clark
// Week 3 Interactive Assignment
// INT 499
// Prof. Joseph Issa
// 03/31/2022
struct EZTechMovie {
string name;
string *cast[10];
string rating;
};
void displaymovie(EZTechMovie movie, int cast_num) {
int i;
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
cout << "Your entry:\n";
//Movies
cout << endl;
cout << "Movie TITLE: " << movie.name;
cout << endl;
//Movie rating
cout << "Movie Rating: " << movie.rating;
cout << endl;
//Cast name
cout << "Main Cast Members: \n";
//loop for cast members ~ stores in array
for (int i = 0; i < cast_num; ++i) {
cout << movie.cast[i];
cout << endl;
}
}
void mainmenu() {
string movie_input;
int m;
cout << endl;
cout << "Would you like to store movies into database? (yes or no) ";
getline(cin, movie_input);
cout << endl;
if (movie_input == "yes") {
string cont;
string cast_name;
int x, m, n, i, cast_num;
EZTechMovie moviedb[100];
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
for (n = 0; n < 100; n++) {
cout << "Movie Title: ";
getline(cin, moviedb[n].name);
cout << endl;
cout << "Movie rating: ";
getline(cin, moviedb[n].rating);
cout << endl;
cout << "How many cast do you want to enter? ";
cin >> cast_num;
cout << endl;
cin.ignore();
for (i = 0; i < cast_num; i++) {
cout << "Cast name: First and Last name: ";
getline(cin, moviedb[n].cast[i]);
cout << endl;
}
cout << endl;
displaymovie(moviedb[n], cast_num);
cout << endl;
cout << "Add more movies? (yes or no) ";
getline(cin, cont);
if (cont == "no") {
break;
}
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
}
}
else if (movie_input == "no") {
return;
}
else {
cout << "INVALID Input";
mainmenu();
}
}
// menu
void movieMenu() {
int choice;
EZTechMovie movie;
do {
cout << "***********************Welcome to EZTechMovie Movie Entry Menu***********************" << endl;
cout << "Press 1 to Enter Movie Info - Name, Cast Members, and Rating.\n";
cout << "Press 2 to Retrieve movie info recently entered.\n";
cout << "Press 3 To Quit program.\n";
// evaluate menu options in switch case
switch (choice) {
case 1:
mainmenu();
break;
case 2:
displaymovie(EZTechMovie movie, int cast_num);
break;
case 3:
cout << "Thank you and Goodbye!";
break;
default:
cout: "Invalid Selection. Try again!\n";
}
//get menu selection
cin >> choice;
} while (choice != 3);
}
int main() {
movieMenu();
}
Regarding the error on line 87 (getline(cin, moviedb[n].cast[i]);) :
moviedb[n].cast[i] is a std::string*, not std::string like you might have meant.
A quick compilation fix would be to use:
getline(cin, *(moviedb[n].cast[i]));
i.e. dereference the pointer.
However - this code raises other design/programming issues:
Why do you use std::string* and not std::string in the first place.
Why do you use C style array instead of std::vector (or std::array if you can commit to the size). This is relevant for both: string *cast[10]; and EZTechMovie moviedb[100];
Hi im creating a cinema ticket operator as a self short project but i am a bit stuck on how to make the results of what the user inputs show up at the end summed up like on a receipt. Thanks in advance.
This is my code :
#include <iostream>
#include <string>
using namespace std;
int selectStaff();
int selectDate();
int selectMovie();
int printReceipt(int a);
string name, date;
int main()
{
int movie;
selectStaff();
date = selectDate();
movie = selectMovie();
printReceipt(movie);
return 0;
}
int printReceipt(int a)
{
if (a == 1) {
cout << "Deadpool" << endl;
}
else {
cout << "Goosebumps" << endl;
}
return 0;
}
int selectStaff() {
cout << "Welcome to FILM Screen Cinema" << endl;
cout << endl;
cout << "ENTER STAFF/OPERATOR'S NAME: " << endl;
cin >> name;
return 0;
}
int selectDate() {
cout << endl;
cout << "ENTER DATE:";
cin >> date;
return 0;
}
int selectMovie() {
int movie;
cout << endl;
cout << "CHOOSE A MOVIE THAT IS SCREENING TODAY:" << endl;
cout << endl;
cout << "Press 1 for first option & 2 for second option" << endl;
cout << endl;
cout << "[1] Deadpool" << endl << "[2] Goosebumps" << endl;
cin >> movie;
return 0;
}
I want all the required input to be displayed at the bottom so when it prints, the user can view the summary. I also need a closing message that says "thank you"
You have all of the variables lined up to do this neatly and simply. Just edit your print receipt function to look something like:
Int printreceipt(int a) {
string title;
if (a==1) {
title = "Deadpool";
} else {
title == "Goosebumps";
}
cout << "Cashier: "; cout << name << endl << endl; //say the operator's name and add a line
cout << "Date: " << date << endl; //say the date
cout << "Movie: " << title; //say the title of the movie
cout << endl << endl; //add a line
cout << "Thank you for choosing this cinema! Enjoy!"; //Thank you message :D
}
To give you results similar to:
Cashier: Guy
Date: May 5, 2017
Movie: Deadpool
Thank you for choosing this cinema! Enjoy!
hello guys i'm trying to delete a full line from a ".txt" file when the user enters the book id
my text file looks like this :
[BOOK INFO]%Book Id : 1%title : Object oriented programming%[PUBLISHER
INFO]%publisher name : misr publish house%puplisher address
:adfaf%[AUTHOR(s) INFO]%Authors Name : ahmed%Nationality :
egyptian%Authors Name : eter%Nationality : american%[MORE
INFO]%PublishedAt : 3/3/2006%status :6.
[BOOK INFO]%Book Id : 2%title : automate%[PUBLISHER INFO]%publisher
name : misr%puplisher address :nasr city%[AUTHOR(s) INFO]%Authors Name
: ahmed khaled%Nationality : egyptian%Authors Name : ohammed
adel%Nationality : egyptian%[MORE INFO]%PublishedAt : 3/8/2005%status
:15.
the line starts from [book info] to the (.)
i should be able to delete the whole line when the user enter the id
but i don't know how or what function to use
my code is :
/*password is admin*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include<stdlib.h>
#include<iomanip>
#include<conio.h>
#define F1 59
#define ESC 27
using namespace std;
void list_all_books_1();
void list_available_books_2();
void borrow_books_3();
void search_for_abook_4();
void add_new_book_5();
void delete_books_6();
fstream d_base;
char path[] = "library books.txt";
void output(){
//this function for displaying choices only
cout << setw(77) << "***********************************" << endl;
cout << setw(71) << "1. List all books in library" << endl;
cout << setw(77) << "2. List available books to borrow " << endl;
cout << setw(72) << "3. Borrow a Book from library" << endl;
cout << setw(63) << "4. Search For a Book" << endl;
cout << setw(59) << "5. Add New Books" << endl;
cout << setw(59) << "6. Delete a Book" << endl;
cout << setw(62) << "7. EXIT The Library" << endl;
cout << setw(77) << "***********************************" << endl;
}
//=====================================================================================================================================================
struct books{
//identfying books with all needed things
string id, status;
string title, p_name, p_address;
string date;
};
struct author{
string aut_name;
string aut_nationality;
};
//=====================================================================================================================================================
//function for choice 1 showing the books
void list_all_books_1(){
ifstream show;
char all;
show.open(path, ios::in | ios::app);
while (!show.eof()){
show >> std::noskipws;
show >> all;
if (all == '%')
cout << "\n";
else if (all == '.')
cout << "\n\n\n";
else
cout << all;
}
cout << endl;
show.close();
}
//=====================================================================================================================================================
void list_available_books_2(){
ifstream available_books;
char x;
available_books.open(path, ios::in | ios::app);
while (!available_books.eof()){
available_books >> std::noskipws;
available_books >> x;
if (x == '%')
cout << "\n";
else if (x == '.')
cout << "\n\n\n";
else
cout << x;
}
cout << endl;
available_books.close();
}
//=====================================================================================================================================================
void borrow_books_3(){
}
//=====================================================================================================================================================
void search_for_abook_4(){
string idx;
int offset, i = 0;
string line;
cout << "enter the ID of the book you're looking for";
cin >> idx;
d_base.open(path, ios::in | ios::app);
while (!d_base.eof()){
getline(d_base, line);
if (((offset = line.find(idx, 0))) != string::npos){
cout << "Book found" << endl;
i++;
d_base.close();
}
}
if (i == 0){
cout << "couldn't find book" << endl << endl;
}
}
//=====================================================================================================================================================
//for choice 5 to fill books
void add_new_book_5(){
int aut_number, booksnumber;
books newbook[1000];
author aut[100];
cout << "how many books you want to add ? ";
cin >> booksnumber;
cout << "what books you want to add :" << endl;
d_base.open(path, ios::out | ios::app);
for (int i = 0; i < booksnumber; i++){
cout << "id please : "; cin >> newbook[i].id;
cout << "title : "; cin.ignore(); getline(cin, newbook[i].title);
cout << "publisher name :"; getline(cin, newbook[i].p_name);
cout << "publisher address : "; getline(cin, newbook[i].p_address);
d_base << "[BOOK INFO]" << "%Book Id : " << newbook[i].id << "%title : " << newbook[i].title;
d_base << "%[PUBLISHER INFO]" << "%publisher name : " << newbook[i].p_name << "%puplisher address :" << newbook[i].p_address;
d_base << "%[AUTHOR(s) INFO]";
cout << "how many authors for the books";
cin >> aut_number;
for (int j = 0; j < aut_number; j++){
cout << "author" << j + 1 << " name : "; cin.ignore(); getline(cin, aut[j].aut_name);
cout << "Nationality : "; getline(cin, aut[j].aut_nationality);
d_base << "% Authors Name : " << aut[j].aut_name << "% Nationality : " << aut[j].aut_nationality;
}
cout << "Publish date :"; getline(cin, newbook[i].date);
cout << "How many copies of " << newbook[i].title << " "; cin >> newbook[i].status;
d_base << "%[MORE INFO]";
d_base << "%PublishedAt : " << newbook[i].date << "%status :" << newbook[i].status << "." << endl;
}
d_base.close();
cout <<setw(76)<< "Books Have Been Saved Sucessfully" << endl;
}
//=====================================================================================================================================================
void delete_books_6(){
//deleting a book
}
//=====================================================================================================================================================
int main(){
char choice;
cout << "\n\n" << setw(76) << "{ welcome to FCIS library }\n\n";
do{
output();
cout << "- what do you want to do ? ";
cin >> choice;
if (choice == '1'){
system("cls");
list_all_books_1();
}
//this one for list available books
else if (choice == '2'){
system("cls");
list_available_books_2();
}
//this one for borrow a book
else if (choice == '3'){
system("cls");
borrow_books_3();
}
else if (choice == '4'){
system("cls");
search_for_abook_4();
}
//this one is for adding new books to the list
else if (choice == '5'){
system("cls");
string pass;
do{
cout << "you must be an admin to add new books." << endl << "please enter passowrd (use small letters) : ";
cin >> pass;
if (pass == "b")
break;
else if (pass == "admin"){
system("cls");
cout << "ACCESS GAINED WELCOME " << endl;
add_new_book_5();
}
else{
cout << "Wrong password try again or press (b) to try another choice";
continue;
}
} while (pass != "admin");
}
//this one for deleteing a book
else if (choice == '6'){
system("cls");
//not completed yet
}
else if (choice == '7'){
system("cls");
cout << "\n\n\n"<<setw(76) << "Thanks for Using FCIS LIBRARY" << endl;
break;
}
else
cout << "\nwrong choice please choose again\n\n";
} while (_getch()!=27);
}
i tried to use get line and search for matching id the delete the line if there's match but couldn't accomplish it
i'm beginner at c++ by the way
Read the whole file into a memory buffer. Delete what you don't want. Overwrite the existing file with the contents of your memory buffer.
You've now deleted what you did not want from the file.
So I'm creating this for an assignment in Uni.
It has to use a class structure with 'Vehicle' as the main class, and two classes which inherit some functions from it; 'Car' and 'Lorry'.
So I've created my class structure successfully (I think) but I need to create a basic UI for this, bearing it mind it is a console application, I am just creating a basic menu using switches.
How ever, in my "getDetails" section, it is only grabbing the 'Vehicle::getDetails' when it should be also getting the 'Car/lorry::getdetails', as well as the vehicle details.
Any ideas what could be causing that?
First post here, so sorry if my post is bad :(.
Thanks!
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class vehicle {
public:
string manufacturer;
int year;
string regnum;
void getDetails() {
cout << "Please enter the details for your vehicle"<< endl;
cout << "Please enter the manufacturer of your vehicle: "<< endl;
cin >> manufacturer;
cout << "Please enter the year of your vehicle's manufacture: "<< endl;
cin >> year;
cout << "Please enter your vehicle's registration number: "<< endl;
cin >> regnum;
}
void printDetails() {
cout << "Your vehicle's details are as follows: " << endl;
cout << "Your Vehicle's manufacturer is " << manufacturer << endl;
cout << "Your Vehicle's year of manufacture is " << year << endl;
cout << "Your Vehicle's registration number is " << regnum << endl;
}
void saveDetails() {
ofstream vehiclefile;
vehiclefile.open ("vehicle.txt");
vehiclefile << "***Your Vehicle's Details***" << endl;
vehiclefile << "Manufacturer:" << manufacturer << endl;
vehiclefile << "Year of Manufacture:" << year << endl;
vehiclefile << "Registration Number: " << regnum << endl;
vehiclefile.close();
}
void openVehicleDetails() {
}
};
class car : public vehicle{
public:
int numpassengers;
string cartype;
void getDetails() {
vehicle::getDetails();
cout << "Please enter the number of maximum passengers your car can hold: "<< endl;
cin >> numpassengers;
cout << "Please enter the car body type: "<< endl;
cin >> cartype;
cout << "Thank your for your details"<< endl;
}
void printDetails() {
vehicle::printDetails();
cout << "Your car's maximum passengers is: " << numpassengers << endl;
cout << "The body type of your car is: " << cartype << endl;
}
void saveDetails() {
vehicle::saveDetails();
ofstream vehiclefile;
vehiclefile.open ("vehicle.txt");
vehiclefile << "Car or Lorry: Car" << endl;
vehiclefile << "Number of passengers: " << numpassengers << endl;
vehiclefile << "Type of car: " << cartype << endl;
vehiclefile.close();
}
};
class lorry : public vehicle{
public:
double tonnage;
string bodtype;
void getDetails() {
vehicle::getDetails();
cout << "Please enter the gross weight of your Lorry: "<< endl;
cin >> tonnage;
cout << "Please enter the body type of your Lorry: "<< endl;
cin >> bodtype;
cout << "Thank your for your details"<< endl;
}
void printDetails() {
vehicle::printDetails();
cout << "Your lorry's details are as follows: " << endl;
cout << "Your lorry's maximum weight is: " << tonnage << endl;
cout << "The body type of your lorry is: " << bodtype << endl;
}
void saveDetails() {
vehicle::saveDetails();
ofstream vehiclefile;
vehiclefile.open ("vehicle.txt");
vehiclefile << "Car or Lorry: Lorry" << endl;
vehiclefile << "Maximum weight: " << tonnage << endl;
vehiclefile << "Body type: " << bodtype << endl;
vehiclefile.close();
}
};
int main () {
int flag = 0;
char choice;
int ifchoice;
vehicle*v;
while (flag == 0){
cout << "***Main Menu***" << endl; //Menu to allow ease of access within the program.
cout << "Select by letter:" << endl;
cout << "1 - Add new entry" << endl;
cout << "2 - Show entry" << endl;
cout << "3 - Save entry" << endl;
cout << "4 - Open saved document" << endl;
cout << "5 - Delete entry" << endl;
cin >> choice;
switch(choice) {
case '1':
cout << "Is your vehicle a Car or a Lorry? " << endl;
cout << "Press '1' for Car " << endl;
cout << "Press '2' for Lorry " << endl;
cin >> ifchoice;
if (ifchoice == 1)
{
v = new car();
}
if (ifchoice == 2)
{
v = new lorry();
}
v->getDetails();
break;
case '2':
v -> printDetails();
break;
case '3':
v -> saveDetails();
break;
}
}
}
The key point of the exercise is to make the void getDetails() method virtual in the base vehicle class.
(I'd also define a virtual destructor in the vehicle class as well, as good coding practice.)
class vehicle {
public:
string manufacturer;
int year;
string regnum;
// Make this virtual
virtual void getDetails() {
...
In the derived classes you may want to use the new C++11 override specifier as well.
In c++, if you want a function to be overridable by a deriving class, you label it virtual, i.e.:
class vehicle {
...
virtual void getDetails()
{
...
}
...
}