Jump to content

C++ problem


Papito

Recommended Posts

Hello guys, long time I dont start a new topic and less one about C++, a few years back I have studied c++ but know I forget everything and now I want to begin again, I have 2 lil console software to do (see attached files), I dont need something complicated just a simple code, is there somebody who can help me out?
 

 

I have this:

 

header.h

#include <string>
#include <vector>
#include<fstream>
using namespace std;
 
enum Genre {
POP_MUSIC,
ROCK_MUSIC,
FOLK_MUSIC,
PUNK_MUSIC,
COUNTRY_MUSIC,
DISCO_MUSIC,
REGGAETON_MUSIC,
CLASSIC_MUSIC
 
};
 
enum DISPLAY_OPTS {
FULL_DESC,
MAIN_DESC,
SIMPLE_DESC
};
 
 
class CD {
private:
string name;
string artist;
string label;
int tracks;
vector<string> tracklist;
int year;
Genre genre;
public:
CD();
~CD();
void setName(string);
void setArtist(string);
void setLabel(string);
void setYear(int);
void setGenre(Genre);
void setTrackList(vector<string>);
void addTrackToList(string);
string removeTrackFromList(string);
 
string getName();
string getArtist();
string getLabel();
int getYear();
Genre getGenre();
vector<string> getTrackList();
string getTrack(int);
int getTracks();
 
string showCD(DISPLAY_OPTS);
void saveCDToFile(ofstream &);
 
};

Resource.cpp

#include "Header.h"
#include <sstream>
using namespace std;
 
CD::CD(){}
CD::~CD(){}
 
void CD::setName(string name) {
this->name = name;
}
void CD::setArtist(string artist) {
this->artist = artist;
}
void CD::setLabel(string label) {
this->label = label;
}
void CD::setYear(int year) {
this->year = year;
}
void CD::setGenre(Genre genre) {
this->genre = genre;
}
void CD::setTrackList(vector<string> list) {
this->tracklist = list;
}
void CD::addTrackToList(string track) {
this->tracklist.push_back(track);
}
string CD::removeTrackFromList(string value) {
int index = -1;
for(int i=0; i<this->tracklist.size(); i++) {
if (value.compare(this->tracklist[i]) == 0){
index = i;
break;
}
 
}
if(index != -1) {
try {
this->tracklist.erase(this->tracklist.begin() + index);
} catch (int ex) {
return "Error al borrar"; 
}
return "Borrado";
}
return "No encontrado";
}
string CD::getName() {
return this->name;
}
string CD::getArtist() {
return this->artist;
}
string CD::getLabel() {
return this->label;
}
int CD::getYear() {
return this->year;
}
Genre CD::getGenre() {
return this->genre;
}
vector<string> CD::getTrackList() {
return this->tracklist;
}
string CD::getTrack(int trackNum) {
if(trackNum < getTracks())
return this->tracklist[trackNum];
return "";
}
int CD::getTracks() {
return this->tracklist.size();
}
 
string CD::showCD(DISPLAY_OPTS opts) {
stringstream outputStr;
if(opts == SIMPLE_DESC) {
outputStr << this->getName() << "," << this->getArtist() << endl;
} else if(opts == MAIN_DESC || opts==FULL_DESC) {
outputStr << "Name:" << this->getName() << endl;
outputStr << "Artist: " << this->getArtist() << endl;
outputStr << "Label: " << this->getLabel() << endl;
outputStr << "Year: " << this->getYear() << endl;
outputStr << "Genre : " << this->getGenre() << endl;
outputStr << "Tracks: " << this->getTracks() << endl;
 
 
} if(opts == FULL_DESC) {
for ( int i = 0; i <this->getTracks(); i++){
outputStr << i+1 << " - " << tracklist[i];
}
}
return outputStr.str();
 
 
 
}
void CD::saveCDToFile(ofstream & output ){
 
output << this->getName() << endl;
output <<  this->getArtist() << endl;
output <<  this->getLabel() << endl;
output <<  (int)this->getGenre() << endl;
output << this->getYear() << endl;
output <<  this->getTracks() << endl;
for ( int i = 0; i <this->getTracks(); i++){
output <<  tracklist[i];
}
 
}

trabajoexamen.cpp

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
 
#include "Header.h"
CD inputCDData() throw (int);
vector<CD> inputCDDataFromFile(string) throw (int);
void saveCatalog(vector<CD>);
int main() {
ifstream file;
vector<CD> catalog;
int menuOpt;
 
 
do{
cout << "Que deseas hacer: \n1 - Capturar\n2 - Cargar\n3 - Mostrar\n4 - Salir";
cin >> menuOpt;
cin.ignore(1 , '\n');
if ( menuOpt == 1) {
try { 
CD ccd = inputCDData();
catalog.push_back(ccd);
} catch ( int exceptionCode) {
cerr << "Error en lectura.\n";
}
}
else if ( menuOpt == 2 ) {
string filename;
try { 
cout << "Que archivo quieres cargar: ";
file >> filename;
vector<CD> ccd = inputCDDataFromFile(filename);
for ( int i = 0; i < ccd.size();i++){
catalog.push_back(ccd[i]);
}
} catch ( int exceptionCode) {
cerr << "Error en lectura.\n";
}
}
else if ( menuOpt == 3 ) {
//displayCDData(catalog);
}
} while ( menuOpt != 4);
saveCatalog(catalog);
system("pause");
}
 
CD inputCDData() throw (int) {
CD record;
string tempValue;
int tempInt;
 
cout << "Dame el titulo del album: ";
getline ( cin , tempValue);
record.setName(tempValue);
cout << "Dame el artista: ";
getline ( cin , tempValue);
record.setArtist(tempValue);
cout << "De que compa\224ia es el album: ";
getline ( cin , tempValue);
record.setLabel(tempValue);
cout << "A que genero pertenece:\n1 - Pop\n2 - Rock\n3 - Folk\n4 - Punk\n5 - Country\n6 - Disco\n7 - Raegeton"
<< "\n8 - Clasico\n";
cin >> tempInt;
record.setGenre(Genre(tempInt - 1));
cout << "De que a\244o es el album: ";
cin >> tempInt;
record.setYear(tempInt);
cout << "Cuantas canciones tiene: ";
cin >> tempInt;
 
for( int i = 0; i < tempInt; i++){
cin.ignore( 1 , '\n');
cout << "Dame la cancion #" << i+1 << ": ";
getline (cin,tempValue);
record.addTrackToList(tempValue);
}
 
system("cls");
return record;
}
 
vector<CD> inputCDDataFromFile(string filename) throw (int) {
vector<CD> records;
CD record;
string tempValue;
int tempInt;
ifstream file;
system("cls");
 
file.open(filename);
if ( file.is_open()) {
while ( !file.eof()){
 
getline ( file , tempValue);
record.setName(tempValue);
 
getline ( file , tempValue);
record.setArtist(tempValue);
 
getline ( file , tempValue);
record.setLabel(tempValue);
 
file >> tempInt;
record.setGenre(Genre(tempInt - 1));
 
file >> tempInt;
record.setYear(tempInt);
 
file >> tempInt;
file.ignore(1,'\n');
for( int i = 0; i < tempInt; i++){
cin.ignore( 1 , '\n');
 
getline (file,tempValue);
record.addTrackToList(tempValue);
}
records.push_back(record);
 
}
file.close();
} else {
throw 10;
}
 
system("cls");
return records;
}
void saveCatalog(vector<CD> catalog){
ofstream output;
output.open("catalog.txt", ios::app || ios::ate);
if(output.is_open()){
for(int i=0; i < catalog.size(); i++){
catalog[i].saveCDToFile(output);
}
output.close();
}
}

post-460-0-45319100-1394778216_thumb.jpg

post-460-0-46948600-1394778218_thumb.jpg

Link to comment
Share on other sites

Had a chance to look at it and unfortunately I was unsuccessful :P

 

The first errors that came up had to do with the ofstream and ifstream, but once those errors went away it had a lot of the header references you were talking about and that is outside the spectrum of knowledge for me :(

 

the errors with the ifstream and ofstream

file.open(filename);    line 94

output.open("catalog.txt", ios::app || ios::ate);   line 134


i googled it and it said that ifstream takes a const char* and not a string

so had to change the first one to file.open(filename.c_str());



sorry papito hopefully someone else more knowledgeable can help you!

  • Like 1
Link to comment
Share on other sites

 

file.open(filename);

should be

 

file.open( filename.c_str() )

 

for one thing.

 

I can work on debugging maybe friday (I like coding). I have tons of shit this week to do :/

 

Link to comment
Share on other sites

Alright, I have your program compiling and running. I'm not sure the functionality is exactly correct, but it appears so. I fixed your indentation/style because it was driving me batty, and I removed the "system" calls, because you should never, ever, do that :) I'm not sure why you had the line

file.open( filename.c_str(), ios::app || ios::ate)

I changed it to:

file.open( filename.c_str(), ios:app)

If you have any further questions, let me know.

 

*edit* Might consider a switch statement for main.cpp instead of if else if else.

 

(I also renamed the files appropriately)

 

main.cpp:

#include <iostream>
#include <vector>
#include <fstream>
#include "jukebox.h"

using namespace std;

CD inputCDData() throw (int);
vector<CD> inputCDDataFromFile(string) throw (int);
void saveCatalog(vector<CD>);

int main()
{
  ifstream file;
  vector<CD> catalog;
  int menuOpt;

  do
  {
    cout << "Que deseas hacer: \n1 - Capturar\n2 - Cargar\n3 - Mostrar\n4 - Salir";
    cin >> menuOpt;
    cin.ignore(1 , '\n');
    if ( menuOpt == 1)
    {
      try
      {
        CD ccd = inputCDData();
        catalog.push_back(ccd);
      }
      catch ( int exceptionCode)
      {
        cerr << "Error en lectura.\n";
      }
    }
    else if ( menuOpt == 2 )
    {
      string filename;
      try
      {
        cout << "Que archivo quieres cargar: ";
        file >> filename;
        vector<CD> ccd = inputCDDataFromFile(filename);
        for ( int i = 0; i < ccd.size();i++)
        {
          catalog.push_back(ccd[i]);
        }
      }
      catch ( int exceptionCode)
      {
        cerr << "Error en lectura.\n";
      }
    }
    else if ( menuOpt == 3 )
    {
    //displayCDData(catalog);
    }
  } while ( menuOpt != 4);

  saveCatalog(catalog);
  return 0;
}

CD inputCDData() throw (int)
{
  CD record;
  string tempValue;
  int tempInt;

  cout << "Dame el titulo del album: ";
  getline ( cin , tempValue);
  record.setName(tempValue);
  cout << "Dame el artista: ";
  getline ( cin , tempValue);
  record.setArtist(tempValue);
  cout << "De que compa\224ia es el album: ";
  getline ( cin , tempValue);
  record.setLabel(tempValue);
  cout << "A que genero pertenece:\n1 - Pop\n2 - Rock\n3 - Folk\n4 - Punk\n5 -"
       << "Country\n6 - Disco\n7 - Raegeton\n8 - Clasico\n";
  cin >> tempInt;
  record.setGenre(Genre(tempInt - 1));
  cout << "De que a\244o es el album: ";
  cin >> tempInt;
  record.setYear(tempInt);
  cout << "Cuantas canciones tiene: ";
  cin >> tempInt;

  for( int i = 0; i < tempInt; i++)
  {
    cin.ignore( 1 , '\n');
    cout << "Dame la cancion #" << i+1 << ": ";
    getline (cin,tempValue);
    record.addTrackToList(tempValue);
  }

  return record;
}

vector<CD> inputCDDataFromFile(string filename) throw (int)
{
  vector<CD> records;
  CD record;
  string tempValue;
  int tempInt;
  ifstream file;

  file.open( filename.c_str() );
  if ( file.is_open() )
  {
    while ( !file.eof() )
    {

      getline (file , tempValue);
      record.setName(tempValue);

      getline (file , tempValue);
      record.setArtist(tempValue);

      getline (file , tempValue);
      record.setLabel(tempValue);

      file >> tempInt;
      record.setGenre( Genre(tempInt - 1) );

      file >> tempInt;
      record.setYear(tempInt);

      file >> tempInt;
      file.ignore(1,'\n');
      for( int i = 0; i < tempInt; i++)
      {
        cin.ignore( 1 , '\n');

        getline (file,tempValue);
        record.addTrackToList(tempValue);
      }
      records.push_back(record);

    }
  file.close();
  }
  else
  {
    throw 10;
  }

  return records;
}

void saveCatalog(vector<CD> catalog)
{
  ofstream output;
  output.open("catalog.txt", ios::app);
  if(output.is_open())
  {
    for(int i=0; i < catalog.size(); i++)
    {
      catalog[i].saveCDToFile(output);
    }
    output.close();
  }
}

jukebox.h: (btw, it's a bad idea to use namespace std in a header file)

#include <string>
#include <vector>
#include<fstream>

enum Genre
{
  POP_MUSIC,
  ROCK_MUSIC,
  FOLK_MUSIC,
  PUNK_MUSIC,
  COUNTRY_MUSIC,
  DISCO_MUSIC,
  REGGAETON_MUSIC,
  CLASSIC_MUSIC
};

enum DISPLAY_OPTS
{
  FULL_DESC,
  MAIN_DESC,
  SIMPLE_DESC
};


class CD
{
  private:
    std::string name;
    std::string artist;
    std::string label;
    int tracks;
    std::vector<std::string> tracklist;
    int year;
    Genre genre;
  public:
    CD();
    ~CD();
    void setName(std::string);
    void setArtist(std::string);
    void setLabel(std::string);
    void setYear(int);
    void setGenre(Genre);
    void setTrackList(std::vector<std::string>);
    void addTrackToList(std::string);
    std::string removeTrackFromList(std::string);
    std::string getName();
    std::string getArtist();
    std::string getLabel();
    int getYear();
    Genre getGenre();
    std::vector<std::string> getTrackList();
    std::string getTrack(int);
    int getTracks();
    std::string showCD(DISPLAY_OPTS);
    void saveCDToFile(std::ofstream &);
};

and jukebox.cpp:

#include "jukebox.h"
#include <sstream>
#include <string>

using namespace std;

CD::CD(){}

CD::~CD(){}

void CD::setName(string name) { this->name = name; }

void CD::setArtist(string artist) { this->artist = artist; }

void CD::setLabel(string label) { this->label = label; }

void CD::setYear(int year) { this->year = year; }

void CD::setGenre(Genre genre) { this->genre = genre; }

void CD::setTrackList(vector<string> list) { this->tracklist = list; }

void CD::addTrackToList(string track) { this->tracklist.push_back(track); }

string CD::removeTrackFromList(string value)
{
  int index = -1;
  for(int i=0; i<this->tracklist.size(); i++)
  {
    if (value.compare(this->tracklist[i]) == 0)
    {
      index = i;
      break;
    }
  }
  if(index != -1)
  {
    try
    {
      this->tracklist.erase(this->tracklist.begin() + index);
    }
    catch (int ex)
    {
      return "Error al borrar";
    }
    return "Borrado";
  }
  return "No encontrado";
}

string CD::getName() { return this->name; }

string CD::getArtist() { return this->artist; }

string CD::getLabel() { return this->label; }

int CD::getYear() { return this->year; }

Genre CD::getGenre() { return this->genre; }

vector<string> CD::getTrackList() { return this->tracklist; }

string CD::getTrack(int trackNum)
{
  if(trackNum < getTracks())
  {
    return this->tracklist[trackNum];
  }
  return "";
}

int CD::getTracks() { return this->tracklist.size(); }

string CD::showCD(DISPLAY_OPTS opts)
{
  stringstream outputStr;
  if(opts == SIMPLE_DESC)
  {
    outputStr << this->getName() << "," << this->getArtist() << endl;
  }
  else if(opts == MAIN_DESC || opts==FULL_DESC)
  {
    outputStr << "Name:" << this->getName() << endl;
    outputStr << "Artist: " << this->getArtist() << endl;
    outputStr << "Label: " << this->getLabel() << endl;
    outputStr << "Year: " << this->getYear() << endl;
    outputStr << "Genre : " << this->getGenre() << endl;
    outputStr << "Tracks: " << this->getTracks() << endl;
  }
  if(opts == FULL_DESC)
  {
    for ( int i = 0; i <this->getTracks(); i++)
    {
      outputStr << i+1 << " - " << tracklist[i];
    }
  }
  return outputStr.str();
}

void CD::saveCDToFile(ofstream & output )
{

  output << this->getName() << endl;
  output <<  this->getArtist() << endl;
  output <<  this->getLabel() << endl;
  output <<  (int)this->getGenre() << endl;
  output << this->getYear() << endl;
  output <<  this->getTracks() << endl;
  for ( int i = 0; i <this->getTracks(); i++)
  {
    output <<  tracklist[i];
  }
}
Edited by parrot
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.