C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 22nd, 2007, 07:43 AM
#include <Hspd> #include <Hspd> is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 30 #include <Hspd> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 48 m 4 sec
Reputation Power: 11
Someone please help! New to C++

My code, basically is a replica from my book and it keeps giving me an error I'll include the error as commentary in the code box. Please help.

Code:
#include <iostream>
#include <iomanip>
using namespace std;

void bubble( int [], const int, int (*)( int, int ) );
int ascending (int, int);
int descending(int, int);

int main()
{
    const int arraySize = 10;
    int array[arraySize] = {3, 6, 2, 1, 4, 7, 23, 26, 31, 54};
    int order,
        counter;
        
    cout << "Enter 1 to sort in ascending order, \n"
         << "Enter 2 to sort in descending order, \n";
    cin >> order;
    cout << "\nData items in original order\n";
    
    for (counter = 0; counter < arraySize; counter++)
        cout << setw(4) << array[counter];
        
    if (order == 1){
       bubble(array, arraySize, ascending);
       cout << "\nData in ascending order\n";
    } 
    else{
       bubble(array, arraySize, descending);
       cout << "\nData in descending order\n";
       
    for(counter = 0; counter < arraySize; counter++)
       cout << setw(4) << array[counter];
       
    cout << endl;
    system("PAUSE");
    return 0;
}

void bubble( int array[], const int SIZE, 
             int (*order)( int, int ) )
{                                                           //a function definition is not allowed here before '{' token.
      void swap(int*, int*);
      for (int pass = 1; pass < SIZE; pass++)
         
         for (int count = 0; count < SIZE; count ++)
             
             if((*order)(array[count], array[count + 1]))
                 swap(&array[count], &array[count + 1]);
}

void swap(int *element1Ptr, int *element2Ptr)
{                                   //a function definition is not allowed here before '{' token.
     int hold;
     
     hold = *element1Ptr;
     *element1Ptr = *element2Ptr;
     *element2Ptr = hold;
}

int ascending(int a, int b)
{                                       //a function definition is not allowed here before '{' token.
    return = b < a;
}

int descending(int a, int b)
{                                      //a function definition is not allowed here before '{' token.
    return = b > a;
}



WTF?


this is the books code
Code:
// Fig. 5.26: fig05_26.cpp
// Multipurpose sorting program using function pointers
#include <iostream>
#include <iomanip>
using namespace std;

void bubble( int [], const int, int (*)( int, int ) );
int ascending( int, int );
int descending( int, int );

int main()
{
   const int arraySize = 10;
   int order, 
       counter,
       a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

   cout << "Enter 1 to sort in ascending order,\n" 
        << "Enter 2 to sort in descending order: ";
   cin >> order;
   cout << "\nData items in original order\n";
   
   for ( counter = 0; counter < arraySize; counter++ )
      cout << setw( 4 ) << a[ counter ];

   if ( order == 1 ) {
      bubble( a, arraySize, ascending );
      cout << "\nData items in ascending order\n";
   }
   else {
      bubble( a, arraySize, descending );
      cout << "\nData items in descending order\n";
   }

   for ( counter = 0; counter < arraySize; counter++ )
      cout << setw( 4 ) << a[ counter ];

   cout << endl;
   system("PAUSE");
   return 0;
}

void bubble( int work[], const int size, 
             int (*compare)( int, int ) )
{
   void swap( int *, int * );

   for ( int pass = 1; pass < size; pass++ )

      for ( int count = 0; count < size - 1; count++ )

         if ( (*compare)( work[ count ], work[ count + 1 ] ) )
            swap( &work[ count ], &work[ count + 1 ] );
}

void swap( int *element1Ptr, int *element2Ptr )
{
   int temp;

   temp = *element1Ptr;
   *element1Ptr = *element2Ptr;
   *element2Ptr = temp;
}

int ascending( int a, int b )
{
   return b < a;   // swap if b is less than a
}

int descending( int a, int b )
{
   return b > a;   // swap if b is greater than a
}

Reply With Quote
  #2  
Old November 22nd, 2007, 07:57 AM
#include <Hspd> #include <Hspd> is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 30 #include <Hspd> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 48 m 4 sec
Reputation Power: 11
wow

crap, I found two errors in which i fixed, and it eliminated the problem... one was the terminating brace for the else, and the other was the return statement in my two last functions.

sorry for the bother, thanks everyone.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Someone please help! New to C++


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap