using classes

Need help please. I'm new to classes and I have a program due. I'm reading from a file and using the following functions:
readdata() Two parameters, the array of objects and n, a reference parameter.
printall() Two parameters, the array and n, number of filled positions. It will print info for each item in my database on a new line
findnumpetals() will read in a value and find the flower with the number of petals I select. thenit will call printone() to print all of the info for the object searhed for if it is found.
sort()one parameter an element of the array of objects. it will sort the array of objects in ascending order by the last name. I'm lost with this. Here is my code so far
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
const int NUMFLOWERS=15;
class flower_name{
public:
string last;
string first;
string temp;
};
class flower_info{
public:
string rating;
string flowercolor;
int numberofpetals;
};
class fav{
public:
flower_name name;
flower_info data;
};
ifstream infile;
ofstream outfile;
void readdata(fav[], int &);
void printall(fav[], int);
void sort(fav[], int);
void findnumpetals(fav[], int, int);
void printone(fav[]);
int main()
{
fav favflowers[NUMFLOWERS];
int n;
int numpetals=12;

infile.open("program8.txt");
outfile.open("program8.out");
n=0;
do{
for(int i=0;i<n;i++)
readdata(favflowers, n);
printall(favflowers, n);
sort(favflowers, n);
printall(favflowers,n);
findnumpetals(favflowers,n,numpetals);
}while(infile>>n);

infile.close();
outfile.close();
system("pause");
return 0;
}
//function to readdata of my favorite celebrity class from a file
void readdata(fav favflowers[], int &n)
{
n=0;
infile>>n;
while(infile){
n++;
infile>>favflowers[n].name.last;
infile>>favflowers[n].name.first;
infile>>favflowers[n].data.rating;
infile>>favflowers[n].data.flowercolor;
infile>>favflowers[n].data.numberofpetals;
}
return;
}
//function to print all the data. It will print info for each item in my list on
//a new line.
void printall(fav favflowers[], int n)
{
outfile<<"\t\tMy favorite flowers in my Database"<<endl<<endl;
outfile<<"Name\t\t\tRating\t\tFlower Color\t\tNumber of petals"<<endl<<endl;
for(int i=0; i<n; i++){
outfile<<favflowers[i].name.first;
outfile<<'\t'<<favflowers[i].name.last;
outfile<<'\t'<<favflowers[i].data.rating;
outfile<<'\t'<<favflowers[i].data.flowercolor;
outfile<<'\t'<<favflowers[i].data.numberofpetals<<endl;
}
return;
}

//function to sort my database of favorite celebs by the last name in
//ascending order
void sort(fav favflowers[], int n)
{

for(int i=0; i<n-1; i++)
for(int cand=i+1;cand<n; cand++)
if(favflowers[n].name.last<favflowers[cand].name.last){
favflowers[n].name.temp=favflowers[n].name.last;
favflowers[n].name.last=favflowers[cand].name.last;
favflowers[cand].name.last=favflowers[n].name.temp;
}
return;
}
//function to search for a celebrity using an age
void findnumpetals(fav favflowers[], int n, int numpetals)
{
numpetals=12;
bool found = false;

outfile<<"The number of petals to search for is "<<numpetals<<endl;

for(int i=0; i<n; i++)
if(favflowers[i].data.numberofpetals==numpetals){
outfile<<favflowers[i].name.first<<" ";
outfile<<favflowers[i].name.last<<endl;
found=true;
printone(favflowers);
}
if(!found)
outfile<<"No flower found with that number of petals"<<endl;
return;
}
//Function to print all of the objects in the array that matched the value searched for
void printone(fav favflowers[ ])
{
int n;

for(int i=0; i<n; i++){
outfile<<'\t'<<favflowers[i].name.last;
outfile<<'\t'<<favflowers[i].name.first;
outfile<<'\t'<<favflowers[i].data.rating;
outfile<<'\t'<<favflowers[i].data.flowercolor;
outfile<<'\t'<<favflowers[i].data.numberofpetals<<endl;
}
return;
}


this is my database file inforamtion:
Bermuda Buttercup one yellow 5
Common Mallow five lavender 5
Scarlet Pimpernikel two purple 5
Lantana Camara three yellow 12
Calla Lily one white 1
Please encase your code in [code] tags, it makes it much easier to read.

What exactly is the problem?
Are you getting compiler errors or is it just the sort() function your having trouble with or what?
Last edited on
It's not compiling
Are you getting compiler errors or is it just the sort() function your having trouble with or what?
The program isn't compiling. I'm not sure what i'm doing wrong. it's not reading from the file at all
There are few faster ways to being ignored than saying "this doesn't compile" and dumping an assload of code.
How doesn't it compile? What errors are you getting? Are you by any chance using a Lisp compiler? Please be more descriptive.
Last edited on
No need to be rude helios. I'm new to classes and simply looking for help. Any help that is givien is appreciated. I'm answering the best way I can. When I run the program nothing happens. It doesn't give any errors. I get a blank screen.
I wasn't being rude, just stating facts.

That's quite different from "not compiling".

The oldest trick in the book to know at which point a program stops functioning is by printing messages.
Last edited on
Okay, here is my code again in
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 tags. Hope I did it right.  Also I just ran it again and this is my output:

		My favorite flowers 

Name	Rating from 1 to 5	Flower Color	Number of petals

	2030043257
	131400
	131400
	134963
	-1258290764
	8
	131400
	0
	2004650994
	134952
	131072
	0
	-1945374993
	2293724
	134952



[code]#include <iostream>
#include<fstream>
#include<string>
using namespace std;
const int NUMFLOWERS=15;
class flower_name{
public:
string last;
string first;
string temp;
};
class flower_info{
public:
string rating;
string flowercolor;
int numberofpetals;
};
class fav{
public:
flower_name name;
flower_info data;
};
ifstream infile;
ofstream outfile;
void readdata(fav[], int &);
void printall(fav[], int);
void sort(fav[], int);
void findnumpetals(fav[], int, int);
void printone(fav[]);
int main()
{
fav favflowers[NUMFLOWERS];
int n;
int numpetals=12;

infile.open("program8.txt");
outfile.open("program8.out");
n=0;
do{
for(int i=0;i<n;i++)
readdata(favflowers, n);
printall(favflowers, n);
sort(favflowers, n);
printall(favflowers,n);
findnumpetals(favflowers,n,numpetals);
}while(infile>>n);

infile.close();
outfile.close();
system("pause");
return 0;
}
//function to readdata of my favorite celebrity class from a file
void readdata(fav favflowers[], int &n)
{
n=0;
infile>>n;
while(infile){
n++;
infile>>favflowers[n].name.last;
infile>>favflowers[n].name.first;
infile>>favflowers[n].data.rating;
infile>>favflowers[n].data.flowercolor;
infile>>favflowers[n].data.numberofpetals;
}
return;
}
//function to print all the data. It will print info for each item in my list on
//a new line.
void printall(fav favflowers[], int n)
{
outfile<<"\t\tMy favorite flowers in my Database"<<endl<<endl;
outfile<<"Name\t\t\tRating\t\tFlower Color\t\tNumber of petals"<<endl<<endl;
for(int i=0; i<n; i++){
outfile<<favflowers[i].name.first;
outfile<<'\t'<<favflowers[i].name.last;
outfile<<'\t'<<favflowers[i].data.rating;
outfile<<'\t'<<favflowers[i].data.flowercolor;
outfile<<'\t'<<favflowers[i].data.numberofpetals<<endl;
}
return;
}

//function to sort my database of favorite celebs by the last name in 
//ascending order
void sort(fav favflowers[], int n)
{

for(int i=0; i<n-1; i++)
for(int cand=i+1;cand<n; cand++)
if(favflowers[n].name.last<favflowers[cand].name.last){
favflowers[n].name.temp=favflowers[n].name.last;
favflowers[n].name.last=favflowers[cand].name.last;
favflowers[cand].name.last=favflowers[n].name.temp;
}
return;
}
//function to search for a celebrity using an age
void findnumpetals(fav favflowers[], int n, int numpetals)
{
numpetals=12;
bool found = false;

outfile<<"The number of petals to search for is "<<numpetals<<endl;

for(int i=0; i<n; i++)
if(favflowers[i].data.numberofpetals==numpetals){
outfile<<favflowers[i].name.first<<" ";
outfile<<favflowers[i].name.last<<endl;
found=true;
printone(favflowers);
}
if(!found)
outfile<<"No flower found with that number of petals"<<endl;
return;
}
//Function to print all of the objects in the array that matched the value searched for
void printone(fav favflowers[ ])
{
int n;

for(int i=0; i<n; i++){
outfile<<'\t'<<favflowers[i].name.last;
outfile<<'\t'<<favflowers[i].name.first;
outfile<<'\t'<<favflowers[i].data.rating;
outfile<<'\t'<<favflowers[i].data.flowercolor;
outfile<<'\t'<<favflowers[i].data.numberofpetals<<endl;
}
return;
} 
Last edited on
hey tyky,

Keep in mind the folks on this forum aren't being paid to help you. We're doing it because we enjoy programming. You may get lucky, but odds are that few people are going to have fun reading through a bunch of improperly formatted code to find an unknown error for you. It's like if someone were to say "My computer's not working, fix it for me."

Especially if you're on a time crunch, take some time to write up an explanation of what exactly is the problem. Which part are you stuck on. Pretend you have to ask just one or two questions and then you're done.

I haven't read your code in detail, but in general you will want to break it up and test each different part individually to see which part is broken. If you want to fix a car, you try your best to figure out which part is broken and test each component individually. Similar idea here. As helios mentioned, print out some statements to make sure the variables you're have are holding the variables you expect at the times you expect them. So something like:

 
cout << "my_variable: " << my_variable << endl;


Do this as a sanity check to ensure things are working as they should be at separate places in your code. Odds are somewhere along the way you'll find that the variables are being modified when you didn't intend.

Also, if you're being graded on your use of classes, my guess is you will obtain a low score, even if your program works. You are essentially using classes as a struct. The basic idea of a class is to group together variables and methods that belong together. There are no functions in any of the classes you defined in your code. See this site's tutorial on classes for more info (http://www.cplusplus.com/doc/tutorial/classes.html). Ultimately you will probably want to stick those functions inside the class.

Finally, once you've done that, modify your program to include the proper use of classes and try to get it to work again. If it still fails, try asking for help again, but be specific in your questions. If you're confused about how classes work, say so, and ask a question about what particular part of the concept you're confused about.

I guess another option is to follow the link to the "College Homework Help Now" banner/link I saw advertised at the top of this page. Might as well support the site.
Hi jeffbmartinez

I know you guys are not getting paid for this. As I said before I do appreciate any help I get. I'd rather do this on my own with some advise than going on a site for someone to do it for me. Then i'm really not learning anything. I will take your advise and break it up.

Thanks for your help.
Topic archived. No new replies allowed.