Structs, Arrays, and Functions. Oh my!

closed account (2hTDizwU)
I'm completely stuck on this assignment. I first wrote everything in int main() without any issue. It all worked lovely! Unfortunately our instructor wants it split up into multiple functions (less than 35 lines per function). I've split it up as you can see below but unfortunately my knowledge (and google hasn't been much help) of functions and passing/referencing through them is not that high. My program doesn't work at all now. All the 'Books' give errors so i'm not sure if I'm passing the struct or array improperly. Please help!

The original txt file reads like:
<number of books>
<title>
<author>
<price>
<title>
<author>
<price>


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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void setStruct() {
	
	struct bookTable {
		string title;
		string author;
		double price;
	};
}

void setArray(int &arraySize, struct bookTable *Book[]) {
	
	ifstream infile;
	int bookCounter = 0;

	infile.open("books2.txt");

	if (!infile) {
		cout << "Unable to open Books.txt" << endl;
	}

	infile >> arraySize;
	infile.ignore(100, '\n');

	bookTable *Book = new bookTable[arraySize];

	infile.close();
}

void readFile(struct bookTable *Book[]) {
	
	ifstream infile;
	int bookCounter = 0;

	infile.open("books2.txt");

	for (int i = 0; getline(infile, Book[i].title); i++) {

		getline(infile, Book[i].author, '\n');
		infile >> Book[i].price;
		infile.ignore(100, '\n');

		bookCounter++;
	}

	infile.close();
}

void displayMenu(struct bookTable *Book[]) {
	int menuChoice = 0, bookCounter = 0;
	string findTitle;

	do { cout << "\n===== Bookstore App =====" << endl; 
	cout << "1. Print Books" << endl; 
	cout << "2. Change Price" << endl;
	cout << "3. Quit" << endl; 
	cout << "\nEnter Choice: ";
		cin >> menuChoice;
		if (menuChoice == 1) {
			for (int i = 0; i < bookCounter; i++) {
				cout << "===== BOOK =====" << endl;
				cout << "Title: " << Book[i].title << endl;
				cout << "Author: " << Book[i].author << endl;
				cout << "Price: " << fixed << setprecision(2) << Book[i].price << endl; } }
		else if (menuChoice == 2) { cin.ignore(100, '\n');
			cout << "What is the title of the book? ";
			getline(cin, findTitle, '\n');
			for (int i = 0; i < bookCounter; i++) {
				if (findTitle == Book[i].title) {
					cout << "Enter New Price: " << endl;
					cin >> Book[i].price;
				}
				else if (findTitle != Book[i].title) {
					cout << "Unable to find Book" << endl;
				}}}

		else if (menuChoice < 1 || menuChoice > 3) {

			cout << "Invalid Entry. Please enter 1, 2, or 3 from the options menu." << endl;
		}	} while (menuChoice != 3);
}

void writeFile(int arraySize, struct bookTable *Book[]) {

	ofstream outfile;
	int bookCounter = 0;

	outfile.open("sale2.txt");

	outfile << arraySize << endl;

	for (int i = 0; i < bookCounter; i++) {

		outfile << Book[i].title << endl;
		outfile << Book[i].author << endl;
		outfile << fixed << setprecision(2) << Book[i].price << endl;
	}

	outfile.close();

	delete[] Book;

}

int main() {

	setStruct();
	setArray();
	readFile();
	displayMenu();
	writeFile();

	cout << "\nSale2.txt has been created." << endl;

	return 0;
}
Whats this function doing.
 
void setStruct() // I see no function definition for this so I can only assume the following: 


If your intent to create a struct using this function, no need.
http://www.cplusplus.com/doc/tutorial/structures/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Note Struct is a reserved word used for defining a new data_type such as "bookTable"
//So where you have tried to pass the struct into a function you used the wrong 
//syntax. The data_type of the array your trying to pass is actually (just) bookTable but 
//notice that to prevent you from not being able to reuse this user defined data_type in the future.
//I made a variable of bookType, This variable can use all the same data in the original struct without 
//changing the value of the original data structures.
//which I did below

struct bookTable  
{
		string title;
		string author;
		double price;
	};

You create a variable that represents this struct, which will basically a copy of the contents of struct into a new variable name of that struct.
 
bookTable booktable2;

Or you could have simply used put the variable bookType2 after the original struct. This syntax is better described at the above link.

Now all of the same contents inside the original struct will be found in this new struct variable "bookType2"

Also struct is not the data type that you pass struct is used to define the new user defined data type "bookType". So when you call the function
void setArray(int &arraySize, struct bookTable *Book[])

you don't need the keyword struct because the data type you created is "bookTable" ... Hope that make since.. If you don't understand my explanation refer to the link. If you understand my way of explaining you should still read the link's material because Im sure Im leaving some stuff out.

Another thing, unless the book[] array is meant to be a char array to represent the string you'll need to add an array in your struct.

Last I have not look at every piece of your code so there may be more errors than I'm pointing out but this is just at first glance. So do some reading on structs and maybe on functions; both of which should help. The reason I mention the functions is because I see you calling functions in main but you have no parameters, yet, in your function definitions you have parameters. http://www.cplusplus.com/doc/tutorial/functions/

Work through those problems and if it still won't run; come back and let me know where you stand from there. I'll do what I can to help you. Remember comments and error messages always help, especially if they provide a number line .

//The following is more of a personal preference and no requirement
A personal opinion, (note) it wouldn't affect your code any but it would look cleaner if you were writing your function definitions on the bottom (under main) and calling the function prototypes right before main. Just remember if you were to move your function definitions to the bottom you can't call them in the main function without having there prototype before main. The struct on the other hand can stay where it is.
Last edited on
Topic archived. No new replies allowed.