a lil' problem here...hihi

i am having a problem with struct here...mainly at the selectionSort method..just below the main method...HELP ME PLEASE!!


#include <iostream>
#include <iomanip>
using namespace std;

const int maxname = 20;
//maximum number of struct data element
const int numrecs = 5;

//declare struct
struct PayRecord
{
long id;
char name[maxname];//maximum number of name character = 20
float rate;

};

//method prototype
void selectionSort(PayRecord employee, int numrecs2);

int main()
{
int i;

//initialize PayRecord array struct with data
PayRecord employee[numrecs] =
{
{1234, "Adam Ali", 1000},
{5321, "Yen Yen", 5000},
{3232, "John Spy", 200},
{1089, "Calie Yoeh", 333},
{1114, "Ali Baba", 1221}

};

//blank line
cout<<endl;


cout<<setiosflags(ios::left);

for(i = 0; i < numrecs ; i++)
cout<<setw(20)<<employee[i].id//display struct data id
<<setw(15)<<employee[i].id//display struct data id
<<setw(6)<<employee[i].rate//display struct data rate
<<endl;

cout<<endl;



//invoking method
selectionSort(employee[numrecs],numrecs);

return 0;
}

//selection sort method
void selectionSort(PayRecord employee[numrecs], int numrecs2)
{

int index;
//PayRecord temp;
int iteration;
float smallest;

//iterate the index after compare
for(index = 0; index < numrecs2; index++)
{


smallest = employee[index].rate;

//iterate each element for compare
for(iteration = index; iteration < numrecs2; iteration++)
{

//compare for the smallest among the available element[iteration]
if(smallest > employee[iteration].rate)
{

smallest = employee[iteration].rate;
}

}

//after comparison
int temp = employee[index];
employee[index] = employee[smallest];
employee[smallest] = temp;

}

}

here is the error from the compiler

--------------------Configuration: Soalan3 - Win32 Debug--------------------
Compiling...
Soalan3.cpp
f:\c++lab2\soalan3.cpp(89) : error C2440: 'initializing' : cannot convert from 'struct PayRecord' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
f:\c++lab2\soalan3.cpp(90) : error C2108: subscript is not of integral type
f:\c++lab2\soalan3.cpp(90) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct PayRecord []' (or there is no acceptable conversion)
f:\c++lab2\soalan3.cpp(91) : error C2108: subscript is not of integral type
f:\c++lab2\soalan3.cpp(91) : error C2440: '=' : cannot convert from 'int' to 'struct PayRecord []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Soalan3.obj - 5 error(s), 0 warning(s)
Last edited on
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
#include <iostream>
#include <iomanip>
using namespace std;

const int maxname = 20;
//maximum number of struct data element
const int numrecs = 5;

//declare struct
struct PayRecord
{
long id;
char name[maxname];//maximum number of name character = 20
float rate;

};

//method prototype
void selectionSort(PayRecord employee, int numrecs2);

int main()
{
int i;

//initialize PayRecord array struct with data
PayRecord employee[numrecs] =
{
{1234, "Adam Ali", 1000},
{5321, "Yen Yen", 5000},
{3232, "John Spy", 200},
{1089, "Calie Yoeh", 333},
{1114, "Ali Baba", 1221}

};

//blank line
cout<<endl;


cout<<setiosflags(ios::left);

for(i = 0; i < numrecs ; i++)
cout<<setw(20)<<employee[i].id//display struct data id
<<setw(15)<<employee[i].id//display struct data id
<<setw(6)<<employee[i].rate//display struct data rate
<<endl;

cout<<endl;



//invoking method
selectionSort(employee[numrecs],numrecs);

return 0;
}

//selection sort method
void selectionSort(PayRecord employee[numrecs], int numrecs2)
{

int index;
//PayRecord temp;
int iteration;
float smallest;

//iterate the index after compare
for(index = 0; index < numrecs2; index++)
{


smallest = employee[index].rate;

//iterate each element for compare
for(iteration = index; iteration < numrecs2; iteration++)
{

//compare for the smallest among the available element[iteration]
if(smallest > employee[iteration].rate)
{

smallest = employee[iteration].rate;
}

}

//after comparison
int temp = employee[index];
employee[index] = employee[smallest];
employee[smallest] = temp;

}

}
line 88. the variable type is int. you are trying to store an instance of "Struct Payrecord" as an int. temp should be declared as type struct Payrecord.
Last edited on
thank you....!i have try your recommendation...the error does reduce but still leaving a problem

--------------------Configuration: Soalan3 - Win32 Debug--------------------
Compiling...
hehe.cpp
F:\c++lab2\hehe.cpp(89) : error C2108: subscript is not of integral type
F:\c++lab2\hehe.cpp(89) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct PayRecord []' (or there is no acceptable conversion)
F:\c++lab2\hehe.cpp(90) : error C2108: subscript is not of integral type
F:\c++lab2\hehe.cpp(90) : error C2440: '=' : cannot convert from 'struct PayRecord' to 'struct PayRecord []'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

hehe.obj - 4 error(s), 0 warning(s)
-------------------------------------------------
especially at the line 90...we don't need to declare the same variable twice right..so i'm leaving the temp as it was...( i have try to declare the temp and it doesn't work...)

and yup...the above line keep confusing me too....@o@
The prototype on line 19 and the implementation on line 59 don't match. If you want an array of an unkonw size you should write it like so: void selectionSort(PayRecord employee[], int numrecs2); // Note the []

for swapping on line 88 you can use std::swap():

1
2
3
4
5
#include <algorithm>

...

std::swap(employee[index], employee[smallest]);


EDIT: If you want to write it yourself:
1
2
3
PayRecord temp = employee[index];
employee[index] = employee[smallest];
employee[smallest] = temp;
Last edited on
Topic archived. No new replies allowed.