Question

I am struggling with structures, and have a few questions that I can't figure out how to do. first i had to define a structure call parts containing with int variable part_number,i and a character array with 25 characters. second i have to define part to be a synonym for the struct part. third read in part_number and a part_name from the keyboard into the individual members of variable a.

include <stdio.h>

int main(void)
{
 struct part_containg

{
    int     part_number,i;
char    part_name[25] !='\0';   
    // set up array of 25 characters and include terminating null character
};

struct  part_containg   part_number,part_number[i];

for(part_number=0; part_number<25;++part_number)  // part_number goes up 1 for each    part_name
{
    printf("Intersert Part Name"\n);
    scanf("%c", &part_name[i]);         // scans for part_name
}
 return 0;
}
Was it helpful?

Solution

You have quite a few syntax errors. You should consult an introductory text book: they are really helpful! I have read half a dozen and I would rate myself quite low on the ~"C Skill Continuum"~

Anyways, we all started at the bottom. Here is an alternate version with a lot of comments:

#include <stdio.h>  /* you need a hash sign to include libraries...!     */
#include <string.h> /* i use this library to copy input rather than loop */

int main() {                      // main does not take an argument

  struct part_containing {
    int part_number;              // what is i for? I removed it
    char part_name[25];           // do the null later, this  is not the time!
  };

  struct part_containing name;    // only declare the struct, not its variables
                                  // p.s. name is an awful name-choice :P

  char input[25];                 // make a local var for input
  puts("Enter part name:\n");     // prompt user for input
  scanf("%s", input);             // capture local var
  strcpy(name.part_name, input);  // copy it into the struct
  name.part_name[25] = '\0';      // null just to be sure!

  puts("Enter number:\n");        // same for the number now
  scanf("%i", &name.part_number); // notice the syntax: &name.part_number...!

  printf("%s and %i", name.part_name, name.part_number); 
  // simple error check
  // did we get the expected input? It can be helpful to check in test runs!

  return 0;
}

It does not answer all of the questions in your assignment, but it should be enough to get you started.

Hopefully this helps! If you work on it and have more questions then ask!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top