Results 1 to 3 of 3

Thread: Can anyone help me with this C code?

  1. #1
    Senior Member dj.turkmaster's Avatar
    Join Date
    Feb 2007
    Location
    TURKEY/Ankara
    Posts
    139

    Default Can anyone help me with this C code?

    Hello, I am computer science student in a university in Turkey. Well I need a little help with the code below.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct dvd_type{
    		int price;
    		char name[21];
    		char genre[9];
    		char director[19];
    		char state[5];
    	} dvd[500];
    	
    void add_dvd(struct dvd_type[]);
    
    void remove_dvd(); //Skip this line
    void search_dvd(); //Skip this line
    void list_dvd(); //Skip this line
    void edit_dvd(); //Skip this line
    void hire_dvd(); //Skip this line
    void quit(); //Skip this line
    
    
    
    int main() {
    	struct dvd_type dvd[500];
    	char choice;
    	printf(" ---HUBM DVD--- \nA:   Add new dvd\nR:   Remove dvd\nS:   Search dvd\nL:   List dvd\nE:   Edit dvd\nH:   Hire dvd\nQ:   Quit\nEnter command:");
    	scanf("%c", &choice);
    	if (choice == 'A'){
    		add_dvd(dvd);
    	}/*
    	else if (choice == 'R'){
    		remove_dvd();
    	}
    	else if (choice == 'S'){
    		search_dvd();
    	}
    	else if (choice == 'L'){
    		list_dvd();
    	}
    	else if (choice == 'E'){
    		edit_dvd();
    	}
    	else if (choice == 'H'){
    		hire_dvd();
    	}
    	else if (choice == 'Q'){
    		quit();
    	}*/
    	else {
    		printf("ERROR! Please enter one of these letters: A R S L E H Q");
    	}
    	
    	return 0;
    }
    void add_dvd(struct dvd_type dvd[]){
    int serial;
    printf("Please enter the serial number of the dvd you want to add.\nSerial:");
    scanf("%d", &serial);
    while (serial>500 & serial<=0){
    	printf("The serial should be between 1 and 500. Please enter a new serial");
    	scanf("%d", &serial);
    }	
    
    //The problem starts here
    
    printf("Please enter the name of the dvd:");
    gets(dvd[serial].name);    // It doesn't get the name =(
    printf("\nPlease enter the price of the dvd:");
    scanf("%d", &dvd[serial].price);    // It gets the price, no problem
    printf("Please enter the director of the dvd:");
    gets(dvd[serial].director);  // It doesn't get the dircetor here =((
    printf("\nPlease enter the genre of the dvd:");
    gets(dvd[serial].genre);   // It gets the genre, no problem
    printf("%d\n", dvd[serial].price); //These lines also works
    puts(dvd[serial].genre);
    }
    Any idea why it doesn't get the director name and the name of the dvd?
    Last edited by dj.turkmaster; 2008-12-15 at 21:44.
    DOCTUS.ORG Turkish security forum

  2. #2
    Member of Team Spybot PepiMK's Avatar
    Join Date
    Oct 2005
    Location
    Planet Earth
    Posts
    3,601

    Default

    gets is evil No buffer length checking, typical C problem leading to buffer overflows = security holes or at least crashes. fgets might be the nearest replacement. Unless you need to use C and not C++, I would probably use cin/cout.

    Btw, why do you mix puts/gets and scanf/printf? Why not stick with the later for the text as well, with checking its return value of course and specifying the length (buffer minus 1!)?

    Anythings better than gets I know that doesn't answer your problem, but I had to say it
    Just remember, love is life, and hate is living death.
    Treat your life for what it's worth, and live for every breath
    (Black Sabbath: A National Acrobat)

  3. #3
    Senior Member dj.turkmaster's Avatar
    Join Date
    Feb 2007
    Location
    TURKEY/Ankara
    Posts
    139

    Default

    Quote Originally Posted by PepiMK View Post
    gets is evil No buffer length checking, typical C problem leading to buffer overflows = security holes or at least crashes. fgets might be the nearest replacement. Unless you need to use C and not C++, I would probably use cin/cout.

    Btw, why do you mix puts/gets and scanf/printf? Why not stick with the later for the text as well, with checking its return value of course and specifying the length (buffer minus 1!)?

    Anythings better than gets I know that doesn't answer your problem, but I had to say it
    First thank you for yur reply PepiiMK. But actually I couldn't understand what you meant at the 2.paragraph, so I can't answer your question, sorry
    It looks like using scanf and gets together causes some problems. I have overcomed of the problem by adding this code:
    Code:
    printf("Please enter the name of the dvd: %s");
    while(i<3){
    a=gets(dvd[serial].name);
     if(i==1 && a[0]==0);
     
     else if(i==2&&a[0]==0)
      break;
    i++;
    }
    DOCTUS.ORG Turkish security forum

Posting Permissions

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