Thread: Need help in a specific code!!

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    4

    Need help in a specific code!!

    Hi. I am new here and barely new at C programming too. I need to write a code, the main objective is the a program that manage the catalog of books sold by an online book store in electronic format.

    To do this, i must use the methodology described below.

    The BookWeb online store wants an application to keep track of its book catalog in a format
    electronic. For the management of all information, and according to the possibilities to include, it is suggested (not
    required) the following structure, although incomplete:
    • Book data
    ◦ Title
    ◦ Author (s)
    ◦ ISBN
    ◦ Editor
    ◦ eBook format: ePUB, mobi, pdf, others
    ◦ Thematic Classification and possible subcategories (see below)
    ◦ List of other related author books
    ◦ Other related books by other authors
    ◦ Reader ratings (0 to 5 stars)
    ◦ Price and year of publication
    ◦ Units sold
    • Bestseller list
    • List of news
    • List of promotions

    I already wrote a bit of the code, but it haves many errors. I know im asking a significant amount of work.. Thank you for your attention.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    > I already wrote a bit of the code, but it haves many errors.
    Well that's a good place to start.
    Post what you have, then we can help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2020
    Posts
    4
    My code is in portuguese (printfs and the names of the functions ), you want me to send like that?? Or translate it all to english? TY.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Well you can post as is for now.
    But generally, if you're posting on an English speaking forum, you should write your code in English to ensure that the majority can read it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2020
    Posts
    4
    I wrote comments on the side to help understand what i meant to do with the functions. I need a function for the Thematic classification and subcategories. I have to put a lot of subcategories and i dont know how to do it.



    Code:
    #include <stdio.h>
    #include <math.h>
    #include <locale.h> //para usar o setlocale(LC_ALL, "Portuguese");
    #include <ctype.h>
    
    
    char formato(){  // to validate the eBook format
    	char f;
    	
    	printf("Formato do eBook?");
    	f = toupper(getchar());
    	
    	while( f!= 'ePUB' || f != 'mobi' || f != 'pdf' || f != 'outros' ){
    		printf("Invalido. Digite novamente por favor.");
    		f = toupper(getchar());
    	}
    	
    	return f;
    }
    
    
    int ClassificacaoEst(int linf, int lsup){  // to validate the star classification
    	int x;
    	
        printf("Introduza um X ");
        scanf("%d", &x);
            
        while (x < linf || x > lsup) {
    	printf("Invalido. Introduza um novo valor X ");
    	scanf("%d", &x);
        }
            
        return x;
    }
    
    
    char lerchar(){   // to read words or sentences, for example to read the authors of the book
    	char x;
    	
    	printf("Introduza um X ");
    	x = getchar();
    	return x;
    }
    
    
    float numero(){  // to read a number
    	float x;
    	
    	printf("Introduza um X");
    	scanf("%f", &x);
    	
    	while (x < 0) {
    		printf("Invalido. Introduza um novo valor X ");
    		scanf("%f", &x);
        }
    	return x;
    }
    
    
    float preço_e_ano(){ // to read the price and year of publication
    	float p, a;
    	
    	printf("Introduza o preço e ano de publicação!");
    	scanf("%f%f", &p, &a);
    	return p, a;
    }
    
    
    char ClassTem(){
    	
    }
    
    
    void main(){
    	
    	setlocale(LC_ALL, "Portuguese");
    	
    	int c;
    	char t, a, e, f, l, a1;
    	float i, unid, pea;
    	
    	printf("Qual o título da obra?");
    	t = lerchar();
    	
    	printf("Qual o Autor/Autores?");
    	a = lerchar();
    	
    	printf("Qual o ISBN?");
    	i = numero();
    	
    	printf("Qual o editor?");
    	e = lerchar();
    	
    	printf("Qual o formato do eBook?");
    	f = formato();
    	
    	printf("Classificação Temática e possíveis subcategorias?");
    	
    	
    	printf("Lista de outros livros do autor relacionados?");
    	l = lerchar();
    	
    	printf("Outros livros relacionados de outros autores?");
    	a1 = lerchar();
    	
    	printf("Classificação dos leitores?");
    	c = ClassificacaoEst(0,5);
    	
    	printf("Preço e ano de publicação?");
    	pea = preço_e_ano();
    	
    	printf("Unidades vendidas?");
    	unid = numero();
    
    
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,630
    Code:
    char formato(){  // to validate the eBook format
    	char f;
    	
    	printf("Formato do eBook?");
    	f = toupper(getchar());
    	
    	while( f!= 'ePUB' || f != 'mobi' || f != 'pdf' || f != 'outros' ){
    		printf("Invalido. Digite novamente por favor.");
    		f = toupper(getchar());
    	}
    	
    	return f;
    }
    Your compiler should be complaining about several things in this code:

    Code:
    main.c||In function ‘formato’:|
    main.c|13|warning: multi-character character constant [-Wmultichar]|
    main.c|13|warning: comparison is always true due to limited range of data type [-Wtype-limits]|
    main.c|13|warning: multi-character character constant [-Wmultichar]|
    main.c|13|warning: comparison is always true due to limited range of data type [-Wtype-limits]|
    main.c|13|warning: multi-character character constant [-Wmultichar]|
    main.c|13|warning: comparison is always true due to limited range of data type [-Wtype-limits]|
    main.c|13|warning: character constant too long for its type|
    main.c|13|warning: comparison is always true due to limited range of data type [-Wtype-limits]|
    In C the ' ' are used for variables with a single character, for multiple characters you need arrays of char, or C-strings. And remember that you can not compare arrays or C-strings with the comparison operators, you will either need to check each character or if using C-strings you will need to use strcmp().

  7. #7
    Registered User
    Join Date
    Dec 2020
    Posts
    4
    The rest of the code looks correct?
    How would you do that function by "checking each character"?
    Can you also help how to make a function that reads several ( like f.e. 10-15) subcategories?
    TY.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,630
    The rest of the code looks correct?
    Not really, but the stated issues are probably the biggest issues. For example main() should return an int, not void, and you have several unused variables in main() that your compiler should also be able to tell you about.

    How would you do that function by "checking each character"?
    Well you would need to start with an array of char instead of a single character. Then loop through the user inputted value checking if each character matches one of your "categories", repeat the loop for each "category" until you either check each "category" or you find a match. But really using C-strings instead would be much easier and probably much quicker.

    Can you also help how to make a function that reads several ( like f.e. 10-15) subcategories?
    I'd recommend you fix the current issues before you start adding more "problems.

    Edit: And hopefully the meaningless one character variable names were a result of the changes to "Anglicize" the code, if not you need to start using more meaningful names.
    Last edited by jimblumberg; 12-05-2020 at 01:31 PM.

  9. #9
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Must be in a very good mood today. Here's a gift:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int formato(char *buffer, size_t maxlen){  // to validate the eBook format
        if(maxlen < 7) {
           printf("Buffer is too small\n");
        }
        while(1) {
           printf("Formato do eBook? ");
           // Read at most 6 characters
           if(scanf("%6s",buffer) != 1) {
              printf("Error on input\n");
              return 0;
           }
    
    
           if(strcmp(buffer,"ePUB") == 0) 
              return 1; 
    
    
           if(strcmp(buffer,"pdf") == 0) 
              return 1; 
    
    
           if(strcmp(buffer,"mobi") == 0) 
              return 1; 
    
    
           if(strcmp(buffer,"outros") == 0) 
              return 1; 
    
    
           printf("bad format type\n");
        }
    }
    
    
    int main(int argc, char *argv[]) {
       char buffer[100];
       if(formato(buffer,sizeof(buffer))) {
          printf("Got '%s'\n",buffer);
       } else {
          printf("Failed\n");
       }
    }
    It will need a bit of tidying up, but should provide a working example of reasonably bullet-proof way to get input.
    Last edited by hamster_nz; 12-05-2020 at 03:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Gravity] General Advice on Specific Code
    By M.Richard Tober in forum Game Programming
    Replies: 5
    Last Post: 01-10-2012, 06:19 PM
  2. Replies: 12
    Last Post: 01-15-2009, 10:04 AM
  3. Run code on specific CPU?
    By pgzh in forum C Programming
    Replies: 3
    Last Post: 03-21-2008, 02:14 PM
  4. specific length
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 07:34 PM
  5. Replies: 3
    Last Post: 01-26-2002, 08:39 PM

Tags for this Thread