Solved

Error on method of Menu Class

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now