2016-04-24 10 views
-1

私はこの新しいコードを持っていますが、データを印刷しようとすると文字列変数が決して印刷されません。 これはちょうど のためのものです。私は実践的なリアーソンのために "goto"を使用しています。文字列は印刷されませんC++

#include <iostream> 
    #include <string> 
    #include <cstdlib> 
    using namespace std; 

    class producto 
    { 
    public: 
     int id; 
     string nombre; 
     string descripcion; 
     int precio; 
     void registrar(); 
     void ver(); 
    }; 
    void producto::registrar() 
    { 
     cout << "Codigo:" << endl; 
     cin >> id; 
     cin.ignore(); 
     cout << "Nombre del producto:" << endl; 
     getline(cin, nombre); 
     cout << "Descripcion del producto:" << endl; 
     getline(cin, descripcion); 
     cout << "Precio:" << endl; 
     cin >> precio; 

    } 
    void producto::ver() 
    { 
     cout << "ID del producto:"; 
     cout << id << endl; 
     cout << "Nombre del producto:" << endl; 
     cout << nombre; 
     cout << "Descripcion del producto:"; 
     cout << descripcion<<endl; 
     cout << "Precio:"; 
     cout << "$" << precio << endl; 

    } 
int main() 
{ 
menu1: 
    int menu; 
    producto cosa; 
    cout << "************************" << endl; 
    cout << "1.- Registrar Producto" << endl; 
    cout << "2.- Ver Producto" << endl; 
    cout << "************************" << endl; 
    cin >> menu; 
    cin.ignore(); 
    switch (menu) 
    { 
    case 1: 
     cout << "INGRESE PRODUCTO NUEVO:\nPresione enter para continuar" << endl; 
     cin.ignore(); 
     system("cls"); 
     cosa.registrar(); 
     cin.ignore(); 
     break; 
    case 2: 
     cosa.ver(); 
     cout << "Presione enter para regresar al menu principal." << endl; 
     cin.ignore(); 
     break; 

    } 
    goto menu1; 
    return 0; 
} 

編集 ここgotoの使用はint型メイン

+0

あなたの 'main'関数は何ですか? –

+0

クラスを使用する場所で 'main()'を追加できますか? –

+0

完了、ありがとうございます。 – 005197503

答えて

0

で推奨されており、初心者でものための非常に悪い習慣とはみなされません。 C++で始める場合は、ベストプラクティスに従うことが最善の方法です。 gotoは下位互換性のためにのみC/C++でサポートされています。

問題のために、gotoの代わりにループを使用してみてください。

int main() 
{ 
    //Condition to show the menu or exit 
    bool bContinue = true; 
    producto cosa; 

    do{ 

     int menu; 
     cout << "************************" << endl; 
     cout << "1.- Registrar Producto" << endl; 
     cout << "2.- Ver Producto" << endl; 
     cout << "3.- Exit" << endl; 
     cout << "************************" << endl; 
     cin >> menu; 
     cin.ignore(); 

     switch (menu) 
     { 
     case 1: 
      cout << "INGRESE PRODUCTO NUEVO:\nPresione enter para continuar" << endl; 
      cin.ignore(); 
      system("cls"); 
      cosa.registrar(); 
      cin.ignore(); 
      break; 
     case 2: 
      cosa.ver(); 
      cout << "Presione enter para regresar al menu principal." << endl; 
      cin.ignore(); 
      break; 
     case 3: 
      bContinue = false; 
      break; 
     } 

    }while(bContinue) 
    return 0; 
} 

このように、問題は修正され、より良い方法を学習します。

+0

ありがとう、私たちはまだそれらを使用することはできないと仮定しているので、ループではありませんでした。 – 005197503

関連する問題