Why does my program hangs?
Nov 2, 2012 at 4:09pm UTC
Can anyone please help? Whenever I input the first coordinate of x pt1, my program just hangs.
The code is below..
Shape
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
// shape.h
#include <string>
#ifndef SHAPE_H
#define SHAPE_H 1
using namespace std;
class Shape
{
protected :
int x, y;
string name;
public :
// a simple inline constructor
Shape(int new_x, int new_y, string new_name): x(new_x), y(new_y), name(new_name)
{
return ;
};
virtual ~Shape()
{
return ;
};
// inline getter/setter functions
string getName() { return name; };
void setName(string new_name)
{
name = new_name;
}
int getX() { return x; };
void setX(int set_x)
{
x = set_x;
}
int getY() { return y; };
void setY(int set_y)
{
y = set_y;
}
void toString();
};
#endif
Square
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
// square.h
#ifndef SQUARE_H
#define SQUARE_H 1
#include <string>
#include "Shape.h"
using namespace std;
class Square : public Shape
{
protected :
int size;
public :
// a c'tor that calls the parent class's c'tor
Square(int new_x, int new_y, string new_name): Shape(new_x, new_y, new_name)
{
return ;
};
void setXY();
Square *arraySquare[1];
};
void Square::setXY()
{
int count = 0;
for (int i=0; i<4; i++)
{
cout<<"Please enter x-ordinate of pt. " <<i+1<<" : " ;
cin>>x;
arraySquare[count]->setX(x);
cout<<"Please enter y-ordinate of pt. " <<i+1<<" : " ;
cin>>y;
arraySquare[count]->setY(y);
count++;
}
}
#endif
Main
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
#include <iostream>
#include <string>
#include "Shape.h"
#include "Square.h"
using namespace std;
class Main
{
public :
void mainMenu();
char menuChoice;
void stringToUpper(string &s);
};
void stringToUpper(string &s)
{
for (unsigned int l = 0; l < s.length(); l++)
{
s[l] = toupper(s[l]);
}
}
void Main::mainMenu()
{
cout<<"Welcome to Main program!" <<endl<<endl;
cout<<"1) Input data" <<endl;
cout<<"2) 2" <<endl;
cout<<"3) 3" <<endl;
cout<<"4) 4" <<endl;
cout<<"Q) Enter 'Q' to quit" <<endl<<endl;
}
int main()
{
char menuChoice;
bool quit=false ;
Main main;
Square *square;
string shape, special;
while ( !quit )
{
main.mainMenu();
cout<<"Please enter your choice : " ;
cin>>menuChoice;
menuChoice = toupper(menuChoice);
switch (menuChoice)
{
case '1' :
cout<<endl<<"[ Input data ]" <<endl;
cout<<"Please enter name of shape : " <<endl;
cin>>shape;
stringToUpper(shape);
if (shape=="SQUARE" )
{
square->setXY();
}
break ;
case '2' :
cout<<"Print" <<endl<<endl;
break ;
case '3' :
cout<<"You choosen 3" <<endl<<endl;
break ;
case '4' :
cout<<"You choosen 4" <<endl<<endl;
break ;
case 'Q' :
cout<<"You have chosen to quit!" <<endl<<endl;
quit=true ;
exit(0);
default :
cout<<"Invalid entry!" <<endl<<endl;
break ;
}
}
}
Nov 2, 2012 at 4:36pm UTC
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
class Square : public Shape
{
protected :
int size;
public :
// a c'tor that calls the parent class's c'tor
Square(int new_x, int new_y, string new_name): Shape(new_x, new_y, new_name)
{
return ;
};
void setXY();
Square *arraySquare[1];
};
void Square::setXY()
{
int count = 0;
for (int i=0; i<4; i++)
{
cout<<"Please enter x-ordinate of pt. " <<i+1<<" : " ;
cin>>x;
arraySquare[count]->setX(x);
cout<<"Please enter y-ordinate of pt. " <<i+1<<" : " ;
cin>>y;
arraySquare[count]->setY(y);
count++;
}
}
So a Square has an array of one pointer to Square? This pointer is never initialized so it's pointing to some random place in memory. You then try to write to the memory it's pointing to. You then try to access it as if it is an array larger than one element, interpreting more random memory as a pointer to.. random memory, which you then try to write to.
That's a lot of bad mojo.
Nov 2, 2012 at 4:49pm UTC
It hangs also when my arraySquare is at [100]. Any idea how do I initialize or fix this?
Nov 2, 2012 at 5:15pm UTC
Well, the obvious thing would be to remove arraySquare from the Square class, since a square should not be a container of squares (or pointers to squares.)
Then, use a container.
Nov 2, 2012 at 5:56pm UTC
Could you please guide me and show me an example? I'm new to c++
Topic archived. No new replies allowed.