im looking to brush up on my C++ coding as its been a quite a few months since ive done anything with it and ive run into a snag with my coding and have spent hours with 2 Compilers (Bloodshed Dev C++ (where code was originally written)as well as Eclipse IDE) to no avail.

#include <iostream>
#include <ctime>
using namespace std;

int rn[20];
int counter3;
int *largestrandom;
int tempNum, counter1, counter2;
int main ()
{//int-main start

srand((unsigned)time(0));//random seed

    for (counter3 = 0;counter3 < 20; counter3++)
    {//random numbers start
    rn[counter3] = rand() % 100 + 1;

    }//random numbers end
    //first loop lets you go by rows and sorts
   for (int counter1=0; counter1<20; counter1++){

           //second loop allows you to go left to right and sort
           for(int counter2=0; counter2<20; counter2++){

        //see if the number on the left is bigger than on the right
                   if(rn[counter2]>rn[counter2+1]){

        //if it is then swap them using a temporary holder
                    tempNum=rn[counter2];
                    rn[counter2]=rn[counter2+1];
                    rn[counter2+1]=tempNum;
                    }
            }
     }
for (counter3 = 0;counter3 < 20; counter3++)
    {//random numbers start

    cout << rn[counter3] << endl;
    }

    cout << "\nand the largest number is " << rn[19];
    tempNum = rn[19];
    largestrandom = &tempNum;
    cout << " Stored at location " << largestrandom << endl ;

system ("pause")
return 0;
}//int-main end

i seem to keep getting these errors:

Function 'system' could not be resolved (i only got this one from eclipse IDE using mini GW C++ compiler plugin/addon)
'rand' was not declared in this scope   
'srand' was not declared in this scope  
Function 'srand' could not be resolved  
Function 'rand' could not be resolved

The weird part about this is that this code worked about 3-4 months ago the last time i ran it. and even when i substituted a simple random number output program instead the compilers still seemed to give me the same errors. Any help would be appreciated and i apologize in advance as i know this is going to be a very simple mistake on my part.

Recommended Answers

All 3 Replies

First off, just to be pedantic about it, Dev-C++ and Eclipse are not themselves compilers; they are development environments which call (presumably) the GCC compiler (which to be even more pedantic, is not actually a compiler either, just a driver for the compiler and linker). I know it sounds picayune to make such distinctions, but there's a reason I bring it up.

You see, development of the original Bloodshed Dev-C++ stopped about ten years ago, and as a result, the IDE (and the default compiler) is sort of frozen in time from around 2005. As a result, the many refinements and changes that have been made to GCC have not been added to it, and compiler that it defaults to is now quite out of date.

However, if you are using Eclipse (or the Orwell Dev-C++ fork, or Code::Blocks, or any other up-to-date IDE), you'll have the latest version of GCC bundled with (as of when you installed it, at least), with the latest and greatest default settings, many of which have to do with code validation. Furthermore, each IDE will have different default switches set when they call GCC, including in most cases -Wall (show all warnings), which Bloodshed Dev-C++ didn't set by default. So, if you are using a newer version of GCC, with a different set of switches set, you could end up with some surprises when compiling code that had seemed fine in the past.

Specifically, the checking for function prototypes has been tightened up significantly. The prototypes for the functions which you are getting errors for are mostly in <cstdlib>, which you haven't #includeed, hence the warnings.

BTW, if you are using the newer IDEs, they have almost universally fixed the bug in Dev-C++ that caused the output window to close when the program exits; so, if you are using a newer IDE, you don't actually need the system() call in any case. Eliminating that annoyance alone is reason enough to upgrade, I would say.

I have found Orwell Dev-C++ to work well for C++11 and before ... it is easy for students to use ... can easily compile a single (student type problem) file (withOUT needing to make a project) ... and run to see the output ... without having to add code to keep the window open when the program is done.

im looking to brush up on my C++ coding

Take a look at these edits ...
1. functions used
2. code reuse made easy via using an include file for the bubble sort
3. comment as you go ...
.
.
.

// test_bsort_ary.cpp //

#include <iostream>
#include <cstdlib> // re, rand
#include <ctime> // re. srand


#include "bsort_ary_vec.h"


//using namespace std;


// can easily adjust array size here as desired ...
// (and then recompile) //
const size_t SIZE_RAN = 20;


// can show arrays of any type ...
template < typename T >
void showAry( const T* ary, size_t size, size_t numOnLine = 10 )
{
    using std::cout; using std::endl;

    for( size_t i = 0; i < size; ++ i )
    {
        if( i % numOnLine == 0 ) cout << endl;
        cout << ary[i] << ' ';
    }
    cout << endl;
}



int main ()
{
    using std::cout; using std::endl;

    // random seed
    srand( (unsigned)time(0) ); 

    // reserve space to hold numbers
    unsigned ranNums[SIZE_RAN];

    // fill ary with random numbers
    for( size_t i = 0; i < SIZE_RAN; ++ i )
    {// get numbers in range 1..100
        ranNums[i] = rand() % 100 + 1; 
    }

    cout << "Before sorting the numbers are: ";
    showAry( ranNums, SIZE_RAN );

    // default is ascending arder
    dw_bsort::bsort( ranNums, SIZE_RAN  );

    cout << "\nAfter sorting the numbers are: ";
    showAry( ranNums, SIZE_RAN );

    cout << "\nand the largest number is " << ranNums[SIZE_RAN-1];

    unsigned* pMaxInt = &ranNums[SIZE_RAN-1];

    cout << " stored at address " << pMaxInt << endl ;
}

Now the include file: name this file: bsort_ary_vec.h

// bsort_ary_vec.h // this version 2014-06-02 //


/*
    BEGINNING COMPUTER PROGRAMMING (using HLA and Python 3 and C++)
    http://developers-heaven.net/forum/index.php/topic,46.0.html
*/


#ifndef BSORT_ARY_VEC_H
#define BSORT_ARY_VEC_H


#include <vector>
#include <string>


namespace dw_bsort // begin namespace dw_bsort ...
{
    #define swaptrue \
        tmp = ary[j-1]; \
        ary[j-1] = ary[j]; \
        ary[j] = tmp; \
        swap = true

    #define bsort_up \
            do \
            { \
                swap = false; \
                \
                for( size_t j = 1; j < size; ++j ) \
                { \
                    if( ary[j] < ary[j-1] ) \
                    { swaptrue; } \
                } \
                --size; \
            }while( swap )

    #define bsort_down \
            do \
            { \
                swap = false; \
                \
                for( size_t j = 1; j < size; ++j ) \
                { \
                    if( ary[j-1] < ary[j] ) \
                    { swaptrue; } \
                } \
                --size; \
            }while( swap )


    template < typename T >
    void bsort( T ary[], size_t size, bool ascending = true )
    {
        bool swap;
        T tmp;
        if( ascending ) bsort_up;
        else bsort_down;
    }

    // vector version of above ...
    template < typename T >
    void bsort( std::vector< T >& ary, bool ascending = true )
    {
        bool swap;
        T tmp;
        size_t size = ary.size();
        if( ascending ) bsort_up;
        else bsort_down;
    }

} // end namespace dw_bsort //

#endif

Like it was said by others, the solution is to add an include of <cstdlib> where the rand / srand and system functions are found.

I would just add that the reason this might have worked on another / older compiler is because sometimes standard library headers include other ones. It is very much possible that in a previous version of GCC, the <ctime> or <iostream> header included the <cstdlib> header. Strictly-speaking, none of the standard headers are required to include any other standard header, and so, if you have code that implicitly assumes that one standard header will include another is potentially going to be invalid on another compiler / platform.

In the old days, there used to be a lot more inter-inclusion of standard headers (especially because early implementations of C++ standard libraries were implemented in terms of the C standard libraries). Over the years, the standard library implementations have gotten more "lean", after people complained repeatedly about standard headers using this "including-the-entire-world" style (which creates code pollution and long compilation times, which people were very frustrated about).

If you were used to writing C++ code with an older compiler, then you might be acustomed to this older behaviour, and it will take some time getting used to to update your coding habits.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.