Re: Newbie question about class member functions...
From: Some Clown (noone_at_nowhere.net)
Date: 02/17/04
- Next message: Claudio Puviani: "Re: using std::sort;"
- Previous message: EventHelix.com: "Re: Recommended Online OOP Tutorials"
- In reply to: Jeff Schwab: "Re: Newbie question about class member functions..."
- Next in thread: jeffc: "Re: Newbie question about class member functions..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 16 Feb 2004 19:14:23 -0800
"Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:GoqdnVXx2LiJ96zdRVn-uA@comcast.com...
> Here are a few words of supposed wisdom. I hope they will be of use.
> I've taken the liberty of applying some of them to your program.
>
> 1. Don't need the "max" constants.
> 2. <fstream>, not <fstream.h>.
> 3. Use std::string instead of raw char arrays.
> 4. Use std::vector instead of other arrays.
> 5. Your "default constructor" isn't a default constructor.
> A default constructor needs no arguments.
> 6. There's no need to define an empty, non-virtual destructor.
> 7. You don't need to pass ios::nocreate to std::ifstream.
> 8. Print error messages to cerr, not cout.
> 9. You might consider handling errors using "exceptions." If you're
> just not at that point yet, it's okay. :)
> 10. Don't use std::endl when a plain newline will do.
> 11. When you do use std::endl, it generally goes after the string
> you're printing, not before it.
> 12. I think you want std::getline, not std::cin.getline.
> 13. main() always returns int, never void. You don't need a return
> statement if the return value is 0.
> 14. An ifstream will close itself at the end of its scope.
> 15. You don't need to label every function with "Function." Just
> list the function's purpose.
While I haven't fixed all of the above (see other posts) I did get my basic
problem solved. There is still the business of using std::string vs. char
arrays... but I need to research that some more as I haven't read much on
that yet. I believe that the book I'm using is a little dated (another
poster clued me into that). Anyhoo... below is my "fixed" code so far
(minus std::string changes).
---------- snip -------------------------
// Playlist Edit - Version 0.1
// Copyright 2004 by Some Clown
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 80; // Maximum number of characters in filename
const int MAX2 = 80; // Maximum number of songs in a playlist
const int maxPath = 200; // Maximum length of path in playlist file
class CPlaylist
{
private:
char* pfileName; // Our filename as passed from main()
char lineItems[MAX2][maxPath];
int j; // Position count in array loops
public:
// Constructor
CPlaylist(char* pfn): pfileName(pfn){}
// Open playlist file and read it into an array
void getFile()
{
j=0; // Number of items read
ifstream theFile(pfileName);
if(!theFile)
{
cerr << "Error opening file!\n";
exit(1);
}
for(int i = 0; ((i < MAX2) && (!theFile.eof())); i++)
{
theFile.getline(lineItems[i], maxPath);
CPlaylist::j++;
}
theFile.close(); // Might need to move this somewhere else once all of the
other
// functions are complete. Or put in some error checking to see
// if file is already closed or not.
}
// Edit a playlist file
void editFile()
{
}
// Print contents of array containing playlist
void printFile()
{
for(int i = 0; i < CPlaylist::j; i++)
{
cout << lineItems[i] << "\n";
}
}
};
int main()
{
char fileName[MAX];
cout << endl
<< "Please enter a playlist name: ";
cin.getline(fileName, MAX);
CPlaylist test(fileName);
test.getFile();
test.printFile();
return 0;
}
- Next message: Claudio Puviani: "Re: using std::sort;"
- Previous message: EventHelix.com: "Re: Recommended Online OOP Tutorials"
- In reply to: Jeff Schwab: "Re: Newbie question about class member functions..."
- Next in thread: jeffc: "Re: Newbie question about class member functions..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|