Solved

Error on method of Menu Class

Posted on 2003-12-10
3
228 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
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 20 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

778 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