C++ ofstream not working

I am struggling with my code. I need the program to accept inputs and write them back into a text file. Here is my current code.
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
#include <iostream>
#include <string>
#include <sstream>
#include <istream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <conio.h>
#include <TCHAR.h>

using namespace std;
vector<int> operands;
vector<string> actionOperator; 

int _tmain(int argc, char *argv[])
{
    ofstream taxfile ("Tax.txt);
	{
		string CoE;
		cout << "Press 1 for Customer, 2 for Employee, and 3 for End ";
		getline(cin, CoE);
		if (CoE != "3")
		{
			if (CoE == "2")
			{
				cout << "Please enter current employee password ";
				string EP;
				getline(cin, EP);
				if (EP == "1234")
				{
					taxfile.open ("Tax.txt", ios::out | ios::trunc);
					cout << "Please enter current Federal Tax";
					string FT;
					getline(cin, FT);
					taxfile << FT; 
					cout << "Please enter current State Tax";
					string ST;
					getline(cin, ST);
					taxfile << ST;
				}
				else end1;       
			}
					else end1;
		}
			else end1;
	}
}
taxfile.close();
endif 
Last edited on
Well you forgot to close your quotes around the file name.
typically you should check the file opened as well, another good check would be to include try/catch.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

int _tmain(int argc, char *argv[])   // <----- if your not going to use args, probably not needed.
{
    ofstream taxFile ("Tax.txt");   //  <---- this is not a function and therefore does not need a 
//  function  body:   No { } needed ... and as already mentioned include closing quotes around 
//  Tax.txt
    
    if ( ! taxFile.is_open())
    {
        cerr << "error opening file"
        exit (1);
    }
    else
    {
	string coE;
	cout << "Press 1 for Customer, 2 for Employee, and 3 for End ";
	getline(cin, coE);
        .... // etc etc
            ....
    }
}

taxfile.close(); //  <----- this should be in function body "main"
#endif     //    <------ end what if?    are you defining something? if so then you may want a ";" at the end or "#" at the start 

additionally you should not use uppercase variables for non-constants.

constants in uppercase: const int ARRAYSIZE=34730;
variables in camel case: string firstName, lastName, thisIsACamelCaseStringVariableSee;
Last edited on
If you're going to do this:

ofstream taxFile ("Tax.txt");

Then I believe you can't do this:

taxfile.open ("Tax.txt", ios::out | ios::trunc);

When you declare your ofstream and specify it a file name right away it will open it in ios::out mode for you, and the second call to open will fail. Since you need ios::trunc as well you should probably do this instead:

1
2
ofstream taxfile;
taxfile.open("Tax.txt", ios::out | ios::trunc);


I also believe you can omit the ios::out mode since you're using an ofstream and ios::out is implied. It worked for me but try it out to make sure.

Topic archived. No new replies allowed.