السلام عليكم ..
المشكلة في الكود انه لا يظهر الuser input لكن يظهر كل شيء تاني. ايش ممكن تكون المشكلة؟
The header file
#ifndef BOOKTYPE_H
#define BOOKTYPE_H
#include <iostream>
#include <string>
using namespace std;
class bookType
{
public:
// constructor
bookType();
bookType(string title , int noOfAuthors,
string authors[4], string publisher,
int ISBN, double price, int noOfCopies);
// functions
void setTitle(string t); // sets title
string getTitle() const; // returns title
void showTitle(); // shows title
void setNoOfAuthors(int numA); // sets no of authors
int getNoOfAuthors(); // returns no of authors
void showNoOfAuthors(); // shows no of authors
void setAuthors(string authors[4]); // sets authors array
string getAuthors() const; // returns authors
void showAuthors(); // shows authors
void setPublisher(string pblshr); // sets publisher
string getPublisher() const; // returns publisher
void showPublisher(); // shows publisher
void setISBN(int isbn); // sets isbn
int getISBN() const; // returns isbn
void showISBN(); // shows isbn
void setPrice(int pr); // sets price
double getPrice() const; // returns price
void showPrice(); // shows price
void setCopies(int numC); // sets no of copies
int getCopies() const; // returns no of copies
void showCopies(); // shows no of copies
void printBookInfo(); // prints books information
void updateNoOfCopies(int n); // updates no of copies of books
void search(char respnse); // searches by title or ISBN
private:
// attributes
string title, authors[4], publisher;
int ISBN, noOfCopies, noOfAuthors, var, n;
double price;
char response;
};
#endif // BOOKTYPE_H
The cpp file
#include "bookType.h"
bookType::bookType()
{
title = "";
noOfAuthors = 0;
authors[0] = "";
authors[1] = "";
authors[2] = "";
authors[3] = "";
publisher = "";
ISBN = 0;
price = 0.0;
noOfCopies = 0;
}
bookType::bookType(string title, int noOfAuthors,
string authors[4], string publisher,
int ISBN, double price, int noOfCopies)
{
title = "";
noOfAuthors = 0;
authors[0] = "";
authors[1] = "";
authors[2] = "";
authors[3] = "";
publisher = "";
ISBN = 0;
price = 0.0;
noOfCopies = 0;
} // end constructor
bookType books[5];
void bookType::setTitle(string t)
{
title = t;
}
string bookType::getTitle() const
{
return title;
}
void bookType::showTitle()
{
cout << "\nTitle: ";
cout << title;
}
void bookType::setNoOfAuthors(int numA)
{
if(numA >= 0) {
noOfAuthors = numA;
} else
noOfAuthors = 0;
}
int bookType::getNoOfAuthors()
{
return noOfAuthors;
}
void bookType::showNoOfAuthors()
{
cout << "\nNumber of Authors: " << noOfAuthors << endl;
}
void bookType::setAuthors(string authors[4])
{
for(int i = 0; i < 4; i++) {
cout << "\nAuthor " << i + 1 << ": ";
cin >> authors[i];
} // end for
}
string bookType::getAuthors() const
{
//return authorName;
for(int i = 0; i < 4; i++) {
return authors[i];
}
}
void bookType::showAuthors()
{
cout <<"\nAuthor(s)";
if (authors[0] != "")
cout << authors[0];
if (authors[1] != "")
cout <<", "<< authors[1];
if (authors[2] != "")
cout <<", "<< authors[2];
if (authors[3] != "")
cout <<", "<< authors[3];
cout << endl;
}
void bookType::setPublisher(string pblshr)
{
publisher = pblshr;
}
string bookType::getPublisher() const
{
return publisher;
}
void bookType::showPublisher()
{
cout << "\nPublisher: " ;
cout << publisher;
}
void bookType::setISBN(int isbn)
{
ISBN = isbn;
}
int bookType::getISBN() const
{
return ISBN;
}
void bookType::showISBN()
{
cout << "\nISBN: ";
cout << ISBN;
}
void bookType::setPrice(int pr)
{
if(pr >= 0) {
price = pr;
} else
price = 0;
}
double bookType::getPrice() const
{
return price;
}
void bookType::showPrice()
{
cout << "\nPrice: ";
cout << price;
}
void bookType::setCopies(int numC)
{
if(numC >= 0) {
noOfCopies = numC;
} else
noOfCopies = 0;
}
int bookType::getCopies() const
{
return noOfCopies;
}
void bookType::showCopies()
{
cout << "\nNumber of Copies: ";
cout << noOfCopies;
}
void bookType::printBookInfo()
{ showTitle();
showNoOfAuthors();
showAuthors();
showPublisher();
showISBN();
showPrice();
showCopies();
} // end printBookInfo
void bookType::updateNoOfCopies(int n)
{
n = noOfCopies;
}
void bookType::search(char response) // searches by title or ISBN
{ if(response == 'y')
{
cout << "Enter 1 to search by title or 2 by ISBN: ";
cin >> var;
switch(var) {
case 1:
{
cout << "Enter book title: ";
string newTitle;
cin >> newTitle;
for(int j = 0; j < 5; j++) {
string temp = books[j].getTitle();
if(newTitle == temp) {
books[j].printBookInfo();
} // end if
} // end for loop
break;
} // end case1
case 2 :
{
cout << "Enter book ISBN: ";
int newISBN;
cin >> newISBN;
for(int i = 0; i < 5; i++) {
int temp = books[i].getISBN();
if(newISBN = temp) {
books[i].printBookInfo();
} // end if
} // end for loop
break;
} // end case2
default :
{
cout << "Invalid Input!";
} // end default
} // end switch
}// end if
else
cout << "Thank you!";
} // end search
The main file
#include <iostream>
#include "bookType.h"
int main()
{
cout << "\nThis program prints books information.";
// attributes
string title, authors[4], publisher, authorName;
int ISBN, noOfCopies, noOfAuthors;
double price;
char response;
bookType books[5];
for(int i = 0; i<2; i++)
{
cout << "\n*********************************";
cout << "\nEnter book title: ";
cin >> title; // reads title
cout << "\nEnter number of authors(1-4): ";
cin >> noOfAuthors; // reads noOfAuthors
cout << "\nEnter name(s) of authors: ";
for(int i = 0; i < noOfAuthors; i++) {
cout << "Author " << i + 1 << ": ";
cin >> authors[i];
} // end for
cout << "\nEnter book publisher: ";
cin >> publisher; // reads publisher
cout << "\nEnter book ISBN: ";
cin >> ISBN; // reads ISBN
cout << "\nEnter book price: ";
cin >> price; // reads price
cout << "\nEnter number of book copies: ";
cin >> noOfCopies; // reads noOfCopies
bookType b(title, noOfAuthors, authors, publisher, ISBN, price, noOfCopies);
books[i] = b;
}
cout << "\nBooks information is displayed below.";
for(int j = 0; j<2; j++)
{
books[j].printBookInfo();
cout << "\n***************";
}
bookType b1;
cout << "\nWould you like to search for a book ?";
cout << "Enter y for yes and n for no: ";
cin >> response;
b1.search(response);
return 0;
}
Input:
This program prints books information.
*********************************
Enter book title: sth
Enter number of authors(1-4): 1
Enter name(s) of authors: Author 1: hh
Enter book publisher: aa
Enter book ISBN: 111
Enter book price: 11
Enter number of book copies: 1
*********************************
Enter book title: sth1
Enter number of authors(1-4): 1
Enter name(s) of authors: Author 1: aa
Enter book publisher: aa
Enter book ISBN: 111
Enter book price: 11
Enter number of book copies: 1
Output:
Books information is displayed below.
Title:
Number of Authors: 14703328
(Author(s
Publisher:
ISBN: 1023
Price: 1.63536e-321
Number of Copies: 0
***************
Title:
Number of Authors: 14703328
Author(s)
Publisher:
ISBN: 1023
Price: 1.63536e-321
Number of Copies: 0