system("cls");
char filnamn[20];
char text[100];
char lookslike;
cout<<"\nThen let's move on! "<<endl;
cout<<"\nEnter the file name you want to create: ";
cin.getline(filnamn, 20);
ofstream f1(filnamn);
cout<<"Enter the text you want to include the file: "<<endl;
cin.getline(text, 100);
f1 << text;
f1.close();
cout<<"You wanna see how the file you just created looks like? J/N";
cin>>lookslike;
if(lookslike=='J' && lookslike=='j'){
ifstream f2(filnamn);
cout<<"Detta står i filen\n"<<endl;
cout<<f2.rdbuf();
f2.close();
}
elseif(lookslike!='J' && lookslike != 'j'){
return 0;
}
I got more code above this, and donät want to copy all. I got, #include <fstream><conio.h><windows.h><iostream><string> :)
Please be a little more specific about what is not working.
If you get compiler errors, please include them, if you get unexpected behaviour, please tell us which behaviour you expect and which behaviour you actually get.
cout<<"\nEnter the file name you want to create: ";
cout<<"Enter the text you want to include the file: "<<endl;
Will appear exacly after each other, and for some reason forget the file I shuld write in between these two. After the "couts" i can enter som text. It dosn't matter what i wrote in there. No file will be created eather. Once again sorry for my bad explanation :) ty
#include <string>char filnamn[20];
char text[100];
char lookslike;std::string filnamn, text, lookslike;
cout<<"\nThen let's move on! "<<endl;
cout<<"\nEnter the file name you want to create: ";
cin >> filnamn;
ofstream f1(filnamn);
cout<<"Enter the text you want to include the file: "<<endl;
cin >> text;
std::string is the preferred way to handle text-strings in C++, rather than char-arrays (C-style-strings).
They also allow for easy use of std::cin as shown above.
system("cls");
std::string filnamn, text;
char lookslike;
cout<<"\nThen let's move on! "<<endl;
cout<<"\nEnter the file name you want to create: ";
cin >> filnamn;
ofstream f1(filnamn);
cout<<"Enter the text you want to include the file: "<<endl;
cin >> test;
f1 << text;
f1.close();
cout<<"You wanna see how the file you just created looks like? J/N";
cin>>lookslike;
if(lookslike=='J' || lookslike=='j'){ //you mean || (OR) here, not && (AND)
ifstream f2(filnamn);
cout<<"Detta står i filen\n"<<endl;
cout<<f2.rdbuf();
f2.close();
}
elseif(lookslike!='J' || lookslike != 'j'){ //you mean || (OR) here, not && (AND)
return 0;
}
If that throws you compiler errors, please copy-paste them here.
Ye now it works! :) Ty! Yeah some misstakes there (||) .
But, if i do not use cin.getline i can't wrote as long text as i want.
string = only one word. But yes, the function works fine now!