PDA

View Full Version : Can anyone help me with this C code?



dj.turkmaster
2008-12-15, 21:42
Hello, I am computer science student in a university in Turkey. Well I need a little help with the code below.

#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?

PepiMK
2008-12-15, 23:21
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 ;)

dj.turkmaster
2008-12-19, 00:17
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:

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