class does not work

I am writing a code for reading a file and input and what not... I know that im not finish but I have a bump on the road. on the switch stament i have an error
stating that member functions re declaration not allowed why is that? please help me resolve this problem. Thanks in advanced.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  
  #include "library.h"

using namespace std;

void PrintMenu()
{
	std::cout << "welcome to sample library what would you like to do \n\n --------" << std::endl;
	std::cout << "enter 1) if you wish to search  book titles " << std::endl;
	std::cout << "enter 2) if you wish to search  book price of the book " << std::endl;
	std::cout << "enter 3) if you wish to search  book author  " << std::endl;
	std::cout << "enter 4) if you wish to search  book year " << endl;
	std::cout << "enter 0) if you wish to exit the program " << endl;
}


int GetUserInput()
{
	PrintMenu();

	int choice;
	std::cin >> choice;

	while ((choice <0 || choice >4) || !std::cin) // incase the user enters an invalid number
	{
		if (!std::cin)
		{
			std::cin.clear();  std::cin.ignore(1000, '\n');
		}

		PrintMenu();
		std::cin >> choice;
	}

	
	return choice;
}



int main()
{
	cout << setw(4) << setprecision(4) << left << endl;


	////usage 
	int userChoice = GetUserInput();

	string line; // to read the files

	vector<Book> lib_book;


	ifstream infile;  // opening the file

	infile.open("books.txt"); // name of file


	// switch statment for the users input
	switch (userChoice)
	{

	case 1:
		void Book::titlesrch();
		break;

	case 2:
		void Book::pricesrch();
		break;

	case 3:
		void Book::authrch();
		break;

	case 4:
		void Book::yearsrch();
		break;

	case 5:
		cout << "\n Thank you " << endl;
		exit(0);
		break;
	}

	system("pause");
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "library.h"


Book::Book()
{
	amt = 0.0;
	title = " ";
	author = " ";
	int year = 0;
}


void Book::setamt(double am)
{
	amt = am;
}

double Book::getamt()
{
	return amt;
}


void Book::settitle(string titl)
{
	title = titl;
}

string Book::gettitle()
{
	return title;
}


void Book::setauth(string aut)
{
	author = aut;
}

string Book::getauth()
{
	return author;
}

void Book::titlesrch()
{
	cout << "you chosed to search by TITLE" << endl;
}
void Book::pricesrch()
{
	cout << "you chosed to search by PRICE" << endl;
}
void Book::authrch()
{
	cout << "you chosed to search by AUTHOR" << endl;
}
void Book::yearsrch()
{
	cout << "you chosed to search by YEAR" << endl;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <vector>


using namespace std;

class Book
{
private:

	double amt; // book amount
	string title; // book title
	string author; // name of author
	int year; // year of the book


public:
	Book(); // constructor assigning the info to variables

	double getamt(); // return amt
	void setamt(double);

	string gettitle(); // return title
	void settitle(string);

	string getauth(); // return author
	void setauth(string);

	int getyear(); // return year
	void setyear(int);

	// swithc statments incase
	void titlesrch();
	void pricesrch();
	void authrch();
	void yearsrch();

};
Last edited on
You don't call that function with void Book::function();

You created a member function of the book class that can only be called when you create a book object.
1
2
Book bookObject;
bookObject.titlesrch();
Hi, rezy3312
I would have write your code in another style, of which I give you an example below, but it doesn’t change a lot.

The issue, I think, is that you would like your class “Book” to perform operation which is not due to carry out.
Let me explain: “Book“ defines the ‘behaviour’ of a single book, while you are trying to add methods (= member functions) that aim to search amongst a collection of books.
It doesn’t make much sense to ask a single book to search inside a library, does it?

I think it could be simpler to separate the two concepts, book and library.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
#include "Book.h"

class Library {
public:
    Library();
    ~Library();
    void titleSearch(); // search inside vector books
    void priceSearch();
    void authorSeach();
    void yearSearch();

private:
    std::vector<Book> books;
};

Of course those methods are to be expunged by class “Book”.

Or, if you think of “Book” like a simple collection of proprieties, without methods, something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <vector>

struct Book {
   double amt {0.0}; // book amount
   std::string title; // book title
   std::string author; // name of author
   int year {0}; // year of the book
}

class Library {
public:
    Library();
    ~Library();
    void titleSearch(); // search inside vector books
    void priceSearch();
    void authorSeach();
    void yearSearch();

    Book getSingleBook(const std::string& title);

private:
    std::vector<Book> books;
};


Or similar.

What follows is your code rewritten in a different style. It compiles and perform few basic, but, from my point of view, pointless actions.

Book.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef BOOK_H
#define BOOK_H

#include <string>

class Book
{
public:
   Book() = default; // constructor assigning the info to variables

   double getAmt() const; // return amt
   void setAmt(double);

   std::string getTitle() const; // return title
   void setTitle(const std::string &);

   std::string getAuth() const; // return author
   void setAuth(const std::string &);

   int getyear(); // return year
   void setyear(int);

   // swithc statments incase
   void titleSearch();
   void priceSearch();
   void authorSeach();
   void yearSearch();

private:
   double amt {0.0}; // book amount
   std::string title; // book title
   std::string author; // name of author
   int year {0}; // year of the book
};

#endif // BOOK_H 


Book.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include "Book.h"

void Book::setAmt(double am)
{ amt = am; }

double Book::getAmt() const
{ return amt; }


void Book::setTitle(const std::string& titl)
{ title = titl; }

std::string Book::getTitle() const
{ return title; }

void Book::setAuth(const std::string& aut)
{ author = aut; }

std::string Book::getAuth() const
{ return author; }

void Book::titleSearch()
{
   std::cout << "you chosed to search by TITLE" << std::endl;
}

void Book::priceSearch()
{
   std::cout << "you chosed to search by PRICE" << std::endl;
}

void Book::authorSeach()
{
   std::cout << "you chosed to search by AUTHOR" << std::endl;
}

void Book::yearSearch()
{
   std::cout << "you chosed to search by YEAR" << std::endl;
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <limits>
#include "Book.h"

void PrintMenu();
int GetUserInput();

int main()
{

   // usage
   int userChoice = GetUserInput();

   Book singlebook;

   // switch statment for the users input
   switch (userChoice)
   {
      case 1:
         singlebook.titleSearch();
         break;

      case 2:
         singlebook.priceSearch();
         break;

      case 3:
         singlebook.authorSeach();
         break;

      case 4:
         singlebook.yearSearch();
         break;

      case 5:
         std::cout << "\n Thank you " << std::endl;
         break;
   }

   std::cout << "\n\nPress ENTER to continue...";
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   return 0;
}

void PrintMenu()
{
   std::cout << "Welcome to sample library!" << std::endl;
   std::cout << "What would you like to do? \n\n --------" << std::endl;
   std::cout << "enter 1) if you wish to search by book titles"
             << std::endl;
   std::cout << "enter 2) if you wish to search by book price "
                "of the book " << std::endl;
   std::cout << "enter 3) if you wish to search by book author"
             << std::endl;
   std::cout << "enter 4) if you wish to search by book year"
             << std::endl;
   std::cout << "enter 0) if you wish to exit the program" << std::endl;
}

int GetUserInput()
{
   int choice {5};
   while (choice <0 || choice >4 || !std::cin)
   {
      PrintMenu();
      std::cin >> choice;
      if (!std::cin)
      {
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
                         '\n');
         std::cin.clear();
      }
   }

   return choice;
}

Topic archived. No new replies allowed.