Hi everyone! My name is Ricardo and i'm having a few problems with a program i'm writing.
The program should deal with products information, i'm doing it using a structure, two procedures and a main function, the purpose is so that in the first function, designation of the products should be entered, qauntity and price, the second should take that data and process it giving the full price of all products, tax per product and an average per product, i'm also trying to do it with a menu using switch.
The code i've already written is as follows:

//programa que lista 10 produtos
#include<iostream>
#include<cstdio>
using namespace std;

struct Produtos
{
    char designacao[20];
    int quantidade;
    int quant_total;
    float preco;
    float preco_total;
    float preco_iva;
    float media;
    int total_produtos;
}P[10];

void prod_receber(Produtos *R)
{
        cout << "Introduza Designacao:" << endl;
        gets(R->designacao);
        cout << "Introduza Quantidade do Produto:" << endl;
        cin >> R->quantidade;
        cout << "Introduza o preco de Compra:" << endl;
        cin >> R->preco;
}

void prod_listar(Produtos *L, int i)
{
    
    cout << "Desigação do Produto:";
    gets(L->designacao);
    cout << "Quantidade:";
    cout << L->quantidade << endl;
    cout << "Preco Compra:";
    cout << L->preco << endl;
    L->preco_iva = (L->quantidade*L->preco)*1.21;
    cout << L->preco_iva << endl;
    for (i=0; L[i]<=9; i++)
    {
        L->preco_total = L->preco_total + preco;
    }
    cout << L->preco_total;
    cout << "Valor Medio de Todos os Produtos:";    
    L->media = preco_total / 10;
    cout << L->media << endl;
}

main()
{
    Produtos q[10], *T=&q[10];

    int menu;
    int i;

    cout << "Escolha uma das Opcoes;" << endl;
    cout << "1 - Introduza Produto;" << endl;
    cout << "2 - Por Produto o seu valor com IVA(21%);" << endl;
    cout << "3 - Valor Gasto na Aquisicao de todos os Produtos;" << endl;
    cout << "4 - Qual o valor Médio de todos os Produtos;" << endl;
    cout << "5 - Sair do Programa;" << endl;
    cout << "Introduza Opção:";
    cin >> menu;
    switch(menu)
    {
        case 1:
            prod_receber(T[i]);
            break;
        case 2:
            cout << "Introduza Posição do Produto:";
            cin >> T[i];
            prod_listar(T->preco_iva);
            break;
        case 3:
            prod_listar(T->preco_total);
            break;
        case 4:
            prod_listar(T->media);
            break;
        case 5:
            return 0;    
    }
}

Can someone help me see whats wrong in the program? Thanks.

Ricardo,

So what happens when you try to compile it?

There's quite a number of bugs and syntax errors in your code that I can see, so I'd say you've got a lot of debugging ahead of you...

For example:

cin >> T[i];

What in the world is that supposed to do? You're just trying to input something into the OBJECT, which you can't do unless you've overloaded the >> operator (and that is something fairly complex). You'll have to specify which variable in the object you want the input to go in...

Also, i was never initalized, so it's going to contain random junk which you're using for your array subscript... bad, bad. You should at least have a loop for your main menu so that the user can enter more than one item.

Hello again.
I've managed to do a few modifications, and know the program compiles, but it doesn't work properly. When i select the first option it prints a line of garbage, when i select the second option the program crashes! The third seems to work, i can't test it properly until i get the first two working, the rest of the options work, but then again there more simple.
This is the code at the moment, i'll apreciate any help that anyone can give me, thanks :

//programa que lista 10 produtos
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;

struct Produtos
{
    char designacao[20];
    double quantidade;
    double quant_total;
    double preco;
    double valor_total;
    double preco_iva;
    double media;
    double total_produtos;
};

Produtos P[10];

inline void prod_receber(Produtos *R)
{
        cout << "Introduza Designacao:" << endl;
        gets(R->designacao);
        cout << "Introduza Quantidade do Produto:" << endl;
        cin >> R->quantidade;
        cout << "Introduza o preco de Compra:" << endl;
        cin >> R->preco;
}

inline void prod_listar(Produtos *L)
{
    
    cout << "Desigação do Produto:";
    gets(L->designacao);
    cout << "Quantidade:";
    cout << L->quantidade << endl;
    cout << "Preco Compra:";
    cout << L->preco << endl;    
}

int main()
{
    Produtos q[10], *T=&q[10];

    int menu;
    int i;

    cout << "Escolha uma das Opcoes;" << endl;
    cout << "1 - Introduza Produto;" << endl;
    cout << "2 - Por Produto o seu valor com IVA(21%);" << endl;
    cout << "3 - Valor Gasto na Aquisicao de todos os Produtos;" << endl;
    cout << "4 - Qual o valor Médio de todos os Produtos;" << endl;
    cout << "5 - Sair do Programa;" << endl;
    cout << "Introduza Opção:";
    cin >> menu;
    switch(menu)
    {
        case 1:
            for (i=0; i<=9; i++)
            {
                cout << prod_receber;
            }
            
            break;
        case 2:
            cout << "Introduza Posição do Produto:";
            cin >> i ;
            T[i].preco_iva = (T[i].quantidade*T[i].preco)*1.21;
            cout << T[i].preco_iva;
            break;
        case 3:
            for (i=0; i<=9; i++)
                {
                    T->quant_total = T->preco * T->quantidade ; 
                    T->valor_total = T->quant_total + T->preco;
                }
                cout << T->valor_total;
            break;
        case 4:
            cout << "Valor Medio de Todos os Produtos:";    
            T->media = T->valor_total / 10;
            cout << T->media << endl;
            break;
        default:
            return 0;    
    }
}

So what happens when you try to compile it?

There's quite a number of bugs and syntax errors in your code that I can see, so I'd say you've got a lot of debugging ahead of you...

For example:

cin >> T[i];

What in the world is that supposed to do? You're just trying to input something into the OBJECT, which you can't do unless you've overloaded the >> operator (and that is something fairly complex). You'll have to specify which variable in the object you want the input to go in...

Also, i was never initalized, so it's going to contain random junk which you're using for your array subscript... bad, bad. You should at least have a loop for your main menu so that the user can enter more than one item.

Well, one thing I noticed is this section of your main menu:

switch(menu)
    {
        case 1:
            for (i=0; i<=9; i++)
            {
                cout << prod_receber; // can't call the function like this
                                      // need () and arguments
                                      // plus the function returns void, so
                                      // there's nothing to print
            }

Also you need to find out why gets is bad and what you should do instead. Besides, it's a bad idea to mix C and C++ input functions anyway.

This article has been dead for over six months. Start a new discussion instead.