I'm trying to save everything to a .txt file with a new line after every string, but I can't get it to work for me. I tried using <<endl; and "\n" but it all gets written in a single line. So if I enter "John" for first , "Smith" for last , and "Casablanca" for film , I get "JohnSmithCasablanca". I've gone through all my notes and my textbook. Am I doing something wrong?

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

int main()
{
    string first, last, film;
    
    cout<<"Enter your first name and last name, then film(one word). ";
    cin>>first;
    cin>>last;
    cin>>film;

    ofstream outFile ("Customers.txt");
    outFile<<film<<endl;
    outFile<<first<<endl;
    outFile<<last<<endl;
    outFile.close();

    return 0;
}

My friend I did exactly same thing with you (just your code) and it perfectly works(in os Ubuntu 10.10 dev Geany)
you should check your compiler probably. Good luck.

It seems to work for me in 2 compilers. Dev C++ and Visual Studio C++. Try a new compiler, VSC++ is the generally the chosen compiler for most people, and its industry standard i believe, so try using that. Its a good idea to get used to it anyway.

Thanks a bunch.

It seems to work for me in 2 compilers. Dev C++ and Visual Studio C++. Try a new compiler, VSC++ is the generally the chosen compiler for most people, and its industry standard i believe, so try using that. Its a good idea to get used to it anyway.

You're absolutely right. I installed the Visual Studio copy that the University gave me and it works perfectly. Thank you so much for your help. NetBeans boooo!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.