Missing something

This is a discussion on Missing something within the C++ Programming forums, part of the General Programming Boards category; I think I have been working on this too long.....in the Main Function area I can't get this to work, ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    Missing something

    I think I have been working on this too long.....in the Main Function area I can't get this to work, I am sure it is probably something simple but I am not seeing it, are you???
    Thanks!!!!

    Code:
     
    #include <iostream.h>
    #include <string.h>
    //----------Node Class-------------//
    class Digit {
         friend class NumberList;
     
    public:
         Digit( const int & );
         int getData() const;
     
    private:
         int data;
         Digit *nextPtr;
     
    };
     
    Digit::Digit( const int &info )
       : data( info ), 
         nextPtr( 0 ) 
    { 
    
    }
     
    int Digit::getData() const 
    { 
         return data;   
    }
    
    //-----------NumberList Class ------------------//
    class NumberList{
     
    public:
         NumberList();
         ~NumberList();
         void insertAtBack( const int & );
         bool isEmpty() const;
         void display() const;
         void HighInt();
         void LowInt();
         void AverageInt();
     
    private:
         Digit *firstPtr;
         Digit *lastPtr;
     
         Digit *getNewNode( const int & ); 
    };
     
    NumberList::NumberList() 
       : firstPtr( 0 ), 
         lastPtr( 0 ) 
    { 
    
    }
     
    NumberList::~NumberList()
    {
         if ( !isEmpty() ) {
              
     
              Digit *currentPtr = firstPtr;
              Digit *tempPtr;
     
              while ( currentPtr != 0 ) 
              {
                   tempPtr = currentPtr;
                   
                   currentPtr = currentPtr->nextPtr;
                   delete tempPtr;
              }
         }
         
    }
     
    void NumberList::insertAtBack( const int &value )
    {
         Digit *newPtr = getNewNode( value );
     
         if ( isEmpty() )
              firstPtr = lastPtr = newPtr;
          else 
         {
              lastPtr->nextPtr = newPtr;
              lastPtr = newPtr; 
         } 
    }
     
    bool NumberList::isEmpty() const 
    { 
         return firstPtr == 0;   
    }
     
    Digit *NumberList::getNewNode( const int &value )
    {
         return new Digit( value ); 
    }
     
    void NumberList::display() const
    {
         if ( isEmpty() )
         {
              cout << "The list is empty\n\n";
              return; 
          }
     
          Digit *currentPtr = firstPtr;
     
         while ( currentPtr != 0 )
         {
              cout << currentPtr->data << " ";
              currentPtr = currentPtr->nextPtr;
        }
         cout << "\n\n";
    }
    
    void NumberList::AverageInt()
    {
         if( isEmpty() )
         {cout << "The list is empty" << endl;}
    
         int length = 0;
    
         Digit *currentPtr = firstPtr;
         int sum = 0;
    
         while(currentPtr != 0)
         {
              length++;
              sum+=currentPtr->data;
              currentPtr = currentPtr->nextPtr;          
         }
         cout << "The average is " << (float)sum/length << endl;
    }
    
    void NumberList::HighInt()
    {
         if( isEmpty() )
         {cout << "The list is empty" << endl;}
    
         Digit *currentPtr = firstPtr;
    
         int high = currentPtr->data;
    
         while(currentPtr != 0)
         {
              if(currentPtr->data > high)
              {high = currentPtr->data;}
              currentPtr = currentPtr->nextPtr;
         }
         cout << "The high is " << high << endl;
    }
    
    void NumberList::LowInt()
    {
         if( isEmpty() )
         {cout << "The list is empty" << endl;}
    
         Digit *currentPtr = firstPtr;
         int low = currentPtr->data;
    
         while(currentPtr != 0)
         {
              if(currentPtr->data < low)
              {low = currentPtr->data;}
              currentPtr = currentPtr->nextPtr;
         }
         cout << "The low is " << low << endl;
    }
     
    //---------------main function---------------//
    void main()
    {
     int input;        
     int getInput(int *input);
    
         int n = 0;
    
        while(n != 9999)
        {
            cout << "Enter #" << n << " => ";
            cin >> input[i];
            if (input[i] == -1) break;
            i++;
        }
    
        return (n - 1);
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What is going wrong.
    Just looking at the main, I see that you need 'int main'. That is standard, and you also return an int. And, 'n' is never incremented. Also, you may just want an infinite loop ('while(1) or while(true) or for(;' ), instead of imposing an arbitrary limit (which is infinite anyway since 'n; never changes).

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I can't get this to work
    Posting a bunch of code and a vague "it doesn't work!" comment won't get you very good results. Describe why it doesn't work, and what you expect it to do that it isn't doing. What is it doing that's so wrong?
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    I did say in the main() function is where the problem is. I am getting a few errors. I altered the code some.......

    Code:
     
    int main()
    {
           
     int getInput(int *input);
    
         int n = 0;
    
        while(n != 9999)
        {
            cout << "Enter #" << n << " => ";
            cin >> input[n];
            if (input[n] == -1) break;
            n++;
        }
    
         (n - 1);
         return 0;
    }
    I am getting 2 errors, unidentified identifier 'input'.
    Any ideas? Thanks!!

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    Quote Originally Posted by campermama
    Code:
           
     int getInput(int *input);
    I am getting 2 errors, unidentified identifier 'input'.
    Any ideas? Thanks!!
    Am i even qualified to answer this?

    You might want to think about adding some arguments to those functions as well so you at least know what you need to pass too them when you get around to calling them.
    Last edited by Meloshski; 07-11-2004 at 11:39 PM.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Describe exactly what you want to accomplish. From the code you posted I could easily take it two different directions both which will be very fugly.

    So far:
    Code:
    int main()  // Much better
    {
           
     int getInput(int *input); //Here you are prototyping a function inside of main().
    // IIRC this is legal but probably not what you want and is the source of your confusion
    
         int n = 0;
    
        while(n != 9999)
        {
            cout << "Enter #" << n << " => ";
            cin >> input[n];
            if (input[n] == -1) break;
            n++;
        }
    
         (n - 1); //Completly useless instruction
         return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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