1

I need a quick hint on this function. So basically I have this struct, used by a class.

#include <cstring>
#include <iostream>
using namespace std;

struct postazione{
    char* nome;
    bool occupato;
};

class Aula{
    int qntpst;
    postazione * vett;
    bool full(const Aula&);
public:
    Aula(int);
    bool aggiungi(const char*);
    friend ostream& operator<<(ostream&, const Aula&);
    Aula& elimina(int);
    Aula(const Aula&); 
    Aula& operator!();
    ~Aula();
};

That is, an array of which each element is a string and a bool, but the the last one is not important now.

The ! operator has to sort the array in alphabetical order.

Here's how I tried to do it.

Aula& Aula::operator!(){
    int qnt=0;
    for(int i=0;i<qntpst;i++)
            if(vett[i].occupato)
                    qnt++;
    if(qnt!=qntpst)
            return *this;
    char *temp;
    for(int i=0;i<qntpst-1;i++){
            for(int j=i+1;j<qntpst;j++){
                    if(strcmp(vett[i].nome,vett[j].nome)>0){
                            temp=new char[strlen(vett[i].nome)+1];
                            strcpy(vett[i].nome,temp);
                            delete [] vett[i].nome;
                            vett[i].nome=new char[strlen(vett[j].nome)+1];
                            strcpy(vett[i].nome,vett[j].nome);
                            delete [] vett[j].nome;
                            vett[j].nome=new char[strlen(temp)+1];
                            strcpy(vett[j].nome,temp);
                            delete temp;
                    }
            }
    }
    return *this;
}

First 7 lines check if each bool of each element of the array is true, otherwise it won't execute. Then it starts to sort. That's how I tried to do it but it doesn't work.

P.S. The solution has to use an auxilary pointer, something like: aux=i; i=j; j=aux;

3
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your code and accurately describe the problem. In particular, "doesn't work" is not a description of the problem. Show the given input and output. Commented Jan 13, 2016 at 1:09
  • 1
    gawd almighty. Any reason not to simply use qsort or std::sort? You pass either one of them an array of whatever you like and provide a comparison function. (Your comparison function would essentially be a one-liner.) Commented Jan 13, 2016 at 1:54
  • I know that all of you think I should be using that, thing is, this whole thing is the C++ Exam I had yesterday, that's why I use the ! operator to sort the array, because it was asked me to do so. And now I have to correct any of those functions that I wasn't able to implement. Commented Jan 13, 2016 at 9:49

2 Answers 2

1

Quick hints.

  1. Don't use operator!() to sort. Use a function named something like sort(). operator!() generally does a very different thing, and using it to sort will make your code harder to understand.
  2. Don't use using namespace std in a header file (or before class definitions that rely on it). There are plenty of explanations as to why on the internet.
  3. Use standard C++ library capabilities rather than rolling your own as you have.

For example, the following omits constructors and the like, but does 90% of what you seek. With no worrying about memory management, getting the algorithm to sort right, etc etc.

#include <string>
#include <vector>
#include <algorithm>

struct postazione
{
    std::string nome;
    bool occupato;
    bool operator<(const postazione & other) const
      {
           return nome < other.nome;   // std::string supports this
      };
};


class Aula
{
       std::vector<postazione> data;
   public:
      // constructors, destructors, etc

       void sort()    // sort the current vector
       {
            std::sort(data.begin(), data.end());  // this sorts using the order defined by postazione::operator<()
       }         

       Aula Sorted() const  // return a sorted copy of ourselves
       {
           Aula temp(*this);    // copy ourself
           temp.sort();         // sort the copy
           return temp;         //  return sorted copy
       };
};

The thing to realise is that std::string, std::vector (and other containers in the C++ library) keep track of their size, and resize themselves cleanly when required.

Read up on std::string and std::vector to work out what you need to do to manage them (set contents, update, etc).

Sign up to request clarification or add additional context in comments.

1 Comment

As I said below to David Alvarez, unfortunately, even though I don't know what sort() does, I guess it is much more faster and easier, but since this code was for the exam I had yesterday, this explains why, for example, I use the ! operator to sort the array, and I have to do it in that specific way
0

You are not copying anything to your temporary pointer.

Change

strcpy(vett[i].nome,temp);

to

strcpy(temp, vett[i].nome);

Also, let me just say the method you're using is extremely inefficient and likely to cause problems. I would define a comparison function for your class and use std::sort(). And please, do provide more information next time, an input and an output with the expected output, it helps.

1 Comment

Unfortunately, even though I don't know what sort() does, I guess it is much more faster and easier, but since this code was for the exam I had yesterday, this explains why, for example, I use the ! operator to sort the array, and I have to do it in that specific way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.