Solved

Strange runtime error in simple program

Posted on 2002-04-21
3
188 Views
Last Modified: 2010-04-02
this is just a fairly simple program i wrote for school
but i am getting a strange error during runtime
from a list of 7 options, when i choose 3 of them the program crashes in windows and gives a core dump in unix
these 3 are choice #2 #4 #6. the code for each of them is quite similar no obivious reason for these errors
is there some kind of pattern? everyother choice seems to suffer from this. i am using a switch statement in the main function to call the appropriate class member functions.

the code is rather long to post here so you can access the zip file containing all the files here:
http://gdstudios.hypermart.net/code.zip

p.s. some of the functions are left as stubs, that was intentional
0
Comment
Question by:grfik
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
LoungeLizard earned 100 total points
ID: 6958608
You use pointers without first initialising them! Your main should look something like:

int main()
{
   int choice;
   Triangle *t = new Triangle;
   RightTriangle *rt = new RightTriangle;
   Quads *q = new Quads;
   Parallelogram *p = new Parallelogram;
   Rhombus *r = new Rhombus;
   Rectangle *rect = new Rectangle;
   Square *s = new Square;
   
   do
   {
      displayMenu();
      cin >> choice;

      switch(choice)
      {
         case 1:
            t->getInfo();
            t->output();
            break;
         case 2:
            rt->getInfo();
            rt->output();
            break;
         case 3:
            q->getInfo();
            q->output();
            break;
         case 4:
            p->getInfo();
            p->output();
            break;
         case 5:
           
            r->getInfo();
            r->output();
            break;
         case 6:
           
            rect->getInfo();
            rect->output();
            break;
         case 7:
           
            s->getInfo();
            s->output();
            break;
         case 0:
            cout << "Good Bye\n";
            break;
         default:
            cout << "Error!\n";
            return 1;
      }// switch

   } while(choice != 0);

   return 0;
}
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6958612
You could still put the new's inside the switch statement, if you want to create new variables every time the switch is executed.
0
 

Author Comment

by:grfik
ID: 6959060
umm i knew there would be a simple solution, thx loungelizard
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question