I am trying to take a user's input and assign it to a certain part of a structure. This code is in my book so I assume it's correct:

scanf("%d", &part_number);
inventory[num_parts].number = part_number;

I need to do the same thing but with a string instead of a number, but I get incompatible types in assignment error.

scanf("%s", &part_name);
inventory[num_parts].name = part_name;

How could I accomplish this?

Once you have scanned the input in you need to use strcpy() to copy the string from the variable to the data structure variable.

strcpy(inventory[num+parts].name, part_name];
commented: Thank you! +1

Thank you my friend. I will give it a go.

We can't correctly solve your problem without more information. Post the definitions of your variables and we'll see.

I tried the above.. I get "incompatible implicit deceleration of built-in function strcpy"

scanf("%s", &part_name)
strcpy(inventory[num_parts].name, part_name);

I tried the above.. I get "incompatible implicit deceleration of built-in function strcpy"

scanf("%s", &part_name)
strcpy(inventory[num_parts].name, part_name);

Post the whole program so that we can take a look.

What you suggested above worked, thank you. Problem was I didn't #include<string.h>...
New problems now (see comments). Here is entire code so far. Pretty sure it is a dumb mistake.

#include <stdio.h>
#include <string.h>
//#include "realine.h"

#define NAME_LEN 25
#define OWNER_LEN 25
#define STATUS_LEN 4
#define DATE_LEN 11
#define RENTER_LEN 25
#define MAX_PARTS 50

int locate_part (char name[]);
void add(void);
void delete(void);
void display(void);
void checkin(void);
void checkout(void);

int num_parts = 0;

struct part {
  char name   [NAME_LEN+1];
  char owner  [OWNER_LEN+1];
  char status [STATUS_LEN+1];
  char date   [DATE_LEN+1];
  char renter [RENTER_LEN+1];
} parts [MAX_PARTS];

//MAIN

int main(void) {

char choice;

for(;;) {
  
  printf("[a]dd part, [d]elete part, dis[p]lay part, check [i]n part, check [o]ut part, [q]uit\n");
  printf("Please enter your choice: ");
  scanf(" %c", &choice);
  
  while (getchar() != '\n');
    
    switch (choice) {
      case 'a' : add();
      break;

      case 'd' : delete();
      break;

      case 'p' : display();
      break;

      case 'i' : checkin();
      break;

      case 'o' : checkout();
      break;

      case 'q' : return 0;
    
      default : printf("Please try again\n");
    }
    printf("\n");
}
}

//LOCATE PART

int locate_part (char name[]) {

  int i;

  for (i=0; i < num_parts; i++)
    if (parts[i].name == name)
     return i;
  return -1;
}

//ADD PART

void add (void) {

  char name[20];

  if (num_parts == MAX_PARTS) {                  
    printf("Database is full.\n");
    return;
  }

  printf("Enter part name: ");
  scanf("%s", name);                                  //PROBLEM IS HERE I THINK
                                           //BUT I DONT KNOW THE PROPER WAY  
                                           //OR IT IS A PROBLEM WITH LOCATE_PART() FUNC
                            
  if (locate_part(name) > 0) {                        //CHECK TO SEE IF PART EXIST
    printf("Part is already in database.\n");         //THIS DOES NOT WORK.
    return;
  }

  strcpy(parts[num_parts].name, name);                
  printf("Owners name: ");
  read_line(parts[num_parts].owner, OWNER_LEN);
  strcpy(parts[num_parts].status, "in");
  num_parts++;
}

//DELETE PART

void delete (void) {
}

//DIPLAY PART

void display (void) {                        //THIS ALSO DOES NOT WORK,
  int i;                                     //ELSE STATEMENT IS ALWAYS PRINTED
  char pname;
  
  printf("Enter part name: ");
  scanf("%s", pname);                       //PROBLEM IS HERE I THINK
                                            //BUT I DONT KNOW THE PROPER WAY
  i = locate_part(pname);            //OR IT IS A PROBLEM WITH LOCATE_PART()

  if (i >=0) {
    printf("Part name: %s\n", parts[i].name);
  }
  else
  printf("Part not found");
}

//CHECK IN

void checkin (void) {
}

//CHECK OUT

void checkout (void) {
}
#include <ctype.h>
#include <stdio.h>
#include "readline.h"

int read_line (char str[], int n) {
  
  int ch, i = 0;
  while (isspace(ch = getchar()));

  while (ch != '\n' && ch != EOF) {
    if (i < n)
      str[i++] = ch;
      ch = getchar();
  }
  str[i] = '\0';
  return i;
}

I should have been using strcmp() in locate_part(). The displaying part problem is fixed, but I am still able to add the same part twice. Can you spot what is wrong using the same code as above except for this small change to locate_part()?

int locate_part (char name[]) {

  int i;
 
  for (i=0; i < num_parts; i++)
    if (strcmp(parts[i].name, name) == 0)
     return i;
  return -1;
}

Hi,

I think check for locate_part function in add() should be

if (locate_part(name) >= 0) {                       
printf("Part is already in database.\n");         
return;  }

As per the code u may be able to add the same string which locate enters first into the database.
You missed check for 0th index.

insted of

if (locate_part(name) > 0)

use

if (locate_part(name) >= 0)
commented: Thank you for spotting that! +1

Hi,

I think check for locate_part function in add() should be

if (locate_part(name) >= 0) {                       
printf("Part is already in database.\n");         
return;  }

As per the code u may be able to add the same string which locate enters first into the database.
You missed check for 0th index.

insted of

if (locate_part(name) > 0)

use

if (locate_part(name) >= 0)

AWESOME! I knew there was a logic error somewhere, props to you for finding it because I sure couldn't.

And for the last problem, and then I will have a 99% complete project. I need to be able to sort the the structure by it's members (forgive me if me terminology is wrong). Right now, this code will just print the members in the order they were saved.

void part (void){
  int i;
  printf("\n");
  for(i=0; i<num_parts; i++)
    printf("%s:%s:%s:%s:%s\n", parts[i].name, parts[i].owner, parts[i].status, parts[i].date, parts[i].renter);
}

So if this code prints:

Hook : Mike : Out : 02/30/2008 : Mark
Axel : Tim : in : :

and I need to print it sorted by the member parts.name so it prints like this:

Axel : Tim : in : :
Hook : Mike : Out : 02/30/2008 : Mark

how can I accomplish this? From what I have seen there is no simple sort function for structures, so am I going to have to write a function to sort it? If so, what's the logic behind it?

Hi,

compare primary key of your database ie: part.name with strcmp in your sorting function and depending on the result swap entire structure object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.