12 Replies - 562 Views - Last Post: 08 April 2012 - 09:06 AM Rate Topic: -----

#1 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Why my program will crash?

Posted 07 April 2012 - 10:18 PM

class CarSystem:public Month
{     private:
              struct node
              {   bool GreenCollection;
                  bool Compact;
                  bool FullSize;
                  bool Suv;
                  node *link;
              };
              node *head;
              
      public:
             CarSystem();
             string displayMonth(int);
             void displayResult(CarSystem);
             void reservation(Month);
             bool ValidOfCar();
};

string CarSystem::displayMonth(int i)
{   switch (i)
    {   case  1:cout<<"January";
                break;
        case  2:cout<<"February";
                break;
        case  3:cout<<"March";
                break;
        case  4:cout<<"April";
                break;
        case  5:cout<<"May";
                break;
        case  6:cout<<"June";
                break;
        case  7:cout<<"July";
                break;
        case  8:cout<<"August";
                break;
        case  9:cout<<"September";
                break;
        case 10:cout<<"October";
                break;
        case 11:cout<<"November";
                break;
        case 12:cout<<"December";
                break;
    }//end switch statement 
}
        
                
void CarSystem::displayResult(CarSystem abc)
{   //node *current;
    //current=head;
    cout<<"_________________________________________________________________________"<<endl;
    
   [color="#FF0000"]int y=1;
    abc.displayMonth(y);[/color]
    /*while(current->link!=NULL)
    { cout<<current->GreenCollection<<" "<<current->Compact<<" "<<current->FullSize<<" "<<current->Suv<<endl;
      current=current->link;
    }*/
}


If the red statement replace with
abc.displayMonth(1);

then the program can display January.
Why the above one cannot display and will crash?

This post has been edited by UrIkOn: 07 April 2012 - 10:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Why my program will crash?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,182
  • Joined: 21-June 11

Re: Why my program will crash?

Posted 07 April 2012 - 11:28 PM

Your displayMonth function is declared to return a string. However it does not contain any return statements. That means the function will finish without returning a value. This causes undefined behavior. In practical terms that means that your stack will be messed up because the call-site will pop a return value from the stack that is not there.

That the code will "work" if you remove the y variable is pure coincidence. Without the variable the stack just happens to be set up in a way that masks the error. Either way your code is wrong.
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Why my program will crash?

Posted 07 April 2012 - 11:34 PM

If you really want it not to return anything declare it as void.
Was This Post Helpful? 0
  • +
  • -

#4 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Why my program will crash?

Posted 08 April 2012 - 04:38 AM

Thank You for your answer.
Another question:
Why this one cannot work?

class CarSystem:public Month
{     private:
              struct node
              {   bool GreenCollection;
                  bool Compact;
                  bool FullSize;
                  bool Suv;
                  node *link;
              };
              node *head;
              
      public:
             CarSystem();
             void displayResult(int);
             void reservation(Month);
             const CarSystem &operator=(const CarSystem& otherlist);
};

const CarSystem &CarSystem::operator = (const CarSystem &otherSystem)
{
      cout<<"111"<<endl;
      if(this!=&otherSystem)
      {   node*p,*class2;
          p=head;
          class2=new node;
          class2->GreenCollection=p->GreenCollection;
          class2->Compact=p->Compact;
          class2->FullSize=p->FullSize;
          class2->Suv=p->Suv;
          class2->link=NULL;
          cout<<class2->Suv<<endl;
          p=p->link;
          int i;
          node* current;
         for(i=0,current=class2;i<13;i++)
          {   node *q;
              q=new node;
              q->GreenCollection=p->GreenCollection;
              q->Compact=p->Compact;
              q->FullSize=p->FullSize;
              q->Suv=p->Suv;
              if(i+1==13)
             {  current->link=NULL;   }
             else
             {
                current->link=q;
                current=q;
             }
          }
     }
     return *this;
}

int main()
{   
    CarSystem abc;
    int choice;
    do
    { 
       cin>>choice;
       else if(choice==4)
       {   CarSystem abd;
           [b]abd=abc;  [/b]
           displayMonth(abd);         }
    }while(choice!=0);
    
    system("PAUSE");
    return 0;
} 


Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Why my program will crash?

Posted 08 April 2012 - 06:16 AM

Post your error messages, exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Why my program will crash?

Posted 08 April 2012 - 06:17 AM

Where's the CarSystem constructor?
Was This Post Helpful? 0
  • +
  • -

#7 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Why my program will crash?

Posted 08 April 2012 - 06:41 AM

I just post out the important part,because my whole program got 400++lines.
The compilation dint have any error.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Why my program will crash?

Posted 08 April 2012 - 06:48 AM

Quote

Why this one cannot work?

And what do you think the above really tells us?

Quote

I just post out the important part,because my whole program got 400++lines.

Important for what?

Quote

The compilation dint have any error.

Then you need to thoroughly explain what is wrong with your program. We can't compile the code you supplied, so you need to explain what is happening. Give us some kind of clue as to what is wrong.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Why my program will crash?

Posted 08 April 2012 - 07:42 AM

ok..sorry for my answering.
I will post out my code now.
My problem is I want to backup my data.
So,my idea is using operator = to copy the linked list to a new linked list.
Sorry again!
#include<iostream>
#include<iomanip>
using namespace std;

class Month
{     public:
             Month();
             Month(char,char,char);
             Month(int);
             void inputMonth(char,char,char);
             int getMonth();
             
      private:
              int month;
};

class CarSystem:public Month
{     private:
              struct node
              {   bool GreenCollection;
                  bool Compact;
                  bool FullSize;
                  bool Suv;
                  node *link;
              };
              node *head;
              
      public:
             CarSystem();
             void displayResult(int);
             void reservation(Month);
             const CarSystem &operator=(const CarSystem& otherlist);
};

const CarSystem &CarSystem::operator = (const CarSystem &otherSystem)
{
      cout<<"111"<<endl;
      if(this!=&otherSystem)
      {   node*p,*class2;
          p=head;
          class2=new node;
          class2->GreenCollection=p->GreenCollection;
          class2->Compact=p->Compact;
          class2->FullSize=p->FullSize;
          class2->Suv=p->Suv;
          class2->link=NULL;
          cout<<class2->Suv<<endl;
          p=p->link;
          int i;
          node* current;
         /* for(i=0,current=class2;i<13;i++)
          {   node *q;
              q=new node;
              q->GreenCollection=p->GreenCollection;
              q->Compact=p->Compact;
              q->FullSize=p->FullSize;
              q->Suv=p->Suv;
              if(i+1==13)
             {  current->link=NULL;   }
             else
             {
                current->link=q;
                current=q;
             }
          }*/
     }
     return *this;
}

CarSystem::CarSystem()//default constructor for class CarSystem
{   
    node *current;
    head=new node;
    head->GreenCollection=true;
    head->Compact=true;
    head->FullSize=true;
    head->Suv=true;
    head->link=NULL;
    
    int i;
    for(i=0,current=head;i<13;i++)
    {   node *newNode;
        newNode=new node;
        newNode->GreenCollection=true;
        newNode->Compact=true;
        newNode->FullSize=true;
        newNode->Suv=true;
        if(i+1==13)
        {  current->link=NULL;   }
        else
        {
           current->link=newNode;
           current=newNode;
        }
     }
}  
                
void CarSystem::displayResult(int i)//for display purpose
{   node *current;
    current=head;
    
    for(int j=1;j<i;j++)
    {   current=current->link;   }
    
    if(current->GreenCollection==true)
    {  cout<<"Green Collection |";  }
    else
    {  cout<<"        X        |";  }
    
    if(current->Compact==true)
    {  cout<<"Compact/MidSize |";   }
    else
    {  cout<<"        X       |";  }
    
    if(current->FullSize==true)
    {  cout<<"Full Size/Standard|"; }
    else
    {  cout<<"        X         |";  }
    
    if(current->Suv==true)
    {  cout<<"SUV/MiniVan|";   }
     else
    {  cout<<"    X      |";  }
    cout<<endl;

}

void CarSystem::reservation(Month month1)//for the reservation purpose
{   cout<<endl;
    cout<<"What car you choose?"<<endl;
    cout<<"1.Green Collection  "<<endl;
    cout<<"2.Compact/MidSize  "<<endl;
    cout<<"3.Full Size/Standard  "<<endl;
    cout<<"4.SUV/MiniVan  "<<endl;
    int choice;
    cout<<"Car choice: ";
    cin>>choice;
    node *current;
    int i;
    if(choice==1)
    {   for(i=1,current=head;i<month1.getMonth();i++)
        {   current=current->link;   }
        if(current->GreenCollection==false)
        {   cout<<"The car is not available!Please choose another choice!"<<endl;   }
        else
        {   current->GreenCollection=false;}
    }
    else if(choice==2)
    {   for(i=1,current=head;i<month1.getMonth();i++)
        {   current=current->link;   }
        if(current->Compact==false)
        {   cout<<"The car is not available!Please choose another choice!"<<endl;   }
        else
        {   current->Compact=false;   }
    }
    else if(choice==3)
    {   for(i=1,current=head;i<month1.getMonth();i++)
        {   current=current->link;   }
        if(current->FullSize==false)
        {   cout<<"The car is not available!Please choose another choice!"<<endl;   }
        else
        {   current->FullSize=false;  } 
    }
    else if(choice==4)
    {   for(i=1,current=head;i<month1.getMonth();i++)
        {   current=current->link;   }
        if(current->Suv==false)
        {   cout<<"The car is not available!Please choose another choice!"<<endl;   }
        else
        {   current->Suv=false;   }
    }
    else
    {   cout<<"Invalid Input!"<<endl;   }
    cout<<endl;
    system("PAUSE");
    system("CLS");
}

Month::Month()//default constructor for class month
{   month=1;   }

Month::Month(char ch1,char ch2,char ch3)//constructor that set the month using an integer as an argument
{   ch1=tolower(ch1);
    ch2=tolower(ch2);
    ch3=tolower(ch3);
    
    if(ch1=='j')
    {   if(ch2=='a')
        {   if(ch3=='n')
            {  month=1;  }
        }
        else
        {   
            if(ch3=='n')
            {   month=6;  }
            else 
            {   month=7;  }
        }
    }
    else if(ch1=='f')
    {  if(ch2=='e')
       {   
           if(ch3=='b')
           {   month=2;   }
       }
    }
    else if(ch1=='m')
    {   if(ch2=='a')
        {   if(ch3=='r')
            {   month=3;   }
            else
            {   month=5;   }
        }
    }   
    else if(ch1=='a')
    {   if(ch2=='p')
        {   if(ch3=='r')
            {   month=4;  }
        }
        else
        {   if(ch3=='g')
            {   month=8;   }
        }
    }
    else if(ch1=='s')
    {   if(ch2=='e')
        {   if(ch3=='p')
            {   month=9;  }
        }
    }
    else if(ch1=='o')
    {   if(ch2=='c')
        {   if(ch3=='t')
            {   month=10;  }
        }
    }
    else if(ch1=='n')
    {   if(ch2=='o')
        {   if(ch3=='v')
            {   month=11;   }
        }
    }
    else
    {   if(ch2=='e')
        {   if(ch3=='c')
            {   month=12;   }
        }
    }
}

Month::Month(int input)//constructor that reads the month as an integer
{   switch(input)
    {   case  1:month=1;
                break;
        case  2:month=2;
                break;
        case  3:month=3;
                break;
        case  4:month=4;
                break;
        case  5:month=5;
                break;
        case  6:month=6;
                break;
        case  7:month=7;
                break;
        case  8:month=8;
                break;
        case  9:month=9;
                break;
        case 10:month=10;
                break;
        case 11:month=11;
                break;
        case 12:month=12;
                break;
        default:cout<<"Invalid input"<<endl;
    }
}

int Month::getMonth()//return the value of month
{   return month;   }
 
void CreateClass(CarSystem abc)//Function that create class using month as integer
{   
    cout<<"Enter month in integer: ";
    int month;
    cin>>month;
    Month systemMonth(month);
    if(month>=1&&month<13)
    { abc.reservation(systemMonth);}
   
}

bool validityOfInput(char ch1,char ch2,char ch3)
{   ch1=tolower(ch1);
    ch2=tolower(ch2);
    ch3=tolower(ch3);
    
    if(ch1=='j')
    {   if(ch2=='a')
        {   if(ch3=='n')
            {  return true;  }
            else
            {  return false; }
        }
        else if(ch2=='u')
        {   
            if(ch3=='n')
            {  return true; }
            else if(ch3=='l')
            {   return true;  }
            else
            {  return false; }
        }
        else
        {   return false;  }
    }
    else if(ch1=='f')
    {  if(ch2=='e')
       {   
           if(ch3=='b')
           {    return true;   }
           else
           {   return false;   }
       }
       else
       {  return false;  }
    }
    else if(ch1=='m')
    {   if(ch2=='a')
        {   if(ch3=='r')
            {   return true;   }
            else if(ch3=='y')
            {   return true;   }
            else
            {   return false;   }
        }
        else
        {   return false;   }
    }   
    else if(ch1=='a')
    {   if(ch2=='p')
        {   if(ch3=='r')
            {   return true;    }
            else
            {   return false;   }
        }
        else if(ch2=='u')
        {   if(ch3=='g')
            {  return true;      }
            else
            {   return false;   }
        }
        else
        {   return false;   }
    }
    else if(ch1=='s')
    {   if(ch2=='e')
        {   if(ch3=='p')
            {   return true;   }
            else
            {  return false;   }
        }
        else
        {   return false;   }
    }
    else if(ch1=='o')
    {   if(ch2=='c')
        {   if(ch3=='t')
            {   return true;   }
            else
            {   return false;   }
        }
        else
        {    return false;   }
    }
    else if(ch1=='n')
    {   if(ch2=='o')
        {   if(ch3=='v')
            {   return true;   }
            else
            {  return false;   }
        }
        else
        {    return false;   }
    }
    else if(ch1=='d')
    {   if(ch2=='e')
        {   if(ch3=='c')
            {   return true;   }
            else
            {    return false;   }
        }
        else
        {    return false;   }
    }
    else
    {    return false;   } 
}

void CreateClassAlp(CarSystem abc)//Function that create class using 3 first letter of month
{
     cout<<"The first three letter of month: ";
     char ch1,ch2,ch3;
     cin>>ch1>>ch2>>ch3;
     if(validityOfInput(ch1,ch2,ch3)==true)//check the validity of input first three letter of the month
     {  Month systemMonth(ch1,ch2,ch3);
        abc.reservation(systemMonth);
     }
     else
     {  cout<<"Invalid Input!"<<endl;  }
}

void displayMonth(CarSystem abc)//display purpose
{   cout<<"_______________________________________________________________________________"<<endl;
    for(int i=1;i<13;i++)
    { cout<<setw(12);
      switch (i)
      {   case  1:cout<<"|  January |";
                  abc.displayResult(1);
                  break;
          case  2:cout<<"| February |";
                  abc.displayResult(2);
                  break;
          case  3:cout<<"|    March |";
                  abc.displayResult(3);
                  break;
          case  4:cout<<"|    April |";
                  abc.displayResult(4);
                  break;
          case  5:cout<<"|      May |";
                  abc.displayResult(5);
                  break;
          case  6:cout<<"|     June |";
                  abc.displayResult(6);
                  break;
          case  7:cout<<"|     July |";
                  abc.displayResult(7);
                  break;
          case  8:cout<<"|   August |";
                  abc.displayResult(8);
                  break;
          case  9:cout<<"|September |";
                  abc.displayResult(9);
                  break;
          case 10:cout<<"|  October |";
                  abc.displayResult(10);
                  break;
          case 11:cout<<"| November |";
                  abc.displayResult(11);
                  break;
          case 12:cout<<"| December |";
                  abc.displayResult(12);
                  break;
      }//end switch statement 
    }//end for loop
     cout<<"_______________________________________________________________________________"<<endl;
    system("PAUSE");
    system("CLS");
}          

int main()
{   
    CarSystem abc;
    int choice;
    do
    {  cout<<"1.Input the month as an integer."<<endl;
       cout<<"2.Input the month as the first three letters in the name of the month."<<endl;
       cout<<"3.Display the system data."<<endl;
       cout<<"4.Backup."<<endl;
       cout<<"0.Exit the program."<<endl;
       cout<<"My Choice: ";
       cin>>choice;
       if(choice==1)
       {   CreateClass(abc);   }
       else if(choice==2)
       {   CreateClassAlp(abc);   }
       else if(choice==3)
       {   displayMonth(abc);   }
       else if(choice==4)
       {   CarSystem abd;
           abd=abc;  
           displayMonth(abd);         }
    }while(choice!=0);
    
    system("PAUSE");
    return 0;
} 

      


This post has been edited by UrIkOn: 08 April 2012 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Why my program will crash?

Posted 08 April 2012 - 07:57 AM

class CarSystem:public Month


A CarSystem is a Month??? That inheritance seems very wrong. Also this:

bool GreenCollection;
bool Compact;
bool FullSize;
bool Suv;


seems wrong as well.

We needed to see the constructor to see what you were doing in there with your pointers.

Once you start getting into programs of this relative complexity, it's time to get to know your debugger. What development environment are you using to write this program?

The assignment operator would usually replace whatever is contained in the object on the left with whatever is on the right side of the =, not make a new list.
Was This Post Helpful? 0
  • +
  • -

#11 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Why my program will crash?

Posted 08 April 2012 - 08:07 AM

I am using devC++ to write my program.
My idea is there are 4 type of car in this car system.
Every month me implementing as a node,
so there are 12 node.
The datatype is boolean,because if the car is true,then the car is available.
That is all my idea.
I will receive all your comment.Thank You!

This post has been edited by UrIkOn: 08 April 2012 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Why my program will crash?

Posted 08 April 2012 - 08:14 AM

Ugh, Dev-C++. The worst debugger out there of course. Old, buggy, and unsupported.

Read some of the links in this Google search to learn how to debug.
Was This Post Helpful? 0
  • +
  • -

#13 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Why my program will crash?

Posted 08 April 2012 - 09:06 AM

The school want us to use devC++.
That mean I must debug to find out the problem myself,right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1