i'm trying to load the file into an array when the program starts so i can
modify or search in it i don't know if my code works or not ( it's not reading the file )i have the file
and there's two books in
i have tried to debug it but couldn't find the problem the code works
but it think there's a problem with the load() function i don't know what
my code :
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct books{
//identfying books with all needed things
int id, status;
string title, p_name, p_address;
string date;
string aut_name, aut_nationality;
}newbook[10000], aut[10000];
int i = 0;
void load(){
ifstream myfile("books.txt", ios::in);
while (myfile >> newbook[i].id >> newbook[i].title >> newbook[i].p_name >> newbook[i].p_address >> aut[i].aut_name >> aut[i].aut_nationality >> newbook[i].date >> newbook[i].status)
i++;
}
void search_for_book(){
int temp = 0;
int idx;
cout << "enter the ID of the book you're looking for : ";
cin >> idx;
for (int srh = 0; srh < i; srh++){
if (newbook[srh].id == idx){
cout << setw(10) << "book found :" << endl;
cout << "title :" << newbook[srh].title << endl;
cout << "publisher name : " << newbook[srh].p_name << endl;
cout << "publisher address" << newbook[srh].p_address << endl;
cout << "author name :" << aut[srh].aut_name << endl;
cout << "author Nationality :" << aut[srh].aut_nationality << endl;
cout << "publish Date :" << newbook[srh].date << endl;
cout << "status :" << newbook[srh].status << endl;
temp++;
break;
}
else
srh++;
}
if (temp == 0){
cout << "couldn't find book" << endl << endl;
}
}
int main(){
load();
char choice;
cout << "enter your choice (3)";
cin >> choice;
if (choice == '3'){
search_for_book();
}
}
note :*( there are other functions like adding new book but not necessary to write )
*(i'm new to c++ don't really know how to read file into memory but i'm trying)
this is the code to save data into file :
void add_new_book_5(){
int booksnumber;
books newbook[1000], 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);
cout << "author" << " name : "; cin.ignore(); getline(cin, newbook[i].aut_name);
cout << "Nationality : "; getline(cin, newbook[i].aut_nationality);
cout << "Publish date :"; getline(cin, newbook[i].date);
cout << "How many copies of " << newbook[i].title << " "; cin >> newbook[i].status;
system("cls");
d_base << newbook[i].id << "\ " << newbook[i].title << "\ ";
d_base << newbook[i].p_name << "\ " << newbook[i].p_address << "\ ";
d_base << newbook[i].aut_name << "\ " << newbook[i].aut_nationality << "\ ";
d_base << newbook[i].date << "\ " << newbook[i].status << endl;
}
d_base.close();
cout << setw(76) << "Books Have Been Saved Sucessfully" << endl;
}
Related
I have a problem in outputting my code to a text file. I just wanted to print a title at the beginning of a .txt file, but it happened to end up printing along with new lines of data. Basically, I am appending new data to the text file using ios::app. How can I achieve not printing my title in the text file all over again after appending other data?
Text File Output
Here is my code btw...
#include <iostream>
#include <fstream>
#include <string>
void QuaranDiary();
struct quarantine3 //structure for QuaranDiary
{
string QuaranDiaryMonth;
string QuaranDiaryDay;
string QuaranDiaryYear;
string QuaranDiaryHour;
string QuaranDiaryMinute;
string QuaranDiaryUpdate;
};
quarantine3 diary[500];
//QuaranDiary
void QuaranDiary()
{
int i = 0, update = 0;
char QuaranDiaryMore;
do
{
cout << "=====================================================================================" << endl;
cout << " COVID-19 QUARANDIARY " << endl;
cout << "=====================================================================================" << endl << endl;
cout << "DATE (MM/DD/YYYY)" << endl << endl;
cout << "Month : ";
cin >> diary[i].QuaranDiaryMonth;
cout << "Day : ";
cin >> diary[i].QuaranDiaryDay;
cout << "Year : ";
cin >> diary[i].QuaranDiaryYear;
cout << endl;
cout << "[" << diary[i].QuaranDiaryMonth << "/" << diary[i].QuaranDiaryDay << "/" << diary[i].QuaranDiaryYear << "]" << endl << endl;
cout << "TIME (23:59)"<<endl<<endl;
cout << "Hour : ";
cin >> diary[i].QuaranDiaryHour;
cout << "Minute : ";
cin >> diary[i].QuaranDiaryMinute;
cout << endl;
cout << "[" << diary[i].QuaranDiaryHour << ":" << diary[i].QuaranDiaryMinute << "]" << endl << endl;
cout << "Health Update: ";
cin.ignore();
getline(cin, diary[i].QuaranDiaryUpdate);
update++;
cout << "\n\nAdd More to QuaranDiary? [Y/N]: ";
cin >> QuaranDiaryMore;
QuaranDiaryMore = toupper(QuaranDiaryMore);
while (toupper(QuaranDiaryMore) != 'Y' && toupper(QuaranDiaryMore) != 'N')
{
cout << "\nWrong Input. Try Again!\n" << endl;
cin.clear();
cin.ignore();
cout << "Add More to QuaranDiary? [Y/N]: ";
cin >> QuaranDiaryMore;
}
cin.clear();
cin.ignore();
system("CLS");
//fstream for QuaranDiary
ofstream QuaranDiary;
QuaranDiary.open("QuaranDiary.txt", ios::app);
QuaranDiary << "=====================================================================================" << endl;
QuaranDiary << " COVID-19 QUARANDIARY " << endl;
QuaranDiary << "=====================================================================================" << endl;;
QuaranDiary << "\nDate: " << diary[i].QuaranDiaryMonth << "/" << diary[i].QuaranDiaryDay << "/" << diary[i].QuaranDiaryYear;
QuaranDiary << "\tTime: " << diary[i].QuaranDiaryHour << ":" << diary[i].QuaranDiaryMinute << endl << endl;
QuaranDiary << "[Health Update]\n\n" << diary[i].QuaranDiaryUpdate << endl;
QuaranDiary << "=====================================================================================" << endl;
if (QuaranDiary.is_open())
{
cout << "\nCheck QuaranDiary.txt for your written health updates!" << endl;
}
else
{
cout << "\n\nUnable to Open File!" << endl;
}
QuaranDiary.close();
} while (toupper(QuaranDiaryMore == 'Y'));
}
I have a big problem...
Just run the program and go to "New Gun", then "Handgun".
The problem comes when you have to enter the model.
For example, if i enter "Desert Eagle", in the text file it outputs only "Eagle". Its strange, and i cant solve it.
code:
#include <fstream>
#include <iostream>
#include <windows.h>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
void setcolor(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
int main (){
system("chcp 1251 > nul");
system("title GunnZone");
string gunModel;
int gunManufactureYear;
int itemNumber;
double gunWeight;
double price;
ofstream myfileHandguns("Handguns.txt", ios::app);
ofstream myfileRifles("Rifles.txt", ios::app);
ofstream myfileShotguns("Shotguns.txt", ios::app);
ofstream myfileSnipers("Snipers.txt", ios::app);
ofstream myfileGranades("Granades.txt", ios::app);
ofstream myfileExplosives("Explosives.txt", ios::app);
int choice;
cout << "1.New Gun" << endl;
cout << "2.List Guns" << endl;
cout << "3.Exit" << endl << endl;
cout << "Enter your choice: ";
cin >> choice;
if(choice == 1){
system("cls");
int gunTypeChoice;
cout << "1.Handgun" << endl;
cout << "2.Rifle" << endl;
cout << "3.Shotgun" << endl;
cout << "4.Sniper" << endl;
cout << "5.Granade" << endl;
cout << "6.Explosives" << endl << endl;
cout << "Enter your choice: ";
cin >> gunTypeChoice;
if(gunTypeChoice == 1){
system("cls");
cout << "Model: ";
cin >> gunModel;
getline(cin, gunModel);
cout << endl << endl;
cout << "Year of Manufacture: ";
cin >> gunManufactureYear;
cout << endl << endl;
cout << "Weight: ";
cin >> gunWeight;
cout << endl << endl;
cout << "Item Number: ";
cin >> itemNumber;
cout << endl << endl;
cout << "Price: ";
cin >> price;
myfileHandguns << "Model: " << gunModel << "\n\n";
myfileHandguns << "Year of Manufacture: " << gunManufactureYear << "\n\n";
myfileHandguns << "Weight: " << gunWeight << " g" << "\n\n";
myfileHandguns << "Item Number: " << itemNumber << "\n\n";
myfileHandguns << "Price: " << price << "$" << "\n\n";
myfileHandguns.close();
}
}
system("pause > nul");
return 0;
}
Remove >> operator as suggested. Use clear and ignore to clear errors skip new line characters \n
cout << "Model: ";
//cin >> gunModel;
std::cin.clear();
std::cin.ignore(0xffff, '\n');
std::getline(cin, gunModel);
cout << "Testing... you entered " << gunModel << endl;
cout << endl << endl;
See also Why would we call cin.clear() and cin.ignore() after reading input?
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.
For my assignment, I have to make a customer card with some info required. I am able to run the program just fine, but when I re-run it with the "Do you wish to run again? (Y/N)", the first 2 questions appear on the same line and it messes up the program. Could anyone help me fix this issue?
#include <iostream>
#include <cstdlib>
#include <iomanip>
int main()
{
char answer;
system("CLS");
cout << "*********************************************" << endl;
cout << "*********************************************" << endl;
cout << "*** W E L C O M E ! ***" << endl;
cout << "*** In this program you will be creating ***" << endl;
cout << "*** a Customer Contact card! ***" << endl;
cout << "*********************************************" << endl;
cout << "*********************************************" << endl;
system("pause");
do
{
string name;
string city;
string address;
string state;
string phone_number;
string zip;
system("CLS");
cout << endl;
cout << "Enter the name of your contact : ";
getline(cin,name);
cout << "Enter your contact's phone number : ";
getline(cin,phone_number);
cout << "Enter your contact's address : ";
getline(cin,address);
cout << "Enter the city your contact lives in : ";
getline(cin,city);
cout << "Enter the state your contact lives in (Enter the abbreviation) : ";
getline(cin,state);
cout << "Enter your contact's zip code : ";
cin >> zip;
system("pause");
system("CLS");
cout << "*********************************************" << endl;
cout << "*** ***" << endl;
cout << "*********************************************" << endl;
cout << "*** " << name << setw(41- name.length()) << "***" << endl;
cout << "*** " << address << setw(41- address.length()) << "***" << endl;
cout << "*** " << city << " " << state << " , " << zip << setw(30- city.length()) << "***" << endl;
cout << "*** " << state << setw(41- state.length()) << "***" << endl;
cout << "*** ";
cout<<"(";
for(int i = 0; i < 3; i++) {
cout << phone_number[i];
}
cout << ")";
for(int i = 3; i < 6; i++) {
cout << phone_number[i];
}
cout << "-";
for(int i = 6; i < 10; i++) {
cout << phone_number[i];
}
cout << setw(38- phone_number.length()) << "***" << endl;
cout << "*** " << zip << setw(41- zip.length()) << "***" << endl;
cout << "*********************************************" << endl;
cout << "*********************************************" << endl;
cout << endl;
cout << "Do you want to create another contact card? (Y/N)" << endl;
cin >> answer;
}
while (answer == 'Y' || answer == 'y');
return 0;
}
You are mixing cin >> and getline(cin,. So the newline was still stuck in cin by the time you wanted to read the first question's answer on the second run.
Stick to one or the other and this confusing behavior shouldn't present itself.
As mentioned in this answer: you could also add the following after cin >> answer to clear cin up to and including the newline:
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
my program seems to want to enter two inputs for name variable instead of just entering one thing and moving on to phone number?
i'm sure its simple but can someone help me fix this please? is it something it do with the getline?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//define Car struct
struct Speaker
{
string name;
string phoneNumber;
string emailAddress;
string theme;
double fee;
};
Speaker *getSpeaker();
int main()
{
Speaker thespeaker;
thespeaker = *getSpeaker();
cout << "The speaker entered is!" << endl;
cout << thespeaker.name << endl;
cout << "phone number: " << thespeaker.phoneNumber << endl;
cout << "email: " << thespeaker.emailAddress << endl;
cout << "theme: " << thespeaker.theme << endl;
cout << "fees: " << thespeaker.fee << endl;
}
Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cin.ignore(100, '\n');
cin.clear();
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;
return theSpeaker;
}
There's no need for cin.ignore();
Simply write it as:
Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;
return theSpeaker;
}