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