?
Solved

Error on method of Menu Class

Posted on 2003-12-10
3
Medium Priority
?
233 Views
Last Modified: 2010-04-01
Hi boys,
i have an error that i can undestand:

i have a method       " ControlloreClienti::stampaListaClienti(vector<Cliente *> c)"
that take a vector(result of a search) and from this built a string inj witch are appended every item of the vector+"esci"

After i have a class menu that take on input this string and built a textual menu.
My program,in runtime have a block in the line "sceltaCliente.setSceltaUtente();"

I have also another method "  " ,so similar,in witch the same code doesn't give problem

I attach my code below:

void ControlloreClienti::stampaListaClienti(vector<Cliente *> c) {                      
    string s = "";
    for(int i = 0; i < c.size(); i++) {                  
        s.append((c[i])->getCodiceFiscale() +
                 Utility::riempimento(15 - ((c[i])->getCodiceFiscale()).size(), ' ')+
                 (c[i])->getNome() +
                 Utility::riempimento(15 - ((c[i])->getNome()).size(), ' ') +
                 (c[i])->getCognome() +
                 Utility::riempimento(15 - ((c[i])->getCognome()).size(), ' ') +
                 (c[i])->getVia() +
                 Utility::num2string((c[i])->getNumeroCivico()) +
                 Utility::riempimento(15 - (Utility::num2string((c[i])->getNumeroCivico())).size(), ' ') +
                 Utility::num_long2string((c[i])->getCap()) +
                 Utility::riempimento(15 - ((c[i])->getVia()).size(), ' ') +
                 (c[i])->getCitta() +
                 Utility::riempimento(15 - ((c[i])->getCitta()).size(), ' ') +        
                 (c[i])->getProvincia() +
                 Utility::riempimento(15 - ((c[i])->getProvincia()).size(), ' ') +
                 (c[i])->getNazione() +
                 Utility::riempimento(15 - ((c[i])->getNazione()).size(), ' ') +
                 Utility::num_long2string((c[i])->getNumeroCartaCredito()) +
                 Utility::riempimento(15 - (Utility::num_long2string((c[i])->getNumeroCartaCredito())).size(), ' ') +
                 Utility::num2string((c[i])->getNumeroNoleggi()) +
                 Utility::riempimento(15 - (Utility::num2string((c[i])->getNumeroNoleggi())).size(), ' ') +
                 Utility::num_long2string((c[i])->getChilometriPercorsi()) +
                 Utility::riempimento(15 - (Utility::num_long2string((c[i])->getChilometriPercorsi())).size(), ' ') +
                 Utility::num_long2string((c[i])->getSpesaTotale()) +
                 Utility::riempimento(15 - (Utility::num_long2string((c[i])->getSpesaTotale())).size(), ' ') +
                 (c[i])->getTarga() +
                 Utility::riempimento(15 - ((c[i])->getTarga()).size(), ' '));                        
        s.append("|");                    
    }
    s.append("ESCI");
    Menu sceltaCliente(s, "VISUALIZZA LE INFORMAZIONI DI UN Cliente");
    do {
        system("cls");
        system("pause");
        cout<<s;system("pause");
        sceltaCliente.setSceltaUtente();/* e qui il problema*/system("pause");
        if(sceltaCliente.getSceltaUtente() > 0 && sceltaCliente.getSceltaUtente() <= c.size()) {system("pause");
            (c[sceltaCliente.getSceltaUtente() - 1])->stampaCliente();
            cout << endl << "\n\nCliente " << sceltaCliente.getSceltaUtente()
                         << "   di   " << c.size() << "." << endl;
            system("pause");
        }
    }
    while(sceltaCliente.getSceltaUtente() != 0);
    return;
}


void ControlloreVeicoli::stampaListaVeicoli(vector<Veicolo *> v) {
    string s = "";
    for(int i = 0; i < v.size(); i++) {
        s.append((v[i])->getCategoria() +
                 Utility::riempimento(15 - ((v[i])->getCategoria()).size(), ' ') +
                 (v[i])->getCasaCostruttrice() +
                 Utility::riempimento(15 - ((v[i])->getCasaCostruttrice()).size(), ' ') +
                 (v[i])->getModello() +
                 Utility::riempimento(15 - ((v[i])->getModello()).size(), ' ') +
                 (v[i])->getTarga());
        s.append("|");
    }
    s.append("ESCI");
    Menu scegliVeicolo(s, "VISUALIZZA LE INFORMAZIONI DI UN VEICOLO");
    do {
        system("cls");
        scegliVeicolo.setSceltaUtente();
        if(scegliVeicolo.getSceltaUtente() > 0 && scegliVeicolo.getSceltaUtente() <= v.size()) {
            (v[scegliVeicolo.getSceltaUtente() - 1])->stampaVeicolo();
            cout << endl << "\n\nVeicolo " << scegliVeicolo.getSceltaUtente()
                         << "   di   " << v.size() << "." << endl;
            system("pause");
        }
    }
    while(scegliVeicolo.getSceltaUtente() != 0);
    return;
}
0
Comment
Question by:zprestaz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 60 total points
ID: 9912025
In Menu::setSceltaUtente() i found this:

     ...
     Utility::riempimento(lunghezza - opzioni[opzioni.size() - 1].size() - 14, ' ')
     ...

As lunghezza is 75  opzioni[opzioni.size() - 1].size()  should be less than 61.
But what happens if not?

You see in

string Utility::riempimento(int nc, char c) {
    string s(nc, ' ');
    for(int i = 0; i < nc; i++)
        s[i] = c;
    return s;
}

that you pass an integer that is signed. When creating a string with

     strinc s(nc, ' ');

nc is converted to an unsigned integer. That might be a very big number if nc is negative (> 2 billion == 2 * 10^9) and i don't know what the string constructor will do in this case (throw a memory exception or need some hours to provide the memory).

I suggest to make this change

string Utility::riempimento(int nc, char c)
{
    if (nc <= 0)
        return string("");         // empty string
    string s(nc, ' ');
    for(int i = 0; i < nc; i++)
        s[i] = c;
    return s;
}

Regards, Alex

0
 

Author Comment

by:zprestaz
ID: 9912210
Alex,
you are great for the n-th time
0
 

Author Comment

by:zprestaz
ID: 9912531
hi alex,
there is my friend that have a problem

see "use of an overloaded operator............. " question
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question