Help With program

Hello, I am newbie in the course of computer science, was learning algorithms with VisualG and passed to C + +, I started making a list of exercises of 10 questions, but I was unable to develop practically nothing this last question, I believe that for me this time the degree of difficulty is high, the teacher recommended to seek help on forums and books the question is as follows:

10ยบ Write a program inventory that stores product information available. (int cod_prod, char name [20], float price, int quantity). For testing purposes, the program should store the information below from the keyboard. Data entry must end with a product code of zero.

example

1 TV 1000 5
2 RADIO 200 3
3 MICROWAVE 400 1


The system should have the functions of input, output. Consider using Struct.

You were given a very good clue.
Consider using Struct.

You can read about it here: http://www.cplusplus.com/doc/tutorial/structures/
structs are little different than classes and for the most part can be interchanged.
Another clue, input, output. So after you create your type, you have to overload std::istream::operator>> and std::ostream::operator<<.
So I got up here :/ , help please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdlib>
#include <iostream>

using namespace std;

struct stc_produto{
      int cod_prod; 
      char nome[20]; 
      float preco; 
      int quantidade; 
      }



stc_produto produto[50];
  
  

void menu (stc_produto prod){
  
  int opc;
  int i=1;
  
  printf("1- ENTRADA\n");
  printf("2- SAIDA\n"); 
  printf("3- SAIR\n"); 
  scanf("%i",opc);
  
  switch(opc){
              case 1:
                   while(i != 0){
          
          printf("Digite o codigo do produto ou 0(zero) para sair:\n");
          scanf("%d",prod[i].cod_prod);
          if (prod[i].cod_prod == 0){
             printf("Produto(s) Cadastrado(s) com Sucesso!");
             menu();
             }
             else{
                  printf("Digite o nome do produto:\n");
                  scanf("%d",prod[i].nome);
                  printf("Digite o preco do produto:\n");
                  scanf("%d",prod[i].preco);
                  printf("Digite a quantidade:\n");
                  scanf("%d",prod[i].quantidade);
                  i++;
                  }
             }
                   break;
              case 2:
                   printf("Digite o codigo do produto:\n");
                   scanf("%i",cod);
      
                   while i<=50 do{
            
                   if (cod == prod[i].cod_prod){
                           
                               
                  printf("Digite a quantidade:\n");
                  scanf("%d",prod[cod].quantidade);
                  
                  menu();
                  
                                    
             }  else
                    i++;  
                    
                    }        
                   break;
              case 3:
                   system("PAUSE");
                   break;
}
  
}

int main(void)
{
  
  menu();
  
    
    
    
    
}
Line 11: missing ;

Line 19: You need to pass prod as an array:
 
void menu (stc_produto prod[])


Line 37,80: menu requires an argument.

Line 52: When is cod defined?

Line 54: while needs () around the condition. do is extraneous.
Last edited on
closed account (j3Rz8vqX)
You're using c libraries, exclude the cpp input/output stream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdio>//Required to used c style, input/output
//#include <cstdlib>//Required for use of system call

struct stc_produto
{
      int cod_prod;
      char nome[20];
      float preco;
      int quantidade;
};

stc_produto produto[50];

void menu (stc_produto prod[]){

    int opc, cod;
    int i=1;//Why start at 1 for?
    bool sair=false;

    do
    {
        printf("1- ENTRADA\n");
        printf("2- SAIDA\n");
        printf("3- SAIR\n");
        scanf("%i",&opc);
        switch(opc){
            case 1:
                while(true)
                {
                    printf("Digite o codigo do produto ou 0(zero) para sair:\n");
                    scanf("%d",&prod[i].cod_prod);
                    if (prod[i].cod_prod == 0){
                        printf("Produto(s) Cadastrado(s) com Sucesso!");
                        break;
                    }
                    else{
                        printf("Digite o nome do produto:\n");
                        scanf("%19s",&prod[i].nome);
                        fflush(stdin);//Handles overflow from scanf-buffer
                        printf("Digite o preco do produto:\n");
                        scanf("%f",&prod[i].preco);
                        printf("Digite a quantidade:\n");
                        scanf("%d",&prod[i].quantidade);
                        i++;
                    }
                }
                break;
            case 2://Not sure what this procedure is attempting, I do not understand the language.
                printf("Digite o codigo do produto:\n");
                scanf("%i",&cod);

                for(int j=0;j<50;j++)//I've modified, because I do not believe you want to modify the value of i; j would be temporary.
                {
                    if (cod == prod[j].cod_prod){
                        printf("Digite a quantidade:\n");
                        scanf("%d",&prod[j].quantidade);
                        break;
                    }
                }
                break;
            case 3:
                //system("PAUSE");
                printf("Press enter to continue...");
                fflush(stdin);//Handles overflow from buffer
                getchar();
                fflush(stdin);//Handles overflow from buffer
                sair=true;
        }
    }while(!sair);
}
int main(void)
{
  menu(produto);
  return 0;
}


This is an example of your code, correctly implemented.
Edit: Edited to better interpret your intentions.

Learning tips:
Scanf always seek the address of the variables:
scanf("%i", &myInt);

Recursive function calling isn't bad, however, you had an infinite loop in case 1, because "i" would never be equal 0 and it would be less expensive to simply loop your code, reusing variables and such, rather than stacking multiples.
Last edited on
Topic archived. No new replies allowed.