CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2014
    Posts
    2

    Help In Last Question please

    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.

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Help In Last Question please

    Study the following topics, and you will be able to solve the exercise:

    * Text input/output using cin/cout.
    * class/struct for storing the data of each object
    * containers (such as std::vector) for storing the set of objects
    * control flow (loops based on while/for/do..while)

    Regards
    Nobody cares how it works as long as it works

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Help In Last Question please

    I would suggest that before you start writing code that you first design the program - ie describe in English (or other natural language) how the program will work. Once you have the design, then code the program from the design.

    Code:
    define input requirements
    define output requirements
    detail any required algorithms (eg average etc)
    detail any required data structures
    do {
         produce program design
         produce pseudo-code if required or necessary
         code the progam from the design/pseudo-code
         test the program
         debug the program
    } while program_not_working
    For help with c++, you might find these sites useful
    http://www.learncpp.com/
    http://www.cplusplus.com/doc/tutorial/
    http://www.tutorialspoint.com/cplusplus/index.htm

    Once you have some code written, if you post it here then we'll be able to offer further guidance.

    Good luck!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    May 2014
    Posts
    2

    Re: Help In Last Question please

    So I got up here :/ , help please?


    Code:
    #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();
      
        
        
        
        
    }

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Help In Last Question please

    Quote Originally Posted by MaryKerry View Post
    So I got up here :/ , help please?
    What problem do you have with this code?
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Help In Last Question please

    Are you coding in c or c++ - as the way you have coded input/output is c-type code and not the c++ way of doing it. In c++ you would usually use the cin/cout streams for input and output. See http://www.cplusplus.com/reference/iostream/

    Also you have menu() calling menu() if the entered product code is 0 which is probably not what you want. You probably want a loop iteration rather than recursion?

    Also rather than using char arrays (eg name[20]) it would be more usual in c++ to use the STL class string http://www.cplusplus.com/reference/string/

    What further help do you want with this code?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured