Hello everybody! I'm a student, I'm come from Viet Nam. And my english skills is so bad.
I've some errors, please help me. My code is below:
# include<stdlib.h>
# include<stdio.h>
# include<conio.h>
# include<iostream>
# include<fstream>
# include<iomanip>
using namespace std;
const int length= 25;
void main()
{
char filename[length],input[length];
cout<<"Please type your file's name:\n";
cin>>setw(length)>>filename;
ofstream fileout("D:\\filename",ios::out);
if(!fileout)
{
cout<<"\nCan't create your file!!!"<<filename<<endl;
exit(1);
}
do
{
cin>>input;
fileout<<input<<' ';
}while((input!="\n")&&(fileout));
fileout.close();
return;
getch();
}
# include<iostream>
# include<fstream>
# include<iomanip>
usingnamespace std;
constint length= 25;
int main()
{
char filename[length],input[length];
cout<<"Please type your file's name:\n";
cin>>setw(length)>>filename;
ofstream fileout("D:\\filename",ios::out);
if(!fileout)
{
cout<<"\nCan't create your file!!!"<<filename<<endl;
return 1;
}
do
{
cin>>input;
fileout<<input<<' ';
}while((input!="\n")&&(fileout));
fileout.close();
return 0;
cin.get();
}
Main should always return an integer. You were trying to return a value with a function that's not supposed to return anything. Tell me if the code above compiles and runs.
Your code can compiles and runs. But it doesn't meet my requirement!
I want my code can allow user type a name for the file. then type the content that they want. Finally user finish their typing by press backspace.
That's all.
# include<iostream>
# include<fstream>
# include<iomanip>
# include<string.h>
usingnamespace std;
constint LEN= 25;
int main()
{
constchar *ROOTPATH = "H:\\"; //change ROOTPATH to your root path
char filename[LEN+1];
char path[LEN+1+strlen(ROOTPATH)];
char input[LEN+1] = " "; //initialize input with a non-empty string
// Get file's name
cout << "Please type your file's name:\n";
cin >> setw(LEN) >> filename;
cin.sync(); //discard unread characters in the buffer
// Build file's path
strcpy(path, ROOTPATH);
strcat(path, filename);
// Create file out
ofstream fileout(path, ios::out);
if(!fileout)
{
cout << "\nCan't create your file!!!" << path << endl;
return 1;
}
// Get input and write to file out
while (*input) //*input means get the value of the first char in input
//(*input) return true if first char is not a null char
//(which means input is not an empty string)
{
cin.getline(input, LEN+1); //use getline not >> operator
if (*input)
fileout << input << ' ';
}
// Close file out
fileout.close();
// Pause the program
cout << "Press enter to close program ";
cin.sync();
cin.get();
return 0;
}