Problem with 3 lines of code, a simple problem?

Hey everyone, so this is my first post, kind of made it because I needed
some help with this program, everything is here. The entire shell of the program is pretty much solid, however in my insert function (line228 and 237) there is a problem, i think mixing up pointers or its NULL or something of the sort. And the same with my print function on (line151),(ignore the cout "yeswthman test function right before it). I've been on this for a while and I think a second mind might be a better idea.



#include <iostream>
#include <string>
#include <cstddef>
#include <fstream>
#include <cmath>
#include <stdio.h>
#include <ctype.h>

using namespace std;

void printAll();
void flush();
void branching(char c);
void cleanUp();

int main()
{
char ch = 'i';

ungetc('\n', stdin);

do {
printf("Enter your selection.\n");
printf("\tTo insert a new shape, enter i.\n");
printf("\tTo print all shapes, enter p.\n");
printf("\tTo quit, enter q.\n");

flush();
ch = tolower(getchar());
branching(ch);
} while (ch != 113);

return 0;
}

class ShapeNodeList {

public:
friend class Shape;
Shape *shape;
ShapeNodeList *next;

ShapeNodeList()
{
shape = NULL;
next = NULL;
}
//setter functions
void setShape(Shape *shape)
{
this->shape = shape;
}
void setNext(Shape *next)
{
this->shape = shape;
}
//getter functions
ShapeNodeList * getNext()
{
return (this->next);
}
Shape * getShape()
{
return (this->shape);
}

} *head = NULL;

class Shape
{
public:
virtual float area()
{
return 0;
};
virtual void displayInfo()
{
cout << "This is an unknown shape" << endl;
};
};

class Rectangle: public Shape
{
private:
float base;
float height;
protected:
float base1 = base;
float height1 = height;
public:
virtual float area()
{
return (base*height);
}
virtual void displayInfo()
{
cout << "This is a rectangle" << endl;
}
void setBase(float newB)
{
base = newB;
}
void setHeight(float newH)
{
height = newH;
}
};

class Circle: public Shape
{
private:
float radius = 1;
float pie = 3.14159265;

public:
virtual float area()
{
return (pie*(radius*radius));
};
virtual void displayInfo()
{
cout << "This is a circle" << endl;
};
void setRadius(float newR)
{
radius = newR;
}
};

class Square: public Rectangle
{
public:
virtual float area()
{
return (height1*height1);
};
virtual void displayInfo()
{
cout << "This is a square" << endl;
};
};

void printAll()
{
ShapeNodeList *temp = head;
if (head == NULL)
{
cout << "yeswthman" << endl;
}

Shape *printer = head->getShape();


while (temp != 0)
{
printer = temp->getShape();
printer->displayInfo();
printf("Area %.2f\n\n", printer->area());
temp = temp->getNext();
}
}



void flush()
{
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}


void insert()
{
Shape *newShape;

int userShape = 0;
int properI = 0;
printf("What shape would you like to insert? (1: rectangle, 2:square, 3: circle\n");
while(properI == 0)
{
cin >> userShape;

if (userShape == 1)
{
float holder;

Rectangle newShape;
cout << "Enter side length 1 " << endl;
cin >> holder;
newShape.setBase(holder);
cout << "Enter side length 2 " << endl;
cin >> holder;
newShape.setHeight(holder);
properI = 1;
}
else if (userShape == 2)
{
float holder;
Square newShape;
cout << "Enter the side length." << endl;
cin >> holder;
newShape.setBase(holder);
properI = 1;
}
else if (userShape == 3)
{
float holder;
Circle newShape;
cout << "Enter the radius." << endl;
cin >> holder;
newShape.setRadius(holder);
properI = 1;
}
else
{
cout << "Invalid input try again" << endl;
}
}
if (properI == 1)
{
ShapeNodeList *temp;
temp = head;

if (head == NULL)
{
temp->setShape(newShape);
head = temp;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp->setNext(newShape);

}
}


}
void cleanUp()
{
ShapeNodeList *prev = head;
for (ShapeNodeList *temp = prev->next; temp != NULL; temp = temp->next)
{
delete prev;
prev = temp;
}
delete prev;
}


void branching(char c) {
switch (c) {
case 'i':
insert();
break;
case 'p':
printAll();
break;
case 'q':
cleanUp();
break;
default:
printf("Invalid input\n");
}
}
Could you use [code] tags around your code?
1
2
3
4
5
6
7
8
        ShapeNodeList *temp;
        temp = head;

        if (head == NULL)
        {
            temp->setShape(newShape);
            head = temp;
        }


you assign head to temp, so if head is null, that means temp is also null. Thus it is guaranteed that when you take the if branch above and try to setShape(), you're dereferencing a null...
should I make the " temp = head;" to "temp = temp->setShape(newShape);", I'm not completely sure I should be inserting at that point. A ShapeNodeList wouldn't be considered null at that point?
If head is NULL, you need to create a new node first.

1
2
3
4
        if (head == NULL)
        {
           head = new Shape;
        }


You can then do your setting up of the object.

1
2
3
4
5
        ShapeNodeList *temp;
        temp = head;
        temp->setShape(newShape);
        head = temp;


Though as head is temp, you might as well just do all the operations on head.

 
        head->setShape(newShape);


Usually for inserting a node in a list, which is what I think you're trying to do, the sequence would be:

1
2
3
4
        ShapeNodeList* temp = new Shape;
        temp->setShape(newShape);
        temp->next = head;
        head = temp;

Topic archived. No new replies allowed.