I know I made two other topics about this already, instead I figured it'd be better if I had it all at once in this topic. So, what we have here is a program that uses templates.
What it's supposed to do:
It is supposed to list the numbers values.
It is also supposed to list the struct values.
Problems:
- When I only want to add 9 numbers in a list that has a max of 50 spots, it lists the 9 numbers, but then displays "-858993460" for another 41 times (I guess).
- I have a function that removes the last value of an array, this function removes the "-858993460" value instead of 9. How do I make it remove a real value like 9?
- Also, it's not displaying the struct values either. How do I do that?
Program:
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
|
#include <iostream>
using namespace std;
struct houses
{
int addressNumber;
int price;
string state;
};
template <class T>
class list
{
private:
#define MAXSIZE 50
T ar[MAXSIZE];
int number;
public:
list();
bool isFull();
bool isEmpty();
void add(T x);
void display();
T remove();
};
template <class T>
list<T>::list()
{
number = 0;
}
template <class T>
bool list<T>::isEmpty()
{
return (number == 0);
}
template <class T>
bool list<T>::isFull()
{
return (number == MAXSIZE);
}
template <class T>
void list<T>::add(T x)
{
ar[number] = x;
number++;
}
template <class T>
T list<T>::remove()
{
if (isEmpty() != true)
{
number--;
return ar[number];
}
}
template <class T>
void list<T>::display()
{
for (int i = 0; i < MAXSIZE; i++)
{
cout << ar[i] << endl;
}
}
int main()
{
list <int> numList;
list <houses> houseList;
houses a, b, c;
a.addressNumber = 400;
a.price = 50000;
a.state = "Michigan";
b.addressNumber = 248;
b.price = 35000;
b.state = "Wyoming";
c.addressNumber = 4573;
c.price = 172352;
c.state = "Soviet Union";
numList.add(1);
numList.add(2);
numList.add(3);
numList.add(4);
numList.add(5);
numList.add(6);
numList.add(7);
numList.add(8);
numList.add(9);
houseList.add(a);
houseList.add(b);
houseList.add(c);
numList.remove();
houseList.remove();
numList.display();
system("pause");
return 0;
}
|
This is what it says in the outcome EXE file:
1
2
3
4
5
6
7
8
9
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
Press any key to continue . . . |
I'm hoping someone can really help me with this.