#include<iostream.h>
#include<string>


class books
{
char  author[20],title[100],publisher[30];
float price;
int stock;


public:
void display();


void getdata(void)
{
char title[100],author[20];
cout<<"Name/Title of the Book--->";
cin>>title;
cout<<"\n\nAuthor of the Book------->";
cin>>author;
}
void stock()
{
char choice='y',another;
while(choice=='y')
{
cout<<"\nEnter the following into Stock....\n";


cout<<"\nAuthor-------->";
cin>>author;


cout<<"\nTitle--------->";
cin>>title;


cout<<"\nPrice--------->";
cin>>price;


cout<<"\nPublisher----->";
cin>>publisher;


cout<<"\nNo of Copies-->";
cin>>stock;


cout<<"\n\nAdd another record(y/n)--->";
fflush(stdin);
another=getche();
//cin>>another;
}
}


void match()
{

in void match()--->I want to match void getdata() and void stock(). How I can do this.
Pls help me

Dani AI

Generated

As pointed out, the core problem is scope: your getdata() reads into local variables, so match() cannot see those values. Fix the design so data lives where you need it. Two clear options:

  • Make book fields (title, author, etc.) member variables and have getdata() store into them.
  • Or have getdata() produce/return a Book record (or take references) and keep your stock as a container of Book objects; then match() searches that container.

A simple, modern approach uses std::string and a std::vector for the inventory. This avoids fixed-size buffers and unsafe I/O. Example:

#include <iostream>
#include <string>
#include <vector>
#include <limits>

struct Book {
    std::string title;
    std::string author;
    std::string publisher;
    double price = 0.0;
    int stock = 0;
};

class Inventory {
    std::vector<Book> items;
public:
    void addBook() {
        Book b;
        std::cout << "Title: ";
        std::getline(std::cin, b.title);
        std::cout << "Author: ";
        std::getline(std::cin, b.author);
        std::cout << "Publisher: ";
        std::getline(std::cin, b.publisher);
        std::cout << "Price: ";
        std::cin >> b.price;
        std::cout << "No of copies: ";
        std::cin >> b.stock;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear newline
        items.push_back(std::move(b));
    }

    Book* find(const std::string& title, const std::string& author) {
        for (auto &b : items)
            if (b.title == title && b.author == author)
                return &b;
        return nullptr;
    }
};

Notes and cautions: prefer std::getline and std::cin.ignore for string input (see std::getline). Avoid fflush(stdin) and nonstandard functions like getche()fflush on input streams is not portable (see fflush behavior). For robust matching consider trimming whitespace and doing case-insensitive compares before comparing strings. This approach keeps getdata/stock responsibilities clear and makes match simply a search over stored records.

Recommended Answers

All 2 Replies

wrap this in [code=cpp] [/code] tags, with indents. And what do you mean you "want to match void getdata() and void stock()"?

in void match()--->I want to match void getdata() and void stock(). How I can do this.

You have a bit of work to do. In getdata() you are reading the book details into local variables, so match() can't access it. One approach would be to make title[] and author[] into class members (rename them so as not to clash with the existing names). In match() you can then compare your getdata() input with the existing stock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.