Reputation: 11
I have a code with two functions for a school assignment. We've just learned about structures. Both functions must take a pointer to a structure as a parameter. One function is supposed to take in user input to change the values in the structure, and the other is supposed to print out the contents of the structure. Scanf or gets() will not work for me in my first function. I've consulted the chapter in the book and the class notes with no luck. Here is my code:
#include<stdio.h>
#include<stdlib.h>
struct Traveller {
int Ticket_ID;
char Destination;
float Price;
};
void CreateTicket(struct Traveller*);
void PrintTicket(struct Traveller*);
main() {
struct Traveller * str;
CreateTicket(str);
PrintTicket(str);
system("PAUSE");
return 0;
}
void CreateTicket(struct Traveller* ptr) {
printf("Please enter your ticket ID.\n");
scanf("%d", &ptr->Ticket_ID);
printf("\n\nPlease enter your destination.\n");
gets(ptr->Destination);
printf("\n\nPlease enter the price.\n");
scanf("%f", &ptr->Price);
}
void PrintTicket(struct Traveller* ptr) {
printf("\n\n%s\n", ptr->Ticket_ID);
printf("%s\n", ptr->Destination);
printf("%.2s\n", ptr->Price);
}
Upvotes: 1
Views: 3380
Reputation: 3802
You need to allocate memory for the Structure pointer ptr. And change Destination from char Destination
to char* Destination
Make the following changes :
Change in Traveller structure
struct Traveller {
int Ticket_ID;
char* Destination; // change from char to char*
float Price;
};
Changes in main function
int main(void) {
// Changes Here (allocate memory)
struct Traveller * str = (struct Traveller *)malloc(sizeof(struct Traveller));
str->Destination = (char *)malloc(sizeof(char) * 10); // for 9 char + null
memset(str->Destination, 0, 10);
// Changes Over
CreateTicket(str);
PrintTicket(str);
return 0;
}
Changes in CreateTicket function
void CreateTicket(struct Traveller* ptr) {
//Rest of the code
scanf("%s",ptr->Destination); // don't use &ptr->Destination
}
Changes in PrintTicket function
void PrintTicket(struct Traveller* ptr) {
printf("\n\n%d\n", ptr->Ticket_ID); // %s to %d
printf("%s\n", ptr->Destination);
printf("%.2f\n", ptr->Price); // %s to %f
}
==Edit (About memset)==
void *memset(void *str, int c, size_t n)
copies the character c (an unsigned char) to the first n characters of the string pointed to by the argument str.
memset(str->Destination, 0, 10);
will put NULL for 10 positions
//Change the following
memset(str->Destination, 0, 10); //not memset(str->Destination, 10, 0);
Upvotes: 0
Reputation: 990
You had some issues with printf
s type you tried to print ,main
was not declared as it should be, and gets
.
This is the right code :
#include<stdio.h>
#include<stdlib.h>
struct Traveller {
int Ticket_ID;
char Destination;
float Price;
};
void CreateTicket(struct Traveller*);
void PrintTicket(struct Traveller*);
int main(void) {
struct Traveller * str;
CreateTicket(str);
PrintTicket(str);
return 0;
}
void CreateTicket(struct Traveller* ptr) {
printf("Please enter your ticket ID.\n");
scanf("%d", &ptr->Ticket_ID);
printf("\n\nPlease enter your destination.\n");
scanf("%s",&ptr->Destination);
printf("\n\nPlease enter the price.\n");
scanf("%f", &ptr->Price);
}
void PrintTicket(struct Traveller* ptr) {
printf("\n\n%d\n", ptr->Ticket_ID);
printf("%s\n", &ptr->Destination);
printf("%.2f\n", ptr->Price);
}
Upvotes: 0
Reputation: 66
First of all, you'll need a return value for main - int main ()
.
It will help if you initialize the struct to NULL
before passing it into the functions.
In gets()
you need to give the address (&
) to your struct variable.
Hope that helps! I found some other issues with the code but i think it's better for you to figure them out yourself :)
Upvotes: 1