Solved

dll

Posted on 2004-09-28
5
250 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
ID: 12177039
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
ID: 12177675
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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++.

920 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

16 Experts available now in Live!

Get 1:1 Help Now