BASIC ERRORS

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();
}
Last edited on
You never declared filein.
Last edited on
Sorry I wrote the wrong, It must be:"fileout". But It still error!!! help me!!
Could you post your errors here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# include<iostream>
# include<fstream> 
# include<iomanip> 
using namespace std; 
const int 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.
Last edited on
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.
by press backspace

Do you mean by pressing enter with no input?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# include<iostream>
# include<fstream>
# include<iomanip>
# include<string.h>
using namespace std;

const int LEN= 25;

int main()
{
    const char *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;
}
Last edited on
Topic archived. No new replies allowed.