#includeusing namespace std; int main(){ ofstream file("output.txt"); //create output file stream if(file.is_open()){ //check if file is opened successfully file << "Hello, world!" << endl; //write to the file file.close(); //close the file stream } else{ cout << "Unable to open file"; //display error message } return 0; }
#includeIn this example, the program displays a prompt to the user ("Enter your name: "). The user is prompted to enter their name. The program reads the user's input using std::cin, and then uses the "<<" operator to display a personalized greeting to the user. The OutStream features are provided by the Standard Library in C++ and do not require any additional package library.using namespace std; int main(){ cout << "Enter your name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; return 0; }