C++. I need help with my program

June 17, 2009 at 22:36:03
Specs: Windows Vista
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <apstring.h>
//#include “apstring.h”

struct CD
{
apstring name,
artist;
int numberOfTracks;
double value;
};

CD temp;
const apstring EXIT = “exit”;
const int WIDTH = 28;

//PROTOTYPES
void Print_Menu(int);
int numberOfCDs(int);
void Print_CD();
void Add_CD(int &);
void View_CDDIR();

// Make this function later, when I know how to without doubt.
//void Delete_CD();

int main()
{
int numCDs;
apstring choice;
do
{
system("CLS");

numCDs = numberOfCDs(numCDs);

Print_Menu(numCDs);
getline(cin, choice);

if(choice != EXIT)
{
switch(choice)
{
case 'p': case 'P': Print_CD(); break;
case 'a': case 'A': Add_CD(numCDs); break;
case 'v': case 'V': View_CDDIR(); break;
default: break;
}
}// end if
}
while(choice != EXIT);
return 0;
}

void Print_Menu(int numCDs)
{
cout << “You currently have " << numCDs << " CD(s) in your archive.\n”;
cout << “Enter 'exit' to quit program.\n\n”;
cout.setf(ios::left);

cout << setw(WIDTH) << "Print an old CD" << "P" << endl;
cout << setw(WIDTH) << "Add a new CD" << "A" << endl;
cout << setw(WIDTH) << "View the CD Archive" << "V" << endl;
// I also need to include a DELETE function
cout << "\nEnter your selection: ";
}

void Print_CD()
{
apstring fileToRead;
ifstream ReadFile;

cout << "\nPlease enter the name of the CD to view: ";
getline(cin,fileToRead);

ReadFile.open(fileToRead,ios::in); // open file for user to view

//Initialize cd temp2 and print it's content

cout.setf(ios::left); // left justify output

CD temp2; // declare the new temporary cd spacifically for viewing

if(ReadFile)
{
//initialize
ReadFile.getline(temp2.name,41);
ReadFile.getline(temp2.artist,41);
ReadFile >> temp2.numberOfTracks;
ReadFile >> temp2.value;

//print
cout<<setw(WIDTH)<<"CD Name:"<<temp2.name<<endl;
cout<<setw(WIDTH)<<"CD Artist:"<<temp2.artist<<endl;
cout<<setw(WIDTH)<<"Number of Tracks:"<<temp2.numberOfTracks<<endl;
cout<<setw(WIDTH)<<"Value of CD:"<<"$"<<temp2.value;

cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}
else
cout << "The file by that name does not exist.";
cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}

void Add_CD(int & numCDs)
{
apstring outputfilename;
ofstream outfile;

cout << "\nEnter the CD name: ";
getline(cin,temp.name);

//initialize the CD name to the name of the file
temp.name = outputfilename;

//open the new file for output
outfile.open(outputfilename, ios::out);

if(outfile)
{
outfile.unsetf(ios::skipws);

cout << "Enter the artist's name: ";
getline(cin,temp.artist);
cout << "Enter the number of tracks: ";
cin >> temp.numberOfTracks;
cout << "Enter the value of the CD: $";
cin >> temp.value;

outfile << temp.name << endl
<< temp.artist << endl
<< temp.numberOfTracks << endl
<< temp.value << endl << endl;

outfile.close();
cout << “File was sucksuccessfully added to the drive.”;
cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}
else
{
cout << "There was an error creating the file.\n";
cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}
//////////////////////////////////////////////////////////////////////////
//INCREMENT THE TOTAL NUMBER OF CD'S IN ARCHIVE BY ADDING LATEST CD NAME
ofstream appfile;
appfile.open("cddir",ios::app);

if (!appfile)
cout << "There was an error opening 'cddir' file.\n";
else
{
appfile << temp.name;
appfile.close();
}
}//end of function add_cd-----------------------------------------------

void View_CDDIR()
{
ifstream infile;
apstring content3;

infile.open("cddir",ios::in);

cout << endl;

do
{
getline(infile,content3);
cout << content3 <<endl;
}
while(!infile.eof());
infile.close();

cin.ignore(80,'\n');
cin.get();
cin.ignore(80,'\n');
}


int numberOfCDs(int numCDs)
{
ifstream infile;
infile.open("cddir",ios::in);
apstring content;
numCDs = 0;

if(infile)
{
do
{
getline(infile,content);
if(!infile.eof())
numCDs++;
}
while(!infile.eof());
infile.close();
}
else
{
cout << "The file 'cddir' is not present!\nOne will be created for you.\n";
ofstream createfile;
createfile.open("cddir",ios::out);
if(createfile)
{
cout << "\nThe file has been created and there is nothing in it.\n\n";
createfile.close();
}
else
{
cout << "\nUnable to create file 'cddir'\n\n";
}
}
return numCDs;
ITs not working help


See More: C++. I need help with my program

Report •


#1
June 17, 2009 at 22:40:03
Wrong Forum.

Eyes Right, top of Page, or click Specialty forums Tab.

Think "Programming"

There is nothing to learn from someone who already agrees with you.


Report •
Related Solutions


Ask Question