Storing data in text file
Nov 5, 2021 at 3:16am UTC
I am creating a program that writes data about a pet to a text file. However this data is not getting written to the pet.txt file. What am I doing wrong?
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
#include <iostream>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int user_choice;
cout << "\nManage Your Pet\n"
<< " 1. Create Pet\n"
<< " 2. Display Pet\n"
<< " 3. Delete Pet\n"
<< " 4. Exit\n"
<< "\nEnter your choice (1-4): " ;
cin >> user_choice;
cout << endl;
switch (user_choice)
{
case 1:
string pname;
string ptype;
int page;
string pcomment;
ofstream fw("pet.txt" , std::ofstream::out);
if (fw.is_open())
{
cout << "What is your pet's name?" << endl;
cin >> pname;
cout << "What is your pet's type?" << endl;
cin >> ptype;
cout << "How old is your pet?" << endl;
cin >> page;
cout << "Enter a comment about the pet!" << endl;
cin >> pcomment;
cout << "Data stored!" << endl;
fw.close();
}
else cout << "Problem with opening file" ;
break ;
}
Topic archived. No new replies allowed.