Errors

This is a discussion on Errors within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I'm new to C++ and was taking a tutorial where at the end they give you a code. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    Talking Errors

    Hi guys. I'm new to C++ and was taking a tutorial where at the end they give you a code. I figured I would plug it in to my compiler (Dev-C++) and run it.
    I got like 4 error messages. The first and second I don't understand, I took away the "h" from iostream trying to fix the third one and got a whole mess of errors, and I thought if you put void in front of something it did not have to return a number!

    Well see for your self....
    Here's the code:

    Code:
    struct node
      {  char name[20];    // Name of up to 20 letters
         int age;          // D.O.B. would be better
         float height;     // In metres
         node *nxt;// Pointer to next node
      };
    
    node *start_ptr = NULL;
    node *current;		 // Used to move along the list
    int option = 0;
    
    void add_node_at_end()
      {  node *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node;
         cout << "Please enter the name of the person: ";
         cin >> temp->name;
         cout << "Please enter the age of the person : ";
         cin >> temp->age;
         cout << "Please enter the height of the person : ";
         cin >> temp->height;
         temp->nxt = NULL;
    
         // Set up link to this node
         if (start_ptr == NULL)
           { start_ptr = temp;
    	 current = start_ptr;
           }
         else
           { temp2 = start_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    
    void display_list()
      {  node *temp;
         temp = start_ptr;
         cout << endl;
         if (temp == NULL)
           cout << "The list is empty!" << endl;
         else
           { while (temp != NULL)
    	   {  // Display details for what temp points to
                  cout << "Name : " << temp->name << " ";
                  cout << "Age : " << temp->age << " ";
    	      cout << "Height : " << temp->height;
    	      if (temp == current)
    		cout << " <-- Current node";
                  cout << endl;
    	      temp = temp->nxt;
    
    	   }
    	 cout << "End of list!" << endl;
           }
      }
    
    void delete_start_node()
       { node *temp;
         temp = start_ptr;
         start_ptr = start_ptr->nxt;
         delete temp;
       }
    
    void delete_end_node()
       { node *temp1, *temp2;
         if (start_ptr == NULL)
              cout << "The list is empty!" << endl;
         else
            { temp1 = start_ptr;
              if (temp1->nxt == NULL)
                 { delete temp1;
                   start_ptr = NULL;
                 }
              else
                 { while (temp1->nxt != NULL)
                    { temp2 = temp1;
                      temp1 = temp1->nxt;
                    }
                   delete temp1;
                   temp2->nxt = NULL;
                 }
            }
       }
    
    void move_current_on ()
       { if (current->nxt == NULL)
          cout << "You are at the end of the list." << endl;
         else
          current = current->nxt;
       }
    
    void move_current_back ()
       { if (current == start_ptr)
          cout << "You are at the start of the list" << endl;
         else
          { node *previous;     // Declare the pointer
            previous = start_ptr;
    
            while (previous->nxt != current)
              { previous = previous->nxt;
              }
            current = previous;
          }
       }
    
    
    void main()
      {  start_ptr = NULL;
         do
    	{
    	  display_list();
    	  cout << endl;
    	  cout << "Please select an option : " << endl;
    	  cout << "0. Exit the program." << endl;
    	  cout << "1. Add a node to the end of the list." << endl;
    	  cout << "2. Delete the start node from the list." << endl;
    	  cout << "3. Delete the end node from the list." << endl;
    	  cout << "4. Move the current pointer on one node." << endl;
              cout << "5. Move the current pointer back one node." << endl;
              cout << endl << " >> ";
    	  cin >> option;
    
    	  switch (option)
    	    {
    	      case 1 : add_node_at_end(); break;
    	      case 2 : delete_start_node(); break;
    	      case 3 : delete_end_node(); break;
    	      case 4 : move_current_on(); break;
                  case 5 : move_current_back();
    	    }
    	}
         while (option != 0);
      }
    Here are the errors: (I numbered them)

    (1) In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31, from C:\Documents and Settings\Administrator\My Documents\Untitled1.cpp


    (2) from C:\Documents and Settings\Administrator\My Documents\Untitled1.cpp



    (3) #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

    (4) `main' must return `int'

    -THANKS!

    P.S. I may be totally wrong in my assumptions

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    main is a special function that is defined by the C++ language to return int. You have to make it return int because the language says so. All other functions can be void if you choose. Change the void to int in front of main() and you'll be good with #4 (main is also special in that 0 is returned automatically if you don't do it yourself inside the function).

    The first two "errors" are not errors, they are just messages meant to help you understand the third message. The third message is a warning, not an error. You should still fix it though because you can.

    <iostream.h> is a nonstandard header. The new header is <iostream>. The likely reason you got errors when you switched to <iostream> is because there is an important difference between the two. You need to specify something called the namespace. In a small program, a simple solution is to add using namespace std; below the #includes. In general I prefer another solution, which is to add std:: to the front of all names coming from the standard library (like cout, cin and endl).

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    20
    In general I prefer another solution, which is to add std:: to the front of all names coming from the standard library (like cout, cin and endl).
    Thanks! It worked.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Just to let you know; its a good idea not to run as Administrator on a PC with internet connection if you arent specifically doing admin stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  4. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21