New to C++ (and basically to all programming)

Discussion in 'Mac Programming' started by DanB91, Apr 27, 2008.

  1. DanB91 macrumors 6502

    Joined:
    Jun 12, 2007
    #1
    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
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Well what code have you got so far? What database are you using? MySQL, PostgreSQL or another?
     
  3. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
  4. DanB91 thread starter macrumors 6502

    Joined:
    Jun 12, 2007
    #4
    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);
    		
    		}
    		 
    	}
    	
     
  5. steelphantom macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #5
    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!
     
  6. DanB91 thread starter macrumors 6502

    Joined:
    Jun 12, 2007
    #6
    thanks very much ill try it out :)
     
  7. psingh01 macrumors 65816

    Joined:
    Apr 19, 2004
    #7
    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
     
  8. jstad macrumors regular

    jstad

    Joined:
    Jun 13, 2007
    #8
    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.
     
  9. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    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.
     
  10. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #10
    Seconded.
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    Thirded.
     

Share This Page