2016-04-24 68 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; 
} 

编辑 这里是INT主要

+0

你的'main'函数是什么? –

+0

你可以添加一个'main()'你在哪里使用你的类? –

+0

完成,谢谢。 – 005197503

回答

0

不推荐使用goto并认为是一个非常不好的做法,即使是初学者。如果您是用C++开始的,遵循最佳做法是最好的开始。为了向后兼容,C/C++仅支持goto

为您的问题尝试使用循环,而不是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