HELP PROJECT .. SORRY FOR SPAM ..

sadly only my option 3 works , when i try to wrtie they put stack around Datetime is corrupted .
and if i read it gives me an error .. and puts information required when my textfile there is information
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  #include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
int write(void);
int read(void);

using namespace std;

int main()
{
      int choice;
      char ch;

	  cout << " Are you a human ? " ;
	  cin >> ch;
      while (ch == 'y' || ch == 'Y')
	  {
          cout<<endl;
          cout<<"Please choose from the following: \n";
          cout<<"\t1. Read Data. \n";
          cout<<"\t2. Write Data. \n";
          cout<<"\t3. Quit. \n";
          cout<<endl;
          cout<<"Your choice: ";
          cin>>choice;
      
          {
              if (choice == 1)
                     read();
                  
             else if (choice == 2)
                     write();
                   
			 else 
                   cout<<"\tInvalid entry!"<<endl;
           }
           cout<<"\nWould you like to try again (y/n): ";
           cin>>ch;

     }
	  while(ch == 'y' || ch == 'Y');

      system("PAUSE");
      return 0;
}




int write(void)
{
    ofstream outfile;
    char firstname[4],
         lastname[4],
         contact[8],
         Datetime[8];
    
	
    outfile.open("textfile2.txt.txt",ios::app);

    cout<<"Enter your First Name: ";
    cin>>firstname;
    cin.ignore();
    cout<<"Enter your Last Name: ";
    cin>>lastname;
    cin.ignore();
    cout<<"Enter your Contact: ";
    cin>>contact;
    cin.ignore();
    cout<<"Enter your Date and Time: ";
    cin>>Datetime;
    cin.ignore();
   

    outfile<<firstname<<setw(2)<<lastname<<setw(2)<<contact<<setw(2)<<Datetime<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return 0;
}


int read(void)
{
    ifstream input;
    input.open("textfile2.txt.txt",ios::in);
	{
    char firstname[4],
         lastname[4],
         contact[8],
         Datetime[8];

    
    while(!input.eof())
        input>>firstname>>lastname>>contact>>Datetime;
        cout<<"Information Required. "<<endl;
        cout<<firstname<<setw(1)<<lastname<<endl;
        cout<<contact<<endl;
        cout<<Datetime<<endl;
  
        cout<<endl;
    }
    cout<<"Reading Completed. "<<endl;
    input.close();
    return 0;
}

What are you entering for date and time?

Are you sure the file is opening?
http://www.cplusplus.com/reference/fstream/ifstream/is_open/


It's better not to loop on EOF. There's some info on handling files here.
http://www.cplusplus.com/doc/tutorial/files/
http://www.umich.edu/~eecs381/handouts/filestreams.pdf
Last edited on
Unhandled exception at 0x0f8429ae (msvcp100d.dll) in Project EYE.exe: 0xC0000005: Access violation reading location 0x00000020.


i get this error sorry i'm a first year
What the hell is wrong with your formatting in main? Your other functions look fine (formatting wise), but main is just a mess and damn hard to even read.

If all you're going to do with functions is return 0, just make them void. You're not doing anything with the return value, and the return value has no purpose.

Where is Datetime defined? I don't see that anywhere. You can't use stuff without having it defined somewhere.

Nevermind I see it now. It's best if you stick to consistent naming schemes for your variables. Don't have one just randomly uppercased.

Change Datetime to be std::string. I'd change all those char arrays to std::string.
Last edited on
Where is Datetime defined?


They've got it defined here.

1
2
3
4
   char firstname[4],
         lastname[4],
         contact[8],
         Datetime[8];
What format are you entering the for datetime? If you're including a space, such as "mm/dd/yy hh:mm:ss". Your input at line 95 is going to terminate at the space. Also, 8 characters is too short to hold a full date/time. Your other fields are also very short.

As ResidentiBiscuit suggested, I would recommend change this variables to use std::string. That way you don;t have to worry about the length of data entered by the user.
Topic archived. No new replies allowed.