I need to register several products

Asked

Viewed 264 times

3

I am creating a Product registration code, but I can only store a single product. How to improve the code?

#include<iostream>
using namespace std;

void novoProduto(){
    string nome="Patrick Oliveira de Souza";
    float custo, venda;

    system("cls");
    cout<<"\tCadastro de Produto\n\n\n";
    cout<<"\nNome do Produto: ";
    cin>>nome;
    cout<<"\nPreco de Custo: ";
    cin>>custo;
    cout<<"\nPreco de Venda: ";
    cin>>venda;
}

int main()
{
    int op;
    cout<<"\tCadastro de Produto\n\n\n";
    cout<<"\n<1>Novo Produto";
    cout<<"\n<2>Listar Produtos";
    cout<<"\n\tOpcao: ";
    cin>>op;

    switch (op){
        case 1:
            novoProduto();
            break;

        /*case 2:
            break*/


    }
    return 0;
}
  • Did the answer solve your problem? Do you think you can accept it now? See [tour] to understand how it works. It would be helpful to indicate to everyone that the solution was useful and satisfactory for you. You can also vote on any question or answer you find useful on the entire site.

1 answer

3

A very basic way to add this functionality:

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

class Produto { //esta é uma forma bem simplificada
    public:
        string Nome;
        float Custo; //em código real não pode usar float para valor monetário
        float Venda;
};
    
void novoProduto(vector<Produto> produtos) { //esta é uma forma bem ingênua de fazer isto
    produtos.push_back(Produto());
    cout << "\tNovo Produto\n";
    cout << "\nNome do Produto: ";
    cin >> produtos[produtos.size() - 1].Nome;
    cout << "\nPreco de Custo: ";
    cin >> produtos[produtos.size() - 1].Custo;
    cout << "\nPreco de Venda: ";
    cin >> produtos[produtos.size() - 1].Venda;
}

int main() {
    vector<Produto> produtos;
    int op;
    do {
        cout << "\n\tCadastro de Produto\n";
        cout << "\n<1> Novo Produto";
        cout << "\n<2> Listar Produtos";
        cout << "\n\tOpcao: ";
        cin >> op;
        system("cls");
        switch (op) {
            case 1:
                novoProduto(produtos);
                break;
        }
    } while (op != 0);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that there have been two major changes. I created a vector to store the various products and created a class to group the data of a product. I made a series of cosmetic changes and added a loop to allow several actions in the same execution.

I made several simplifications, in real code a lot of things should be done differently.

  • when I register a product and put the name of the product with space it does not work. for example: test cell. Another thing is sale value and cost if I put 1.50. The program does not accept even changing the variable to double.

  • This is already another problem. The cin is not suitable for texts in general. See this: http://answall.com/a/42338/101

Browser other questions tagged

You are not signed in. Login or sign up in order to post.