Click to See Complete Forum and Search --> : Spoiled by MS VC 6, help!


Cloaca
March 10th, 2002, 08:00 PM
Hi all,

Please see my code below. It compiles and runs beautifully in MSVC++ 6.0 (as a console program), but my teacher uses UNIX, and says it does not compile. Other students have said they've had luck using GCC (I am not familiar with that though). I know that MSVC is supposed to be ANSI compatible, but I think I am missing something, or maybe using functions in a way that isn't 'portable'. I will be thankful for any advice you can provide!
Thanks, Eric (code follows)

// CS501
// Eric
// 2/19/2002
// HW#4
// Exercise 17

// FirstOccurrence
// This program will show the (0 based) index of the first occurrence of the
// largest integer it finds in the input array.
// The input is the InputArray.
// The output is the index described bove.

#include "iostream.h"
#include "stdlib.h" // For random number generation.
#include "time.h" // For using time to get an integer to seed rand with.

#define LENGTH 15

int main(void)
{
int InputArray[LENGTH] = {0};

// Fill the array with random integers...
srand((unsigned)(time(NULL)));
for(int c = 0; c < LENGTH; c++)
InputArray[c] = (rand() % 10);

cout << "Input Sequence is: ";
for(int go = 0; go < LENGTH - 1; go++)
cout << InputArray[go] << ", ";
cout << InputArray[go];
cout << "\n\n";

int largest = InputArray[0];
int index = 0;

for(go = 1; go < LENGTH; go++)
{
if(InputArray[go] > largest)
{
largest = InputArray[go];
index = go;
}
}

cout << "The 0 based index of the first occurrence of " << largest << " is " << index;
cout << "\n\n";

return 0;
}




Competent novice programmer.

Paul McKenzie
March 10th, 2002, 11:57 PM
Your teacher didn't say what was wrong with the compilation?

Anyway, here are your errors:

//#include "iostream.h" // Wrong. not standard header
#include <iostream> // Correct
#include "stdlib.h" // For random number generation.
#include "time.h" // For using time to get an integer to seed rand with.
using namespace std; // This is missing.



Here is your next error:

for(int go = 0; go < LENGTH - 1; go++)
cout << InputArray[go] << ", ";
cout << InputArray[go]; // "go" is undefined



In C++, variables declared inside of loop constructs only have scope within the loop. Once you are outside the loop, the variable no longer exists. I know that VC++ and some other compilers don't follow this rule, but they are following the non-standard convention. This is probably why your teacher could not compile your program. Here is the correction:

int go;
for(go = 0; go < LENGTH - 1; go++)
cout << InputArray[go] << ", ";
cout << InputArray[go]; // OK.
//...
// Later on...
// "go" is already declared
for(go = 1; go < LENGTH; go++)
{
if(InputArray[go] > largest)
{
largest = InputArray[go];
index = go;
//...



Make these changes, and possibly the compile will work.

Regards,

Paul McKenzie

Paul McKenzie
March 11th, 2002, 12:06 AM
I also forgot to mention that VC++ has a compiler switch that compiles in strict ANSI mode. Then VC++ would have reported the error to you about the variable declared in the for() loop. Next time, make sure that you go into the project settings (I'm not sure where), and let the compiler know to compile using ANSI standard mode.

The GCC users in your class also have a compiler switch that will compile their code in strict ANSI standard mode. Let your friends using GCC know about this too, and have them look at the command line for GCC to make sure the ANSI mode switch is set. The reason why this is important is that you don't get into the habit of using "VC++ syntax" that only works for VC++, and your friends will start to use GCC syntax that only works for GCC. According to the teacher you and your friends will be wrong, and the teacher would be correct.

Cloaca
March 11th, 2002, 10:00 AM
Hi Paul,

Many thanks and much appreciative! I suspected something about cout, but I never even considered the declaration inside the loop being trouble.
And no, the prof never gave specific info. While he is a great lecturer, he rarely 'has' time to make specific comments about HW :-(.
Anyhow, I will also pass along your compiler switch advice to my classmates.

Thanks again!
Eric

Competent novice programmer.

Paul McKenzie
March 11th, 2002, 12:25 PM
Here's the info on VC++ compilation in ANSI mode.

a) Go to "Project Settings | C/C++"

b) from the "Category" drop-down, choose "Customize".

c) check the "Disable language extensions" check box.

For a test, recompile your old code (not your changed code) with this switch, and see if you get the error with the for loop.

For GCC, I think the -ansi switch is used, but I'm not sure.

Regards,

Paul McKenzie

NMTop40
March 11th, 2002, 02:39 PM
And next you should be telling him how to use std::vector and ostream_iterator


The best things come to those who rate

Cloaca
March 12th, 2002, 10:49 AM
Hi there,

Thanks for the VC project setting tip Paul. It indeed detected the int declared improperly in the loop. But after I made all the fixes and tried to compile 'without language extensions', I had like 120 compiler errors.
Playing around, I found it didn't like the <iostream> for some reason. The first line in the error output brought up some file named 'utility' and it complained about a missing ; symbol.
Here is the first line from the error output:
e:\program files\microsoft visual studio\vc98\include\utility(81) : error C2146: syntax error : missing ';' before identifier 'iterator_category'
e:\program files\microsoft visual studio\vc98\include\utility(84) : see reference to class template instantiation 'std::iterator_traits<_It>' being compiled

And here is the section of code in 'utility' that it complained about:
template<class _It>
struct iterator_traits {
typedef _It::iterator_category iterator_category;
typedef _It::value_type value_type;
typedef _It::distance_type distance_type;
};

Going back to project settings and unchecking (allowing) language extensions made the thing compile fine.
Do I also need to explicitly include some libraries in the compiler settings if I use 'ANSI' mode and use <iostream>?
Thanks again for your help!

Best
Eric

Competent novice programmer.

NMTop40
March 12th, 2002, 11:02 AM
those errors are normally caused by errors in your code, not errors in STL.

What happens with templates is that they are expanded inline, i.e. in your code.

The chances are that you are either using a template of an inappropriate type or a template of a type that is not defined at all.


The best things come to those who rate

Cloaca
March 12th, 2002, 11:53 AM
Hi there,

Here is the complete program as it is now:


// CS501
// Eric
// 2/19/2002
// HW#4
// Exercise 14

// TwoBiggies
// This program will calculate the two biggest numbers in an sequence.
// The input is the InputArray.
// The outputs are Biggest and SecondBiggest

#include <iostream>

using namespace std;

#define LENGTH 4

int main(void)
{
int InputArray[LENGTH] = {10, 9, 1, 2};
int go;

cout << "Input Sequence is: ";
for(go = 0; go < LENGTH - 1; go++)
cout << InputArray[go] << ", ";
cout << InputArray[go];
cout << "\n\n";

int Biggest = InputArray[0];
int SecondBiggest = InputArray[1];

for(go = 1; go < LENGTH; go++)
{
if(Biggest < InputArray[go])
{
SecondBiggest = Biggest;
Biggest = InputArray[go];
}
else
if(SecondBiggest < InputArray[go])
SecondBiggest = InputArray[go];
}

cout << "The biggest number is: " << Biggest << "\n";
cout << "The second biggest number is: " << SecondBiggest << "\n\n\n";

return 0;
}




I found that if I have a program as simple as the following I get the _same_ errors:


#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello World!";
return 0;
}




Now, this simple program (as well as the first one) compiles no problem with 'language extensions enabled (checkbox unchecked). Could something be corrupted in my compiler or settings?

Thanks for your tips!

Best,
Eric

Competent novice programmer.

NMTop40
March 12th, 2002, 01:03 PM
your code worked fine for me, even with no extensions. Maybe your STL is not up-to-date. Get patches from http://www.dinkumware.com if that is the case.

But now perhaps you should be introduced to the vector class. Ok, maybe this is overdoing it but


#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include "time.h"
#include "stdio.h" // for rand()

using namespace std;
#define LENGTH 15
#define RANDMAX 10
// functor to return integer from 0 to Max-1
// but we use this trick for our transform
template< int Max >
struct RandMax : unary_function< int, int >
{
operator()( const int & )
{
return rand() % Max;
}
};
//
bool Bigger( const int &x, const int &y )
{
return x > y;
}
//
//
void main()
{
using namespace std;
srand( time( NULL ) );
vector<int> InputArray(LENGTH);
transform( InputArray.begin(), InputArray.end(),
InputArray.begin(), RandMax< RANDMAX >() );

copy( InputArray.begin(), InputArray.end() - 1,
ostream_iterator< int >( cout, ", " ) );

cout << InputArray[LENGTH-1] << endl << endl;

partial_sort( InputArray.begin(), InputArray.begin()+2,
InputArray.end(), Bigger );

cout << "Biggest element " << InputArray[0] << endl <<
"Second biggest element " << InputArray[1] << endl;
}





The best things come to those who rate

Cloaca
March 12th, 2002, 01:20 PM
OK, cool!

There looks like lots of stuff for me to chew on here. I am aware of templates, but I have never (had to) use(d) them.
I appreciate you taking the time to include the sample code. I will also look into the Dinkum SW, but it looks like it'll be $90, and I'm on a budget so I'll have to be careful. :-)
Maybe my STL files are screwed up.
Thanks again to you and Paul!

Best,
Eric

Competent novice programmer.

Paul McKenzie
March 12th, 2002, 01:23 PM
This is an STL problem. Seems like the VC++ version of the STL was tested only when extensions are turned on. NMTop40 got it to compile with the latest update from dimkumware, so maybe you should check that out. For now, I guess your option is to leave extensions on and be careful.

Also, (to add to NMTop40's code), the usage of max_element() and distance() would have eliminated the need for your loop.

#include <iostream>
#include <algorithm>
//...
#define LENGTH 4
using namespace std;
//...
int main(void)
{
int InputArray[LENGTH] = {10, 12, 1, 2};
int *pLargest = max_element(InputArray, InputArray + LENGTH);
int nDistance = distance(InputArray, pLargest);
cout << "The 0 based index of the first occurrence of " << *pLargest << " is " << nDistance << endl;
return 0;
}



Read up on <algorithm> in the standard library for an understanding.

Regards,

Paul McKenzie

Paul McKenzie
March 12th, 2002, 04:20 PM
You could always get the free STLPort version of STL. It should work fine -- all you need to do is run a batch program to compile all the libs to set it up. You can get it at http://www.stlport.org

Regards,

Paul McKenzie

NMTop40
March 12th, 2002, 05:32 PM
I think it's only $90 to download the whole of STL but the patches are free.


The best things come to those who rate

Cloaca
March 16th, 2002, 03:25 PM
Hi guys!

Thanks for all your help, Paul and Neil (I think that's right, NMTop40). My intial problem is licked and I have learned lots of good stuff.
I hope I can one day help others as much, too.

Best regards,
Eric


Competent novice programmer.

NMTop40
March 16th, 2002, 03:58 PM
Hopefully you can spread the word about using standard C++ libraries.



The best things come to those who rate

popmaker
March 17th, 2002, 12:16 PM
I also suggest you get yourself gcc. It runs on
most platforms. If you don't like it, you can
least use it for comparison.