Input/output with files

My only problem is the data is not inputting/outputting through the files.

I tried running these two in the input txt file and the output txt file was empty

****************************
5 6 4 6 31
****************************
string
string
3
4
5
****************************

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	ifstream in_file;
	ofstream out_file;
	in_file.open("D:\in-class-assignments\farmdata.txt");
	out_file.open("D:\in-class-assignments\farmoutput.txt");
	string farm_name, food_item;
	float amount, cost, total,amount_paid , return_amount;


	in_file >> farm_name >> food_item >> amount >> cost >> amount_paid;
	total = cost * amount;
    out_file << amount << " " << food_item << "s @ " << "$" << cost << " each costs $" << total << "\n";
	return_amount = amount_paid - total;
	out_file << "You bought " << amount << " " << food_item << "s at @ $" << cost << " each\n\n";
	
	out_file << left << setw(25) << setfill('*')
		 << "Amount Paid: ";
	out_file << right << " $" << amount_paid << "\n";
	out_file << left << setw(25) << setfill('*')
	     << "Cost: ";
	out_file << right << " $" << total << "\n";
	out_file << left << setw(25) << setfill('*')
		 << "Cash Returned: ";	 
	out_file << right << " $" << return_amount << "\n\n";
	out_file << "Thank you for shopping at " << farm_name;

	in_file.close();
	out_file.close();
	return 0;

}
Last edited on
What is your expected program output?
This is a sample output I did without files, just with cin/cout
Now I am trying to turn the original code into one with files instead of cin/cout

****************************************
What is the name of your farm? fleet farm
What is the type of your food item? carrot
How many carrots do you want? 3
What does each cost? (please start with a $): $.5
3 carrots @ $0.5 each costs $1.5
How much cash did you give me? (dont forget the $): $5
You bought 3 carrots at @ $0.5 each

Amount Paid: ************ $5
Cost: ******************* $1.5
Cash Returned: ********** $3.5

Thank you for shopping at fleet farm
****************************************



This is the code that I am trying to use to output into the file

1
2
3
4
5
6
7
8
9
10
11
12
13
out_file << "You bought " << amount << " " << food_item << "s at @ $" << cost << " each\n\n";
	
	out_file << left << setw(25) << setfill('*')
		 << "Amount Paid: ";
	out_file << right << " $" << amount_paid << "\n";
	out_file << left << setw(25) << setfill('*')
	     << "Cost: ";
	out_file << right << " $" << total << "\n";
	out_file << left << setw(25) << setfill('*')
		 << "Cash Returned: ";	 
	out_file << right << " $" << return_amount << "\n\n";
	out_file << "Thank you for shopping at " << farm_name;



Last edited on
I tried running these two in the input txt file and the output txt file was empty


No, I did not see a problem in your code. Here is my output :
4 6s @ $6 each costs $24
You bought 4 6s at @ $6 each

Amount Paid: ************ $31
Cost: ******************* $24
Cash Returned: ********** $7

Thank you for shopping at 5


Note I have changed the file path :
1
2
in_file.open("farmdata.txt");
out_file.open("farmoutput.txt");
Are you supposed to make files named that for the output, because that is what I did
Also I am running visual studio if that matters
Last edited on
Oh I just had to make a file name that I don't have on my computer for it to work, I thought you had to pre-make the files. Thanks for your time
Also I am running visual studio if that matters

Why? I am also using visual studio just to make sure you know it.

Are you sure there is absolutely nothing in 'farmoutput.txt' at all. Maybe you just happened to open a wrong text file perhaps?
It worked now, I just had to name the output file as a txt file not saved on my computer, I thought you had to pre-make the output file. Thanks again
Last edited on
Also I am running visual studio because my teacher recommended it, I don't know much about other IDEs
hello mclogical,

One question: on line 11 and 12 you open files, but how do you know that they are open? I generally check with:
1
2
3
4
5
6
7
8
if (in_file.is_open())
    cout << "File is open" << endl;
else
{
    cout << "File did not open" << endl;
    Sleep(3000);  //  so you can read the messagee
    exit(1);
}

Once you know it is working skip this part if you want.

Hope that helps,

Andy
Topic archived. No new replies allowed.