Incrementation and user input problem

here is the write function of my program. Each time you write a file it add 1 to a number in another file so that the nextime you want to write a file it doesn't overwrite your last file. What I want to do is this i want to be able to let the user input how many lines the data file will contain. I've tried using For(~~~~~~~~~~) and the ++ operatior but the compiler doesn't like that. I am using XP Home and the compiler is Dev C++. Thanks for your help



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
void write()
{
     char fileNO[4];
     char data[3];
     int dat;
     ifstream infile;
     infile.open("data.dat", ios::in);
     infile.get(fileNO,4);
     infile.ignore(50);
     dat = atoi(fileNO);
     cout << "File# = " << fileNO << '\n';
     infile.close();
     cin.get();
     

     
     float close;
     int x;
     char line1[100];
     char line2[100];
     char line3[100];
     char line4[100];
     char line5[100];
     char filename[10];
     int random;
     ofstream outfile;
     cout << "Enter data to be written: \n";

     cin.get(line1, 100); 
     cin.ignore(101,'\n');
     cin.get(line2, 100); 
     cin.ignore(101,'\n');
     cin.get(line3, 100); 
     cin.ignore(101,'\n');
     cin.get(line4, 100); 
     cin.ignore(101,'\n');
     cin.get(line5, 100); 
     cin.ignore(101,'\n');

     random = dat + 1;
     cout << "writing file# "<< random << '\n';
     itoa(random, filename, 20);
     


     outfile.open( filename ,ios::out);
//make sure file is opened
     if (outfile)
     {
                 outfile << line1 << endl;
                 outfile << line2 << endl;
                 outfile << line3 << endl;
                 outfile << line4 << endl;
                 outfile << line5 << endl;

                 cout << "Write successfull.\n";
                 outfile.close();
                 cin.ignore(1000, '\n');
                 
                 
                 int file;
                 file = atoi(fileNO) + 1;
                 ofstream outfile;
                 outfile.open( "data.dat" ,ios::out);
                 outfile << file << endl;
                 cout << "Data file wrtten successfully.\n";
                 outfile.close();
                 cin.ignore();
                 
                 
                 }
     else
     { 
         cout << "Error writing file.\n";
         cin.get();
         cin.get();
         } 
         }
Last edited on
Topic archived. No new replies allowed.