I've been trying to have my program take the text entered by the user, save it as a variable, then create a file with the variable as the name of the file. I was going to post my progress so far to help show what I mean but I lost the file. The format of the file is .txt.
You need to prefix your standard library symbols with std::
Also the function open() requires a const char* rather than a std::string. You can get this using the c_str() member function of the std::string. This should work better:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string text;
std::ofstream file;
std::cout << "Please enter a new file name:\n";
std::cin >> text;
file.open(text.c_str());
}
You can use usingnamespace std; but it is greatly discouraged. It is okay for little noddy programs like this but it should not be used in any serious software.
If you want to call a function you have to declare the function before you use it. And then to call it you simply use its name and append parentheses ().
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
int second() // declare and define function
{
cout << "It worked!!";
}
int main()
{
second(); // call function
}