Solved

dll

Posted on 2004-09-28
5
238 Views
Last Modified: 2010-04-01
hi
 experts

    i have a runtime error in my program in c++.it is a very small program but not able to debug it.there is the source code.

#include <iostream>
using namespace std;
int insert();
int print();
main()
{
   struct node
   {
         int info;
         node *next;
   };

      node* head=NULL;
      node* temp=NULL;
      node* temp1=NULL;
      head=new node;
      head->next=NULL;
      int c;
      cout<<"1--->insert."<<endl<<"2--->delete."<<endl<<"3--->print."<<endl<<"4--->Exit."<<endl;
      cout<<"Enter the choice --";
      cin>>c;
      switch(c)
      {
      case 1:insert();
            {
              int x;
            cout<<"Enter the data to be insterted __ ";
              cin>>x;
              if(head->next==NULL)
                head->info=x;
              else
                  {
                  temp=head;
                  while(temp->next!=NULL)
                    temp=temp->next;
                  if(temp->next==NULL)
                    {
                     temp1=new node;
               temp1->info=x;
                     temp->next=temp1;
                     temp1->next=NULL;
                    }
                  }
            }
            break;
//      case 2:del();break;
    case 3:print();
            {
      if(head==NULL)
            cout<<"No elements to display";
      else
      {
      temp=head;
       do
         {
               cout<<temp->info<<"->";
               temp=temp->next;
         }while(temp->next!=NULL);
         }
}
break;
      case 4:exit(0);break;
      default:cout<<"Entered wrong choice";
      }
      return 0;
}
 

please help me asap.
0
Comment
Question by:rajashekar2004
5 Comments
 

Accepted Solution

by:
amjedmsa earned 20 total points
Comment Utility
why int insert();

here a modifiyed one it works

#include <iostream>

using namespace std;


int insert;
int print;
main()
{
   struct node
   {
        int info;
        node *next;
   };

     node* head=NULL;
     node* temp=NULL;
     node* temp1=NULL;
     head=new node;
     head->next=NULL;
     int c;
     cout<<"1--->insert."<<endl<<"2--->delete."<<endl<<"3--->print."<<endl<<"4--->Exit."<<endl;
     cout<<"Enter the choice --";
     cin>>c;
     switch(c)
     {
     case 1:insert;
          {
             int x;
            cout<<"Enter the data to be insterted __ ";
             cin>>x;
             if(head->next==NULL)
              head->info=x;
             else
               {
                temp=head;
                while(temp->next!=NULL)
                 temp=temp->next;
                if(temp->next==NULL)
                 {
                  temp1=new node;
               temp1->info=x;
                  temp->next=temp1;
                  temp1->next=NULL;
                 }
               }
          }
          break;
//     case 2:del();break;
    case 3:print;
          {
     if(head==NULL)
          cout<<"No elements to display";
     else
     {
      temp=head;
       do
        {
             cout<<temp->info<<"->";
             temp=temp->next;
        }while(temp->next!=NULL);
        }
}
break;
     case 4:exit(0);break;
     default:cout<<"Entered wrong choice";
     }
     return 0;
}
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Maybe you want that:

#include <iostream>
using namespace std;

struct node
{
   int info;
   node *next;
};

void insert(node*& head);
void print(node* head);
void deleteAll(node* head);

int main()
{
   
    node* head= NULL;
   
    while (true)
    {
        int c;
        cout<<endl<<"1--->insert."<<endl<<"2--->delete."<<endl<<"3--->print."<<endl<<"4--->Exit."<<endl;
        cout<<"Enter the choice --";
        cin>>c;
       
        switch(c)
        {
        case 1:
            insert(head);
            break;
            //     case 2:del();break;
        case 3:
            print(head);
            break;
        case 4:
            deleteAll(head);
            return 0;
        default:
            cout<<"Entered wrong choice"<<endl;
            break;
        }
    }
    return 1;
}


void insert(node*& head)
{  
    node* temp;
    node* temp1;
    int x;
    cout<<"Enter the data to be inserted __ ";
    cin>>x;
    if(head==NULL)
    {
        head = new node;
        head->next = NULL;
        head->info = x;
        return;
    }
    temp=head;
    while(temp->next!=NULL)
        temp=temp->next;
    temp1=new node;
    temp1->info=x;
    temp->next=temp1;
    temp1->next=NULL;
}

void print(node* head)
{
    if(head==NULL)
        cout<<"No elements to display";
    else
    {
        node* temp=head;
        do
        {
            cout<<temp->info<<"->";
            temp=temp->next;
        }
        while(temp !=NULL);
    }
}


void deleteAll(node* head)
{
    node* temp = head;
    node* temp1;
   
    while (temp != NULL)
    {
        temp1 = temp->next;
        delete temp;
        temp = temp1;
    }
}

Regards, Alex
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
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 clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now