CS3364 - Design and analysis of Algorithms, Summer I / 2004

Notes on C++

If you try to submit your program you might run into some strange errors, that are due do the incompatibility between Visual C++ and gcc

main function

The main function has to return an int. void is no longer allowed:

int main
{
  ...
  return 0;
}

Declaration inside for

Do not use declarations inside a for loop. The scope where the variabel is valid has changed. Instead of

for (int i=0;...) {
}

use
int i;
for (i=0;....)

The only exception is when you use your variable only inside this loop. But then you still have to be careful not to use the same variable again

Reading from standard-in / a text file

To read from a textfile and standard-in with the same methods, use something like the following code:
...
#define TEXTINPUT
...
int main() 
{
  ...
#ifdef TEXTINPUT
  istream *inp = new ifstream("my_input.txt");
#else
  istream *inp = &cin;
#endif
  *inp >> s;
  ...
}
And comment out the #define line before submission.