pointers help...

Hey guy's Little confused here on pointers. I am trying to finish this assignment here and I can't remember what I have to do now. I thought I had it all figured out, but I don't any help would be nice thank you.

error list will be in second post. over the character limit.



Lab4Header.h
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
#include <iostream>
#ifndef LAB4CLASS_H
#define LAB4CLASS_H

const int S_SIZE = 256;
const int FLEET_SIZE = 30;
using namespace std;

struct shipData
{
	char* name;
	int regNum;
	char* type;
	char* position;
	char* condition;
	char* captain;
};

class starFleet
{
public:
	void newShip();
	void addstarships();
	void displayFleet();
	char* getName(int index);
	char* getType(int index);
	char* getPosition(int index);
	char* getCaptain(int index);
	void setName(char* aname, int index);
	void seType(char* atype, int index);
	void setPosition(char* aposition, int index);
	void setCaptain(char* acaptain, int index);
	int loadData();
	bool searchFleet();
	int getCount();
	char* aname;
	char* atype;
	char* aposition;
	char* acaptian;
	starFleet(); // Constructor
	// Other methods go here.

private:
	shipData Ships[FLEET_SIZE];
	int index;
	int shipCount;
};
#endif 


Lab4class.cpp
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
155
156
#include "lab4Header.h" 
#include <iostream>
#include <fstream>
#include <iomanip>
//initialize pointer
starFleet::starFleet()
{
	for (int i = 0; i < FLEET_SIZE; i++)
	{
		Ships[i] = NULL;
	}
	shipCount = 0;
}

int starFleet::loadData()
{
	shipCount = 0; //set the count = 0
	char tempName;
	ifstream inputFile;
	cout << "Loading Data file......." << endl;
	inputFile.open("starships.txt");

	while (!inputFile.eof())
	{							 
		inputFile.get(Ships[tempName]->name, S_SIZE, ';');
		inputFile >> Ships[shipCount]->regNum;
		inputFile.ignore(100, ';');
		inputFile.get(Ships[shipCount]->type, S_SIZE, ';');
		inputFile.get(Ships[shipCount]->position, S_SIZE, ';');
		inputFile.get(Ships[shipCount]->condition, S_SIZE, ';');
		inputFile.get(Ships[shipCount]->captain, S_SIZE);
		shipCount++;
	}
	if (shipCount > 0)
		shipCount--;
	inputFile.close();
	std::cout << endl << "Successfully loaded " << shipCount << " ships." << endl << endl; // return the total count 
	return;
}

char* starFleet::getName(int index)
{
	return Ships[index]->name;
}
char* starFleet::getType(int index)
{
	return Ships[index]->type;
}
char* starFleet::getPosition(int index)
{
	return Ships[index]->position;
}
char* starFleet::getCaptain(int index)
{
	return Ships[index]->captain;
}
//set 
void starFleet::setName(char* aname, int index)
{
	Ships[index]->name = aname;
}
void starFleet::seType(char* atype, int index)
{
	Ships[index]->type = atype;
}
void starFleet::setPosition(char* aposition, int index)
{
	Ships[index]->position = aposition;
}
void starFleet::setCaptain(char* acaptain, int index)
{
	Ships[index]->captain = acaptain;
}

void starFleet::addstarships()
{
	Ships[shipCount] = new Ship();

	char tempName[S_SIZE];
	cout << " What is the name of the ship ? : ";
	cin.get(tempName, S_SIZE);

	Ships[shipCount]->name = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(Ships[shipCount]->name, tempName);

	cout << " What is the registry number of the ship ? : ";
	cin >> Ships[shipCount]->regNum;

	cout << " What is the type of the ship ? : ";
	cin.get(tempName, S_SIZE);

	Ships[shipCount]->type = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(Ships[shipCount]->type, tempName);


	cout << " What is the position of the ship ? : ";
	cin.get(tempName, S_SIZE);

	Ships[shipCount]->position = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(Ships[shipCount]->position, tempName);

	cout << " What is the condition of the ship ? : ";
	cin.get(tempName, S_SIZE);

	Ships[shipCount]->condition = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(Ships[shipCount]->condition, tempName);

	cout << " What is the captain of the ship ? : ";
	cin.get(tempName, S_SIZE);

	Ships[shipCount]->captain = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(Ships[shipCount]->captain, tempName);

	shipCount++;
	return;


}
void starFleet::displayFleet()
{
	int count = 0;
	if (shipCount > 0)
	{
		cout << "List all of the fleet!" << endl;
		for (int i = NULL; i < shipCount; i++)
		{
			cout << "Name of the ship: " << Ships[i].name << endl;
			cout << "Registry number: " << Ships[i].regNum << endl;
			cout << "Type of ship" << Ships[i].type << endl;
			cout << "Position of ship: " << Ships[i].position << endl;
			cout << "Condition of the ship: " << Ships[i].condition << endl;
			cout << "Captain of the ship: " << Ships[i].captain << endl;
		}
	}
}
bool starFleet::searchFleet()
{
	int regNum1;
	cout << "Please enter the name of the fleet you would like to search for: ";
	cin >> regNum1;

	for (int d = NULL; d < shipCount; d++)
	{
		if (Ships[d].regNum == regNum1){
			cout << setprecision(2) << setw(5) << left << "Name: " << Ships[d].name << endl;
			cout << setprecision(2) << setw(5) << left << "registry number: " << Ships[d].regNum << endl;
			cout << setprecision(2) << setw(5) << left << "type: " << Ships[d].type << endl;
			cout << setprecision(2) << setw(5) << left << "Position: " << Ships[d].position << endl;
			cout << setprecision(2) << setw(5) << left << "Condition of ship: " << Ships[d].condition << endl;
			cout << setprecision(2) << setw(5) << left << "Captain of ship: " << Ships[d].captain << endl;
			return true;
		}
	}
	cout << "That fleet does not exist" << endl;
	return false;
}


Main.cpp
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include "lab4Header.h" 

using namespace std;
int main()
{
	starFleet myFleet;
	ofstream outputFile;
	ifstream inputFile;
	int count = 0;
	char choice;
	myFleet.loadData();
	inputFile.close();

	fstream file("starships.txt", fstream::app);
	do {
		cout << "\n**********************************" << endl;
		cout << "Welcome to ****'s star fleet manager" << endl;
		cout << "Here are your following options." << endl;
		cout << "a. Add a fleet" << endl;
		cout << "s. search for a fleet" << endl;				 /*Main Menu*/
		cout << "l. list all fleets" << endl;
		cout << "q. To quit the fleet manager." << endl;
		cout << "Please use all lower case during the program" << endl;
		cout << "**********************************" << endl;
		cout << "please enter your choice: ";
		cin >> choice;
		cin.ignore(); // Added ignore, to get rid of '\n' stuck in the buffer.

		switch (choice) {
		case 'a':
		case 'A':
			if (myFleet.getCount() < FLEET_SIZE)
			{

				myFleet.addstarships();
				break;
			}
			else
				cout << "Sorry, no more room left in the system.";
			break;
		case 's':
		case 'S':
			if (myFleet.getCount() > 0)
			{
				myFleet.searchFleet();
				//class call bool search fleet
				break;
			}
			else
				cout << "sorry no Systems have been added in the database yet" << endl;
			break;
		case 'l':
		case 'L':
			myFleet.displayFleet();
			break;
		case 'q':
		case 'Q':
			cout << "Thank you for using ****'s Star Fleet manager." << endl;
			cout << "Endoy the rest of your day." << endl;
			file.close();
			system("pause");
			return 0;
			break;
		}
	} while (choice != 'q');

	return 0;
}



error list:
Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) 10 1 LAB4TEST
Error 2 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 25 1 LAB4TEST
Error 3 error C2232: '->shipData::name' : left operand has 'struct' type, use '.' 25 1 LAB4TEST
Error 4 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 26 1 LAB4TEST
Error 5 error C2232: '->shipData::regNum' : left operand has 'struct' type, use '.' 26 1 LAB4TEST
Error 6 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 28 1 LAB4TEST
Error 7 error C2232: '->shipData::type' : left operand has 'struct' type, use '.' 28 1 LAB4TEST
Error 8 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 29 1 LAB4TEST
Error 9 error C2232: '->shipData::position' : left operand has 'struct' type, use '.' 29 1 LAB4TEST
Error 10 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 30 1 LAB4TEST
Error 11 error C2232: '->shipData::condition' : left operand has 'struct' type, use '.' 30 1 LAB4TEST
Error 12 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 31 1 LAB4TEST
Error 13 error C2232: '->shipData::captain' : left operand has 'struct' type, use '.' 31 1 LAB4TEST
Error 14 error C2561: 'starFleet::loadData' : function must return a value 38 1 LAB4TEST
Error 15 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 43 1 LAB4TEST
Error 16 error C2232: '->shipData::name' : left operand has 'struct' type, use '.' 43 1 LAB4TEST
Error 17 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 47 1 LAB4TEST
Error 18 error C2232: '->shipData::type' : left operand has 'struct' type, use '.' 47 1 LAB4TEST
Error 19 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 51 1 LAB4TEST
Error 20 error C2232: '->shipData::position' : left operand has 'struct' type, use '.' 51 1 LAB4TEST
Error 21 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 55 1 LAB4TEST
Error 22 error C2232: '->shipData::captain' : left operand has 'struct' type, use '.' 55 1 LAB4TEST
Error 23 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 60 1 LAB4TEST
Error 24 error C2232: '->shipData::name' : left operand has 'struct' type, use '.' 60 1 LAB4TEST
Error 25 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 64 1 LAB4TEST
Error 26 error C2232: '->shipData::type' : left operand has 'struct' type, use '.' 64 1 LAB4TEST
Error 27 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 68 1 LAB4TEST
Error 28 error C2232: '->shipData::position' : left operand has 'struct' type, use '.' 68 1 LAB4TEST
Error 29 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 72 1 LAB4TEST
Error 30 error C2232: '->shipData::captain' : left operand has 'struct' type, use '.' 72 1 LAB4TEST
Error 31 error C2061: syntax error : identifier 'Ship' 77 1 LAB4TEST
Error 32 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 83 1 LAB4TEST
Error 33 error C2232: '->shipData::name' : left operand has 'struct' type, use '.' 83 1 LAB4TEST
Error 34 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 84 1 LAB4TEST
Error 35 error C2232: '->shipData::name' : left operand has 'struct' type, use '.' 84 1 LAB4TEST
Error 36 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 87 1 LAB4TEST
Error 38 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 92 1 LAB4TEST
Error 39 error C2232: '->shipData::type' : left operand has 'struct' type, use '.' 92 1 LAB4TEST
Error 40 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 93 1 LAB4TEST
Error 41 error C2232: '->shipData::type' : left operand has 'struct' type, use '.' 93 1 LAB4TEST
Error 42 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 99 1 LAB4TEST
Error 43 error C2232: '->shipData::position' : left operand has 'struct' type, use '.' 99 1 LAB4TEST
Error 44 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 100 1 LAB4TEST
Error 45 error C2232: '->shipData::position' : left operand has 'struct' type, use '.' 100 1 LAB4TEST
Error 46 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 105 1 LAB4TEST
Error 47 error C2232: '->shipData::condition' : left operand has 'struct' type, use '.' 105 1 LAB4TEST
Error 48 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 106 1 LAB4TEST
Error 49 error C2232: '->shipData::condition' : left operand has 'struct' type, use '.' 106 1 LAB4TEST
Error 50 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 111 1 LAB4TEST
Error 51 error C2232: '->shipData::captain' : left operand has 'struct' type, use '.' 111 1 LAB4TEST
Error 52 error C2819: type 'shipData' does not have an overloaded member 'operator ->' 112 1 LAB4TEST
Error 53 error C2232: '->shipData::captain' : left operand has 'struct' type, use '.' 112 1 LAB4TEST
54 IntelliSense: no operator "=" matches these operands
operand types are: shipData = int 10 12 LAB4TEST
55 IntelliSense: expression must have pointer type 25 17 LAB4TEST
56 IntelliSense: expression must have pointer type 26 16 LAB4TEST
57 IntelliSense: expression must have pointer type 28 17 LAB4TEST
58 IntelliSense: expression must have pointer type 29 17 LAB4TEST
59 IntelliSense: expression must have pointer type 30 17 LAB4TEST
60 IntelliSense: expression must have pointer type 31 17 LAB4TEST
61 IntelliSense: expression must have pointer type 43 9 LAB4TEST
62 IntelliSense: expression must have pointer type 47 9 LAB4TEST
63 IntelliSense: expression must have pointer type 51 9 LAB4TEST
64 IntelliSense: expression must have pointer type 55 9 LAB4TEST
65 IntelliSense: expression must have pointer type 60 2 LAB4TEST
66 IntelliSense: expression must have pointer type 64 2 LAB4TEST
67 IntelliSense: expression must have pointer type 68 2 LAB4TEST
68 IntelliSense: expression must have pointer type 72 2 LAB4TEST
69 IntelliSense: expected a type specifier 77 25 LAB4TEST
70 IntelliSense: expression must have pointer type 83 2 LAB4TEST
71 IntelliSense: expression must have pointer type 84 9 LAB4TEST
72 IntelliSense: expression must have pointer type 87 9 LAB4TEST
73 IntelliSense: expression must have pointer type 92 2 LAB4TEST
74 IntelliSense: expression must have pointer type 93 9 LAB4TEST
75 IntelliSense: expression must have pointer type 99 2 LAB4TEST
76 IntelliSense: expression must have pointer type 100 9 LAB4TEST
77 IntelliSense: expression must have pointer type 105 2 LAB4TEST
78 IntelliSense: expression must have pointer type 106 9 LAB4TEST
79 IntelliSense: expression must have pointer type 111 2 LAB4TEST
80 IntelliSense: expression must have pointer type 112 9 LAB4TEST
END OF ERROR LIST
lab4class.cpp
line 10: ships is a struct. You can;t assign NULL to a struct.

Line 18: tempName is an uninitialized char.
Line 25: You're trying to use tempName as an index (it's garbage). Did you mean shipCount?
ships[shipCount]->name is an uninitialized pointer. You're going to get an address fault reading into that.

Line 28-31: Ditto for type, position, condition, captain.

line 23: [code]while (!eof())[/code is a poor practice. eof is set only after a read attempt fails. By putting at the top of the loop and you're logically at eof, eof is not set. You're going to execute the loop one extra time.

Lines 25-31,43,47,51,55,60,64,68,72: ships is a direct array. It is not an array of pointers. -> is inappropriate here. Use .

Line 38: starFleet::loadData() is declared as an int function. It needs to return something.

Line 77: ships is a direct array. You can't assign a new instance to it. This is not Java.

Lines 83-84,92-93,111-112: Why use C strings? Why not use std::string? BTW, strcpy is unsafe.







I understand that. here is the problem I can only use c-style string. Also I have to use strcpy I can't use string's in this class or i would. here is what I have to do.



An alien digital life form has taken up residence in the Enterprise data banks. This is a first contact opportunity, and the xenomorphology lab needs as much memory space as possible to make a hospitable environment for the digital aliens. Therefore the captain has mandated that all programs will only be allocated a minimum amount of memory. So you will re-write the code for assignment 3 to use dynamic memory whenever possible. Therefore, this lab assignment will have the same requirements as lab 3, with the added requirement that all arrays will be dynamic, and will use the minimum amount of memory.

Strategies for using the minimum amount of memory
Lab 3 required you to use a class for Starfleet ships, using an internal array of structs or classes. The Starfleet class and Ship struct (or class) might look something like the textbox to the right. Suppose you have a constant called FLEET_SIZE that you use to create your fleet array, and S_SIZE for your c-strings (char arrays).
Instead of creating an array of ships with structs in memory that may or may not be used, you will create an array of pointers for Ships. Then you will only allocate memory whenever a Ship is read from the file or entered by the user. And instead of c-string arrays that are a fixed (and long) size, you will wait until run time to find out how much memory each c-string needs. Then you will allocate exactly enough memory for the c-string, and no more. So for assignment 4, your class and struct arrangement might look something like the textbox to the left and below.
Now that you will be using Ship pointers for the Ships array, you will need a constructor for the class that will set every Ship pointer in the array to the nullptr. You can use a loop for this. Then you will have to allocate a new ship whenever you want to load the data for a ship into memory. For example, suppose the user wants to enter a new ship from the keyboard, so the method addShip() is called. One of the first things to do inside of addShip() is to set aside memory for the new ship’s data:
Ships[shipCount] = new Ship;
Now that you have a struct set aside, you will need to get and set the data (name, registry, type, etc.). Most of the members are char pointers that will point to c-strings. So to set aside just the right amount of space for a c-string, you can use a temporary c-string that is large enough to handle any name, say 128 or so, which is how we might set S_SIZE. Then we can use strlen() to find out how long the name is, and set aside that amount of memory (plus 1 for the null terminator). So suppose you want to set aside memory for the name of the ship. You could ask for the ship’s name with a temporary string:
char tempName[S_SIZE];
cout << “What is the name of the ship?: “;
cin.get(tempName, S_SIZE);
Now that you know what the name of the ship is, all you have to do is set aside the memory and copy over the name:

Ships[shipCount]->name = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(Ships[shipCount]->name, tempName);

When the user decides to quit the program (by selecting ‘q’ or ‘Q’ from the menu), you must deallocate all the dynamic memory that it used before program termination. Therefore you will need a destructor for the Starfleet class. You may also optionally create a destructor for the Ship struct. If you decide to only use a Starfleet destructor, you can delete allocated memory for each Ship item in the array:
for (int i = 0; i < shipCount; i++){
if (Ships[i] != nullptr){ // Make sure we don’t try to delete memory
// that hasn’t been allocated.
delete[] Ships[i]->name;
// Also delete the other dynamic c-strings (type, position, etc.)
}
}
Now that all of the c-strings have been deallocated, you can delete the memory set aside for the Ships array.
Please Note:
If your program has any memory leaks, you will loose points on this assignment. Therefore use valgrind on the Linux systems, or some other leak checking program or utility to make sure you deleted all dynamically allocated memory before program termination.

Design Considerations
Please follow the specifications below and do not deviate from them. Failure to follow the specifications will result in deduction of points.
1. Name your main file lab4main.cpp, and your additional files appropriately (lab4ClassImp.cpp and lab4Header.h for example). Please upload your data file as well.
2. Please be sure the source file includes your name, assignment description and number, and date, as a program comment. Also include a Sources line.
3. You MUST use a class to model the collection of starships (class Starfleet, for example).
4. Write methods for your Starfleet class to do the tasks (load the data, add a new ship, search for a ship, etc.). You may write any other methods that you think you need. You may create other nonmember functions, but if you do, they must not have any interaction with data items internal to a class.
5. Use a struct or a class named Ship, or something similar, to model the data for a starship.
6. You MUST use an array of Ship pointers to implement the above class (Starfleet). The array of structs must be internal to the class, and must be dynamically allocated.
7. You MUST use char pointers and dynamically allocated c-strings for all string-type items in a Ship struct (or class). You may optionally use a dynamically allocated c-string or an integer for a ship’s registration.
8. You must create a constructor for the Starfleet class that will set each Ship pointer in the array to the nullptr. Your constructor may optionally take care of other initialization needs, and you may optionally create other constructors with arguments if you wish.
9. You MUST use a destructor for the Starfleet class that will deallocate all dynamically allocated memory. You may optionally create a destructor for the Ship struct (or class).
10. When using a class, please make sure you encapsulate the data, which means make all the instance data members private and provide accessor methods and mutator methods to access and manipulate the data.
11. Make sure to have a delimiter written between each item in the file when you write the text file, such as a newline or a semicolon. This will be important when you read the information back from the file.

“Do-Not” List for All Labs in CS162:
• No Global Variables (you can have global constants)
• No use of the stdio library (use iostream and fstream)
• Instead of the string class, you will be using arrays of characters and the cstring library
• No goto statements.

Things You Should Do:
• Follow the style guide for this class
• Your programs should always guard against bad data being entered by mistake. Bad data means anything that could cause a stream to go into input failure.

Goals for This Lab:
• Using class to model Abstract Data Type
• OOP-Data Encapsulation
• Breaking tasks down into methods
• Dynamically allocated memory for arrays and c-strings.
• File input/output

Topic archived. No new replies allowed.