user3020768
user3020768

Reputation: 11

Why won't scanf take user input for a structure?

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

Answers (3)

Denim Datta
Denim Datta

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 :

  1. Change in Traveller structure

    struct Traveller {
        int Ticket_ID;
        char* Destination;  // change from char to char*
        float Price;
    };
    
  2. 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;
    
    }
    
  3. Changes in CreateTicket function

    void CreateTicket(struct Traveller* ptr) {
        //Rest of the code
        scanf("%s",ptr->Destination);  // don't use &ptr->Destination
    }
    
  4. 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

Themis Beris
Themis Beris

Reputation: 990

You had some issues with printfs 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

VeganCreamPie
VeganCreamPie

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

Related Questions