2

I am using Dev C++ to write to a text file using ofstream but it does not work, I have made a text file in dev c++and saved it and on another source file I wrote the following code:

  #include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main(){

srand(time(0));
ofstream out(“hello.txt”);

for(int i=0;i<100;i++)
          out << rand()%1000 << “ “;
out.close();


return 0;
}

However, when I try to compile this code I get an error and it highlights the following in red:

ofstream out(“hello.txt”);

It says hello undeclared.

The tutorials that I am following are from youtube and the programmer is using a Linux operating system, he is using g++, will the code still be the same on all operating systems? because I am using windows 7.

mamta rani
  • 133
  • 1
  • 2
  • 7

1 Answers1

0

However, when I try to compile this code I get an error and it highlights the following in red:

Your quotes are "smart quotes". See how they're all bendy (“”), instead of straight ("")?

Use normal, straight quotation marks.

Do not program in Word. You may also have to change a Windows setting as I vaguely recall that some keyboard drivers have a smart quotes mode (which boggles the mind).


The tutorials that I am following are from youtube and the programmer is using a Linux operating system, he is using g++, will the code still be the same on all operating systems? because I am using windows 7

For standard code like this yes, absolutely, by design.

When you start playing with OS-specific functionality (i.e. anything from POSIX or the Windows API) then it gets more complex, but you're not there yet.

And I can't recommend enough that you put down your "tutorials" and get a real C++ book.

(It may even be that the smart quotes came from the tutorial website itself! If so, you should inform the author immediately.)

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 3
    dev c++ isn't a good choice either :P – paul23 Jan 24 '13 at 19:13
  • Thank you very much, I dont understand how it is possible for you to realise such a small thing so fast and from that, tell me it was done in word, amazing... I was following the tutorial away from home so I decided to use word to save my code into. @Paul, what is a better compiler? – mamta rani Jan 24 '13 at 19:23
  • @mamtarani: It's because I am a magical person. – Lightness Races in Orbit Jan 24 '13 at 19:44
  • @mamtarani: Dev-C++ is an IDE, not a compiler; the compiler you're using is MinGW GCC which is perfectly acceptable. However, Dev-C++ as an IDE hasn't been updated in some time so people like to get on your horse about it. _Code::Blocks_ is oft suggested as a substitute. – Lightness Races in Orbit Jan 24 '13 at 19:44
  • 2
    To keep things straight - there IS a currently developed branch of Dev-C++, containing G++ 4.7 and newer libraries. I *still* wouldn't recommend it, and just download Visual C++ Express Edition. – Bartek Banachewicz Jan 24 '13 at 19:48
  • Switching compilers _as well_ seems a little extreme. – Lightness Races in Orbit Jan 24 '13 at 19:49