Solved

Error on method of Menu Class

Posted on 2003-12-10
9
244 Views
Last Modified: 2010-04-02
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
  • 4
  • 3
9 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9911197
Can you please show the code for setSceltaUtente () of class Menu.  

A hint: not everyone will understand "e qui il problema" - The problem is here.
0
 

Author Comment

by:zprestaz
ID: 9911206
Code of class menu:

// Menu.cpp

#include "Menu.h"
#include "Utility.h"

//costruttore
Menu::Menu( string o, string t ="", string dv = "20", string ev = "0")
   : sceltaUtente("Seleziona",dv,ev,"-1")
{
  titolo = ( t=="" ? "MENU'" : t );
  escapeValue = ev;
 
  int index;
  do {
     index = o.find("|");
     if ( index != string::npos ) {  // se non ho trovato "|" ...
        opzioni.push_back( o.substr(0,index) );
        o = o.substr(index+1);
     }
     else {
        opzioni.push_back( o );
        o = "";
     }
  } while ( !o.empty() );
}

//la scelta
bool Menu::valutaOpzione()    
{
  if  ( sceltaUtente.getDato() == atoi(escapeValue.c_str()) )
      return true;

  if  ( sceltaUtente.getDato() < 0 || sceltaUtente.getDato() > opzioni.size() )
     return  false;
  else
     return true;
}


//costruzione menù grafico
void Menu::setSceltaUtente( bool clean=true , bool main = false )
{
    do {
        if (clean) system("cls");
        cout << endl;
        cout << Utility::riempimento(lunghezza, '^') << endl;
        cout << ">" << Utility::riempimento(lunghezza - 2, ' ') << "<" << endl;
        cout << ">" << Utility::riempimento((lunghezza - 2 - titolo.size()) / 2, ' ') << titolo;

        cout << (
            titolo.size() % 2 == 1 ?
            Utility::riempimento((lunghezza - titolo.size() - 2) / 2, ' ') :
            Utility::riempimento((lunghezza - titolo.size() - 2) / 2 + 1, ' ')
                 )
            << "<" << endl;

        cout << ">" << Utility::riempimento(lunghezza - 2, ' ') << "<" << endl;
        cout << Utility::riempimento(lunghezza, '^') << endl;
        cout << ">" << Utility::riempimento(lunghezza - 2, ' ') << "<" << endl;
        for(int i = 0; i < opzioni.size() - 1; i++)
                cout << ">      " << i + 1 << " )   " << opzioni[i]
                     << Utility::riempimento(lunghezza - opzioni[i].size() - 14, ' ') << "<" << endl;
        cout << ">" << Utility::riempimento(lunghezza - 2, ' ') << "<" << endl;

        cout << ">      0 )   " << opzioni[opzioni.size() - 1]
             << Utility::riempimento(lunghezza - opzioni[opzioni.size() - 1].size() - 14, ' ')
             << "<" << endl;
        cout << ">" << Utility::riempimento(lunghezza - 2, ' ') << "<" << endl;
        cout << Utility::riempimento(lunghezza, '^') << endl;
        cout << endl;

        sceltaUtente.setDato(false);
    }
    while ( !valutaOpzione() );
}


int Menu::getSceltaUtente()
{
    return sceltaUtente.getDato();
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 9911482
From that piece of code I would look deeper into while (!valutaOpzione()). Does this end? How about sceltaUtente.getData and .setDato?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:zprestaz
ID: 9911538
Can you explain better please.......................
do you want another piece of code or what??
0
 
LVL 13

Expert Comment

by:SteH
ID: 9911617
At first I want to give you hints to find the error yourself. Only if this fails I might find the time to get into it more deeply. I suggest you to use a debugger to look at that line and whether it returns "true" at some time. For me knowledge of getDato and setDato prevent me from giving an answer.
0
 

Author Comment

by:zprestaz
ID: 9912131
the point is that in void ControlloreVeicoli::stampaListaVeicoli(vector<Veicolo *> v) the problem doesn't exist....................,but in void ControlloreClienti::stampaListaClienti(vector<Cliente *> c) yes.............
the two methods are so identic......................the problem exists only in runtime...........and in function void ControlloreClienti::stampaListaClienti(vector<Cliente *> c) i've added system("pause") and i've find that the command that block the program is sceltaCliente.setSceltaUtente();

GetDato() and SetDato() are methods of class CasellaInt (derivated from Casella class) that i attack below.These class take data on input and check this input

#include <cmath>
#include "Casella.h"

extern bool admin;

//messaggio della richiesta di input
void Casella::showMessage() {
    if(admin)
        cout << messaggioRichiesta << "  -#>  ";
    else
        cout << messaggioRichiesta << "  -@>  ";
}

//costruttore
Casella::Casella(string mr, string dd, string ec, string d)
{
      messaggioRichiesta = mr;
      defaultDato = dd;
      escapeChar = ec;
      dato = d;

}

//lettura dato da tastiera
void Casella::setDato(bool repeat=true)
{

  do{
    errorMsg=string();
    char buffer[200];        
    showMessage();
    cin.getline(buffer,200,'\n'); //fino al RETURN, max 200 char
    dato = buffer;

    if (dato=="") {
        dato = defaultDato;
        return;
    }
    else
      if (dato!=escapeChar) {
        isValid();         //chiama la funzione virtuale, ovvero quella di ogni classe specifica
        if( Error() && repeat )
        {
//          cout << "\a\nErrore: " << getError() << endl;
//          system("pause");
//          system("cls");
        }
        if(!repeat) return;
      }
  }
  while( repeat && Error() );    

}

bool Casella::Error() {
    return !errorMsg.empty();
}


string Casella::getError()
{
  return errorMsg;
}

// CasellaInt.cpp

#include <cmath>
#include "CasellaInt.h"


     
//controllo specifico per la suddetta classe
void CasellaInt::isValid()
{

  bool esito=true;
  errorMsg=string();
  int start = ( ( dato.at(0) == '-' ) ? 1 : 0 );   //da dove deve partire se c'è il segno meno
  for (int i=start; i<dato.size(); i++)
     if ( !isdigit( dato.at(i) ) )
        {
          errorMsg= "Il dato inserito non e' un numero intero";
          esito=false;
          break;
        }
  if (!esito)
     dato = errorDato;
 
}  
   

   
//costruttore


CasellaInt::CasellaInt(string mr, string dd, string ec, string ed="")
   : Casella(mr,dd,ec,"")
{
      errorDato = ed;
}

//restituzione dato come numero    
int CasellaInt::getDato()
{
  return atoi( dato.c_str() );    //conversione stringa vecchio stile C
}
     

//restituzione dato come stringa

string CasellaInt::getDatoString(){
  return dato;
}
0
 
LVL 13

Accepted Solution

by:
SteH earned 20 total points
ID: 9912245
sceltaCliente.setSceltaUtente(); is called twice but with different menus. Now you say it blocks: this could mean that a loop never ends and there is only one while and one for loop inside which could possibly not end. The for loop has a predefined number ofiterations so you're left with the while loop. But it could be another function call within this function. Without all code it's difficult to tell but I prefer that you try it yourself  and I give some guidance.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

810 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