classes

For my task i am suppose to ask the user and edit and change the .txt file of the ID, title, author, category and number of copies according to the user. I am also to add a book to the collection if the book does not exist in the text file, and if the book exist, i only update the copy. I'm not good with classes and constructors, so I only have the basics and I need help on calling it into 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
#ifndef BOOKS_H
#define BOOKS_H
#include <string>
#include <iostream>
using namespace std;

class books
{
	private:
		int id, copies;
		string title, author, category;
	
	public:
		void setID(int userID);
		void setTitle(string userTitle);
		void setAuthor(string userAuthor);
		void setCategory(string userCategory);
		void setCopies(int userCopies);
		int getID();
		string getTitle();
		string getAuthor();
		string getCategory();
		int getCopies();	
};

#endif 


with the class.cpp file
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
#include <string>
#include "books.h"
#include <iostream>
using namespace std;

void books::setID(int userID)
{
	id = userID;
}
void books::setAuthor(string userAuthor)
{
	author = userAuthor;
}
void books::setCategory(string userCategory)
{
	category = userCategory;
}
void books::setTitle(string userTitle)
{
	title = userTitle;
}
void books::setCopies(int userCopies)
{
	copies = userCopies;
}
int books::getID()
{
	return id;
}
string books::getTitle()
{
	return title;
}
string books::getAuthor()
{
	return author;
}
string books::getCategory()
{
	return category;
}
int books::getCopies()
{
	return copies;
}


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
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <fstream>
#include <sstream>
#include "books.h"
using namespace std;

int main()
{

	
	string line;
	ifstream input("books.txt");
	
	while(getline(input, line))
	{
		stringstream strStream(line);
		string id, title, author, category, copies;

		// Read tokens in this line one by one
		getline(strStream, id, '|');
		getline(strStream, title, '|');
		getline(strStream, author, '|');
		getline(strStream, category, '|');
		getline(strStream, copies, '|');
		

		// We need to convert the number of copies, and may be ID, to integers
		int idNum, copiesNum;
		stringstream idConverter(id);
		idConverter >> idNum;
		
		stringstream copiesConverter(copies);
		copiesConverter >> copiesNum;
		
		cout << idNum << ", " << title << ", " << author << ", " << category << ", " << copiesNum << endl;
		
	}
	
	
	
	system("pause");
	return 0;
}
Last edited on
Hi,

declare the object books and then use it with your functions in main.cpp
Topic archived. No new replies allowed.