Solved

Error on method of Menu Class

Posted on 2003-12-10
3
230 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 70
Embarcadero C++ Builder XE2 TDateTime 8 80
Eclipse IDE - Cannot copy/paste from console output 8 324
Problem with SqlConnection 4 184
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

730 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