Solved

Error on method of Menu Class

Posted on 2003-12-10
9
242 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
Comment Utility
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
Comment Utility
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
Comment Utility
From that piece of code I would look deeper into while (!valutaOpzione()). Does this end? How about sceltaUtente.getData and .setDato?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Expert Comment

by:SteH
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.

743 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

8 Experts available now in Live!

Get 1:1 Help Now