Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DanB91

macrumors 6502
Original poster
Jun 12, 2007
257
0
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
 

DanB91

macrumors 6502
Original poster
Jun 12, 2007
257
0
its for windows and mac this is what i have so far. its prob completely wrong though

Code:
#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);
		
		}
		 
	}
 

steelphantom

macrumors 6502a
Oct 15, 2005
555
1
Just 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!
 

DanB91

macrumors 6502
Original poster
Jun 12, 2007
257
0
Just 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!
thanks very much ill try it out :)
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
Code:
ofstream fout("~/Desktop")

This opens the Desktop directory for writing. I think you want to open a new file within the directory. Like this:

Code:
ofstream fout("~/Desktop/database.txt")

The rest seems ok as far as writing. You should close the file when you are done writing to it.

Code:
fout.close();

Here is a nice link: http://www.gamedev.net/reference/articles/article1127.asp
 

jstad

macrumors regular
Jun 13, 2007
119
0
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.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.

Seconded.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.

Thirded.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.