Reputation: 3
Ok,so I have a struct and I need to create a function that increases the amount of a book.After calling the function for each book ,I will call printBooks which I can do just fine.I know it's a very simple program but I just couldn't do it so any help is appreciated
#include <iostream>
using namespace std;
#define BOOKS 3
struct Book
{
string title;
string isbn;
int amount;
} books [BOOKS];
void printBooks(Book b);
void addAmount(Book &book,int amount);
int main()
{
int i;
for(int i = 0;i < BOOKS; i++)
{
cout << "Enter isbn : ";
cin >> books[i].isbn;
cout << "Enter title : ";
cin >> books[i].title;
cout << "Enter amount : ";
cin >> books[i].amount;
}
cout << "\nThe number of books after adding amount by one :\n";
for(i = 0;i < BOOKS; i++)
{
addAmount(); // intentionally left blank.don't know what to put
printBooks(books[i]);
}
return 0;
}
void printBooks(Book b)
{
cout << b.isbn << endl;
cout << b.title << endl;
cout << b.amount << endl;
}
void addAmount(Book &book,int amount)
{
book.amount++;
}
Upvotes: 0
Views: 4344
Reputation: 10541
void addAmount(Book &book)
{
book.amount++;
}
and then
addAmount(books[i]);
Btw, you don't really need to pass a structure by value to printBooks
. Just use a const reference instead. The calling code doesn't need to be modified.
void printBooks(const Book &b)
{
cout << b.isbn << endl;
cout << b.title << endl;
cout << b.amount << endl;
}
Upvotes: 1
Reputation: 258548
You're calling addAmount();
without parameters. You probably mean
addAmount(books[i], 42);
Also consider changing the printBooks
signature to
void printBook(const Book& b)
or, even better, making it a member function.
Upvotes: 6