A n00b.

T

TuperYabba

Hello.
I'm a noob. As such, I will now ask the obligatory stupid questions.
Can anyone tell me how to do this???

I need to write a program that acts as a database for a CD collection,
It has to use a class to store the CD info, has to have a file saving
implemented, and must use dynamic data.

Thank you for any help.
 
V

Victor Bazarov

Hello.
I'm a noob. As such, I will now ask the obligatory stupid questions.
Can anyone tell me how to do this???

We can always help you ask obligatory stupid questions. You can even
get a sample of those off 'groups.google.com'.
I need to write a program that acts as a database for a CD collection,
It has to use a class to store the CD info, has to have a file saving
implemented, and must use dynamic data.

No, sorry, C++ has no means to do this. Database connectivity is OS-
and database-specific, and has to be asked about in the newsgroup that
deals with the OS or/and with the database of choice.

V
 
A

Artist

Okay... Not a database then. The program has to let you enter three
bits of data (artist name, Cdname, year of release for as many CD's as
the user wants, and allow for the saving and retreval of that data to
and from a file.
 
V

Victor Bazarov

Artist said:
Okay... Not a database then. The program has to let you enter three
bits of data (artist name, Cdname, year of release for as many CD's as
the user wants, and allow for the saving and retreval of that data to
and from a file.

Ah, that... I think it's covered by the FAQ 5.2. Check it out.
 
A

Artist

I'm not asking for anyone to do this for me, just a pointer towards
generally the right direction.
 
P

Phlip

Artist said:
I'm not asking for anyone to do this for me, just a pointer towards
generally the right direction.

You go first. Write a little code, and we will review it.
 
J

Jim Langston

Artist said:
I'm not asking for anyone to do this for me, just a pointer towards
generally the right direction.

std::cin for input.
store the data in a vector or a map
std::cout for output
If you need to store the data, look at fstreams

Put some code together the best you can and post what you got if you still
have problems.
 
D

Daniel T.

Artist said:
Okay... Not a database then. The program has to let you enter three
bits of data (artist name, Cdname, year of release for as many CD's as
the user wants, and allow for the saving and retreval of that data to
and from a file.

Well first, I think you need to read up on how to accept user data. Have
you ever done that? Like taking the user's name and printing it back out?
 
A

Artist

OKay, this is my baseline code, what I have so far

// classes test
#include <iostream>
using namespace std;

class riaaconner {
char conname[30],cartist[30];
int doc;
Public:
void set_values (conname,cartist,doc);
};

void riaaconner::set_values (char cnget[30], char caget[30], int dget)
{

conname = cnget;
cartist = caget;
doc = dget;
}

int main () {
char cngsend[30]
char cagsend[30]
int dgsend
riaaconner getdat;
cout << "CD Storage Prog BETA" <<endl;
endl;
cout << "This Prog enters data for you CD colection. The syntax is"
<<endl;
cout << "as follows: CD Name, Artist, Year Realeased." <<endl;
endl;
cout << "Please enter the data." <<endl;
endl;
cout << "Enter the CD Name." <<endl;
cin >> cngsend;
cout << "Enter the Artist Name." <<endl;
cin >> cagsend;
cout << "Enter the Release Year." <<endl;
cin >> dgsend;
getdat.set_values (cngsend,cagsend,dgsend)
cout << "Results:"<<endl;
cout << "CD Name is: " << getdat.cnget();
endl;
cout << "Artist Name is: " << getdat.caget();
endl;
cout << "Release Year is: " << getdat.dget();
return 0;
}
 
B

Bart

Artist wrote:
<snip>

See my comments below.
class riaaconner {

Nice naming. ;)

Should be lowercase.

public:
conname = cnget;
cartist = caget;

This doesn't work. You can't assign an array. I will suggest a better
solution below.
char cngsend[30]
char cagsend[30]
int dgsend

You forgot your semi-colons here.

This statement doesn't do anything. If you want to print a newline you
have to pass it to cout:

cout << endl;

Or just use << endl << endl at the end of each output line.

<snip>

There are several other problems with your code. First of all, you
shouldn't use character arrays to store strings. There are many good
reasons. Among them:

a) You have no control over the user input. If someone enters more than
30 characters your program will probably crash.
b) You have to manually keep track of and copy the characters.
c) It doesn't do what you expect when you compare or assign.

Instead, you should use the 'string' class. It works exactly as
expected, like all the other built-in types and accepts an arbitrary
number of input characters. Here's an example:

#include <string>
using namespace std;
// ...
string mystring;
mystring = "hello!";
cout << mystring;

Second of all, your class is fairly useless. It just serves as a big
bag to put stuff in instead of representing a true logical object.
There are a few things you can do to make it better. First, let your
class read itself instead of doing it outside:

public:
void Read()
{
cin >> name;
cin >> artist;
}

You would use this class like so:

MyClass obj;
obj.Read();

For writing you do the same thing. Have a member function that does it:

public:
void Write()
{
cout << name;
cout << artist;
}

Once you get it to work with 'cin' and 'cout' you can make it work with
any stream. You just have to pass your streams as parameters, like so:

void Read(istream& input);
void Write(ostream& output);

Now, to call the Read and Write functions you have to pass it an open
stream (possibly a file), like so:

obj.Read(inputfile);
obj.Write(outputfile);

Try all the things I mention and then post your code again.

Regards,
Bart.
 
O

osmium

Artist said:
Okay... Not a database then. The program has to let you enter three
bits of data (artist name, Cdname, year of release for as many CD's as
the user wants, and allow for the saving and retreval of that data to
and from a file.

See if this gives you something to think about. Not tested.

class CD
{
public:
CD(string artist_arg, string name_arg; int year);
private:
string artist;
string name;
int year;
}
----
class Data_base
{
public:
Data_base(char* file_name, bool new); // restore the data from the
file if new is false
~ Data_base() // save the data base to a file
private:
vector<CD> db;
};
----
int main()
{
// see if this is an initial creation of the data base
Data_base db( stuff);
// stuff
return 0;
}
 
D

Default User

Hello.
I'm a noob. As such, I will now ask the obligatory stupid questions.
Can anyone tell me how to do this???

I need to write a program that acts as a database for a CD collection,
It has to use a class to store the CD info, has to have a file saving
implemented, and must use dynamic data.

Thank you for any help.


Assuming that this is some sort of homework, and that you aren't really
interested in using a relational database, with all its associated
baggage, then likely what you'd want is text-based flatfile database.
Those are easy to work with and work well enough for a small project.

The exact form of the flatfile will be up to you. Tab-separated fields
with a new-line record separator are pretty easy to use, especially if
you restrict the data so that those characters can't appear (you'll
need to check). Something like:


CD Title\tArtist\tRelease Date\nDetails\n


Here the \t and \n represent actual tab and new-line character that
would appear in the file, you use the escape sequences to create them.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top