Need help input is wrong

I am writing a program where a user enters from a menu and picks how they want to search from the file that is read. I know I made a few mistakes already but can someone point out and help me from them I will really appreciate it. This is what I have so far and completely lost with the user input I am getting a few errors.


Last edited on
Header

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
    
#pragma once

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iomanip>
  
 
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

	string gettitle(); // return title

	string getauth(); // return author

	int getyear(); // return year
};



books.txt

Author Title Year Price
-------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10

Green   Hunter       Return of the light		 2014        $19.50
Tom     Freeman      Hurting Beth                2012        $12.79
Billy   Noris        How It Hurts                2012        $12.79
Justin  Jefferson    Chandler Beths Story        2012        $12.79
Mitch   Freeman      Shooting for the Stars      2012        $12.79
Bob     Freeman      H.Q Secrets                 2012        $12.79
Billy   Noris        Fun In The Middle           2012        $12.79



library.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
#include "library.h"


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

double getamt(int amt)
{
	return amt;
}

string gettitle(string title)
{
	return title;
}

string getauth(string author)
{
	return author;
}


main

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
88
89
90
#include "library.h"

using namespace std;

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


	string line; // to read the files
	int menu; // user choice to make when search for book

	Book Info; // class object


				
	ifstream infile;  // opening the file

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

	if (!infile.is_open()) // if file not read
	{
		cout << "an error occured please try again later" << endl;
	}
	else
	{
		cout << "here is the libary you wish to buy" << endl;

		while (getline(infile, line))
		{
			cout << line << endl; // prints the text file to screen
		}
	}




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

		cin >> menu;

	} while (menu != 0);

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

	case 1:
		cout << "\n please enter the title of the book" << endl;
		cin >> Info.gettitle;
		void titlesrch( /* send input to search book title*/);
		break;

	case 2:
		cout << "\n please enter price of the book" << endl;
		cin >> Info.getamt;
		void pricesrch( /* send input to search book price*/);
		break;

	case 3:
		cout << "\n please enter the author of the book" << endl;
		cin >> Info.getauth;
		void authrch( /* send input to search book author*/);
		break;

	case 4:
		cout << "\n please enter year of the book" << endl;
		cin >> Info.getyear;
		void yearsrch( /* send input to search book year*/);
		break;

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



	system("pause");
	return 0;
}
Last edited on
closed account (SECMoG1T)
Hello, i would like to offer some advice.

1. your class "Book" have no way of setting it's member values, maybe you should provide a constructor that takes arguments or some mutator functions e.g

1
2
3
4
5
6
7
Book(double,std::string,std::string,int)///in class declaration

Book::Book(double amount, std::string Title, std::string Author, int Yr)
:amt(amount), title(Title),author(Author),year(Yr)
{
	
}


2. you need a way to store your data once read from the txt, you could create a container of books and then you can access your items from there.


 
 std::vector<Book> library_books;// populate your books here, you may need this for searching books by details rather than opening the file everytime. 


3. you need a way of exiting your loop once you acquire user input.
consider this two function.

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
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)
        {
             if(!std::cin)
               {
                   std::cin.clear();  std::cin.ignore(1000,'\n');
                }  

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

     return choice; 
}



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


4. you seem to be declaring functions within your switch , you should instead define those functions elsewhere and the call them in your cases.

1
2
3
4
5
case 1:
		cout << "\n please enter the title of the book" << endl;
		cin >> Info.gettitle;
		void titlesrch( /* send input to search book title*/);/// declare/define this function elsewhere
		break;
Last edited on
Thank you andy1992 for the advise, I made a few changes from the program but i have a few questions
for the construtor on part one. When i made the changes i got an error saying no appropriate "default constructor available"
wasn't the constructor used to assign the private variables that way i can access them.

part 4, I don't think i got the input right for

 
cin >> Info.gettitle;


or would i have to use getline to get the whitespace?
sorry i am really new to this im a beginner.

Last edited on
Topic archived. No new replies allowed.