Can anyone help me with this C code?

dj.turkmaster

New member
Alpha Testers
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(); [COLOR="Green"]//Skip this line[/COLOR]
void search_dvd(); [COLOR="Green"]//Skip this line[/COLOR]
void list_dvd(); [COLOR="Green"]//Skip this line[/COLOR]
void edit_dvd(); [COLOR="Green"]//Skip this line[/COLOR]
void hire_dvd(); [COLOR="Green"]//Skip this line[/COLOR]
void quit(); [COLOR="Green"]//Skip this line[/COLOR]



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);
}	

[COLOR="Green"][B]//The problem starts here[/B][/COLOR]

printf("Please enter the name of the dvd:");
gets(dvd[serial].name);   [COLOR="Green"] // It doesn't get the name =([/COLOR]
printf("\nPlease enter the price of the dvd:");
scanf("%d", &dvd[serial].price);   [COLOR="Green"] // It gets the price, no problem[/COLOR]
printf("Please enter the director of the dvd:");
gets(dvd[serial].director); [COLOR="Green"] // It doesn't get the dircetor here =(([/COLOR]
printf("\nPlease enter the genre of the dvd:");
gets(dvd[serial].genre);  [COLOR="Green"] // It gets the genre, no problem[/COLOR]
printf("%d\n", dvd[serial].price);[COLOR="Green"] //These lines also works[/COLOR]
puts(dvd[serial].genre);
}

Any idea why it doesn't get the director name and the name of the dvd?
 
Last edited:
gets is evil :fear: 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 ;)
 
gets is evil :fear: 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:oops:
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++;
}
 
Back
Top