I am trying to make a "database" where one can store all the values they entered into a text file. what is the command to make it save out? thanks
#include <stdlib.h>
#include <ostream>
#include <fstream>
#include <iostream>
using namespace std;
int main() {
string RoomName; //string for the name of the room
string RoomDes; //string for the contents of the room
string NetWorth; //sring for the net worth of the contents of the room
cout << "Welcome to CCDatabase! This program is used to store information on the things in your for insurance purposes.\n" << endl;
cout << "Enter 'q' when the room name is asked to quit and save your data.\n" << endl;
cout << "Please enter the name of the room you want to be recorded.\n" << endl;
getline(cin, RoomName);
cout << "Please enter the contents of that particular room\n" << endl;
getline(cin, RoomDes);
cout << "What is the net worth of the contents of that particular room?\n" << endl;
getline(cin, NetWorth);
if (RoomName == "q"){
ofstream fout("~/Desktop");
fout << "Room=" + RoomName << endl;
fout << "Contents=" + RoomDes << endl;
fout << "Networth=" + NetWorth << endl;
cout << "Please enter the name of the room you want to be recorded.\n" << endl;
getline(cin, RoomName);
cout << "Please enter the contents of that particular room\n" << endl;
getline(cin, RoomDes);
cout << "What is the net worth of the contents of that particular room?\n" << endl;
getline(cin, NetWorth);
}
}
thanks very much ill try it outJust a few comments about your code:
1. "\n" and "endl" do the same thing, so your code is actually creating two new lines after you print out a statement. There's nothing wrong with this, but you could also do this: cout << "blah blah\n\n" for two newlines, or you could do this: cout << "blah blah" << endl << endl. Just thought I'd point that out.
2. "fout << "Room=" + RoomName << endl;" might work (I'm not really sure), but C++ convention would be this: "fout << "Room=" << RoomName << endl;
Also, you're going to want to use a while loop to prompt the user to keep entering values into your database. Something like this:
while (RoomName != "q")
{
read in data
}
Good luck!
ofstream fout("~/Desktop")
ofstream fout("~/Desktop/database.txt")
fout.close();
If your new to all programming I suggest taking a look at an Intro to ______ Language book. Also i would start from a object oriented language (as I think its much easier to start with) such as Java, Ruby or C#. Your code has a few bad habits but I have definitely seen much worse.
Actually, I would not start with an object oriented language. Object orientation is an enormous benefit when tackling complex and/or large projects, but when shoe-horned into "hobby" or "newbe" projects, it can cause a lot of mental overhead.
Just my two cents.
Actually, I would not start with an object oriented language. Object orientation is an enormous benefit when tackling complex and/or large projects, but when shoe-horned into "hobby" or "newbe" projects, it can cause a lot of mental overhead.
Just my two cents.