Problem with fstream

I may be pretty proficient in html but i am a newbie to c++. I am trying to create a program that lets me write basic html webpages.
i want to be able to write to my website by calling file so i decided write:
ofstream file;
file.open ("website.html");
at the top so it could be used globally. The problem is here that on line 6 it gives me an error saying
error: 'file' does not name a type|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

if someone could help me fix my problem or find a way that i can use file with all my functions alternatively that would be greatly appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ofstream file;
file.open ("website.html");



void title  (){
    bool valid=false;
    char choice;
    string title;
  cout << "Do you want a title for your webpage [y]es or [n]o" << endl;
  while (valid==false){
  cin >> choice;
  if (choice=='n'){
    valid=true;
    break;
  }
  if (choice=='y'){
    valid=true;
    cout << "What do you want the title of the webpage to be" << endl;
    cin >> title;
    file << "<title>" << title << "</title>" << endl;
    cout << "Wrote title \"" << title << "\"" << endl;
  }
  if(choice!='n'&&choice!='y'){
    valid==false;
  }
  }
}



void initialize(){
file << "<html>" << endl;
file << "<head>" << endl;
}
void picture(){

}
void text(){

}
void linebreak(){

}
void marquee(){

}

int main(){

  initialize();
  title();


file.close();
}
You can't call statements outside of a function. However, you do have a couple of options. Either you could call open in the first line of main, or you could use its constructor:
 
std::ofstream file ("website.html");


Note that you should try to avoid global variables when possible. You also don't need to explicitly call file.close, either (the destructor will do that for you).
Thanks A lot, You fixed my problem!
Topic archived. No new replies allowed.