Question

Project 8 – Populating a struct and saving it. This is another “switch” assignment. Rather like...

Project 8 – Populating a struct and saving it.

This is another “switch” assignment. Rather like the scenarios, here you must select someone else's posted .h file, post a claim, and using it, codepopulating their struct with values you choose. Here “populate” means to give each member a value, using assignment operators and strcpy ( ) as needed.

This all assumes you completed Project 7 first! You can't skip ahead on this one.

I populated the Cat fluffy in my example in Project 7. Let's pretend someone else created it so I can do this part.

a. Post a claim for a header file/struct, not your own. As usual, up to two people can claim the same struct, but your solutions (C code) must be different.

b. At the top of your .c file, #include the header file you claimed.

#include “cat.h:

in my case.

c. You must create five instances of the struct you claim. That means I need to create five Cats. (Don't worry, I have nine). So I might code:

Cat fluffy ;

Cat fuzzy ;

Cat stinky ;

Cat gaz ;

Cat biyombo ;

followed by C code in my main function to set the name, color, etc., etc. for each of the five cats.

strcpy( fluffy.color, “calico” ) ;

strcpy( fuzzy.color “gray” ) ;

and so on.

Copy and paste will really make this easier, just make sure the information for every instance of your selected struct is different. No two cats with the same name,. in my example. This is only difficult if you make lots of typos.

d. Save your populated structs – use the fwrite ( ) C library method described in zyBook Chapter 11.6. This chapter deals specifically with binary files, which is what your structs will become when they get written out. General information about opening and closing files is covered in zyBook Chapter 9 as well.

e. Submit your .c code on this assignment.

f. Post your binary file struct output file by attaching it to a reply in the discussion forum. Use the same thread on which you claimed the topic.

So to summarize Project 8:

  • Claim someone else's Project 7, (not your own) struct .h file by posting a reply.

  • #include that .h file into the main.c you code.

  • Code completely populating the members of the struct for five separate instances. (Like my five cats)

  • Use fwrite ( ) code to save your populated structs to a binary file.

  • Name the file with the name of your struct. In my example. Using cat.h, my binary output file would be cat.dat

  • Submit your .c code here
  • Post the binary file output file from running your code as a reply to your claim of the .h file. Be sure to attach the file – do not try to in-line it in the post – won't work very well for a binary file.

  • I clamied topic Games

  • Language C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// File name: cat.h
#ifndef FILECREATOR_H
#define FILECREATOR_H
#define MAX 20
#include <stdio.h>
#include <string.h>

// Defines a structure to store cat name and color
typedef struct
{
char name[MAX];
char color[MAX];
}Cat;// End of structure

// Creates an array of Cats object of size MAX
Cat cats[MAX];
// Record counter
int len = 0;

// Function to check duplicate cat
// if no duplicate record found adds the cat to the list
void addCat(Cat cat)
{
// Loop variable
int c = 0;
// To store duplicate status
int flag = -1;

// Loops till number of records
for(c = 0; c < len; c++)
{
// Checks if current object name is equals to parameter object name
if(strcmp(cats[c].name, cat.name) == 0)
{
// Sets the flag to loop variable
flag = c;
// Stop the loop
break;
}// End of if condition
}// End of for loop

// Checks if flag value is -1 then no duplicate record found
if(flag == -1)
{
// Copies the parameter cat name to current object cat name
strcpy(cats[c].name, cat.name);
// Copies the parameter cat color to current object cat color
strcpy(cats[c].color, cat.color);

// Increase the record counter by one
len = len + 1;
}// End of if condition

// Otherwise duplicate record, display error message
else
printf("\n Duplicate cat name %s not allowed.", cat.name);
}// End of function

// Function to display all Cats information
void showCats()
{
// Loop variable
int c;

// Loops till number of records
for(c = 0; c < len; c++)
// Displays each cat name and color
printf("\n\n Cat Name: %s \n Cat Color: %s", cats[c].name, cats[c].color);
}// End of function

// Function to display all Cats information
void writeCats()
{
// Loop variable
int c;
// Opens a file to write in binary mode
FILE *writeF = fopen("catInfo.txt", "wb+");

// Loops till number of records
for(c = 0; c < len; c++)
// Writes the cats information to file
fwrite(&cats[c], sizeof(Cat), 1, writeF);
// Close the file
fclose(writeF);
}// End of function
#endif

----------------------------------------------------------------------------------------------------------

#include "cat.h"

// main function definition
int main()
{
// Declares objects of Cat
Cat fluffy;
Cat fuzzy;
Cat stinky;
Cat gaz;
Cat biyombo;

// Assigns name and color to cat object
strcpy(fluffy.color, "calico");
strcpy(fluffy.name, "fluffy");
// Calls the function to add the object
addCat(fluffy);

// Assigns name and color to cat object
strcpy(fuzzy.color, "gray");
strcpy(fuzzy.name, "fuzzy");
// Calls the function to add the object
addCat(fuzzy);

// Assigns name and color to cat object
strcpy(stinky.color, "white");
strcpy(stinky.name, "stinky");
// Calls the function to add the object
addCat(stinky);

// Assigns name and color to cat object
strcpy(gaz.color, "black");
strcpy(gaz.name, "gaz");
// Calls the function to add the object
addCat(gaz);

// Assigns name and color to cat object
strcpy(biyombo.color, "red");
strcpy(biyombo.name, "biyombo");
// Calls the function to add the object
addCat(biyombo);

printf("\n Adds a duplicate Cat %s to test: \n", biyombo.name);
Cat duplicate;
// Assigns name and color to cat object
strcpy(duplicate.color, "red");
strcpy(duplicate.name, "biyombo");
// Calls the function to add the object
addCat(duplicate);

// Calls the function to display cats information
showCats();

// Calls the function to write cats information
writeCats();
}// End of main function

Sample Output:

Adds a duplicate Cat biyombo to test:

Duplicate cat name biyombo not allowed.

Cat Name: fluffy
Cat Color: calico

Cat Name: fuzzy
Cat Color: gray

Cat Name: stinky
Cat Color: white

Cat Name: gaz
Cat Color: black

Cat Name: biyombo
Cat Color: red

catInfo.txt file contents

fluffy calico   
fuzzy gray   
stinky white
gaz black
biyombo red

Add a comment
Know the answer?
Add Answer to:
Project 8 – Populating a struct and saving it. This is another “switch” assignment. Rather like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours...

    Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours Unit semester of study List of prerequisites Number of prerequisites List of post requisites Number of post requisites Inside your main function declare an array of type Unit called units with size 20 elements. This array will contain the information related to all the units in our diploma program. Each of the elements of this array correspond with one our diploma units. II III...

  • uhayDownloads/ab%2010.pdf Lab 10 Fall 2017 Eric May struct TreeNode string word TreeNode left, right Note: A...

    uhayDownloads/ab%2010.pdf Lab 10 Fall 2017 Eric May struct TreeNode string word TreeNode left, right Note: A lot of your work from lab 9 can be reused or repurposed. Remember that working with a partner is encouraged Using Classes Define a Tree class suitable for holding the Node struct defined above. I have provided a header file and the obj that accompanies it so you can see how a binary search tree is supposed to function, simply put both files into...

  • C programming is the main one and pick another code of choice!!! Background This assignment is...

    C programming is the main one and pick another code of choice!!! Background This assignment is based on one of the Big Ideas in this course, namely that reading C source code is a real aid in learning how to write C code. To that end, in this project, you write a program that scans C source from the 'Net and calculates some simple statistics. We're learning lots of concepts, and by running the program you write here on several...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

  • -can you change the program that I attached to make 3 file songmain.cpp , song.cpp ,...

    -can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...

  • Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {   ...

    Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {    printf("%s,%d\n", ny->name, ny->year); } int main() {    char * ny_format = "%s,%d\n"; #if PHASE >= 1    puts("Phase 1 tests");    // Make sure we can create an object and get the data back.    // We're creating the name as a local array so that we can    // make sure that create_name_year() does not merely make    // a copy of...

  • /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've...

    /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've been working on. i am not sure how to read the file's contents AND put them into variables/an array. should it be in an arraylist? or maybe a regular array? or no arrays at all? i am also not sure how to call the factory method from garagec. i'm thinking something along the lines of [Garage garage = new GarageC.getInstance("GarageC", numFloors, areaofEachFloor);] but i...

  • Hi! I need help with a C++ program In this assignment, you will create some routines...

    Hi! I need help with a C++ program In this assignment, you will create some routines that will later be used in a "Black Jack" program. Your output on this first part will look something like: card's name is QC with a black jack value of 10 Unshuffled Deck AC/1 2C/2 3C/3 4C/4 5C/5 6C/6 7C/7 8C/8 9C/9 TC/10 JC/10 QC/10 KC/10 AD/1 2D/2 3D/3 4D/4 5D/5 6D/6 7D/7 8D/8 9D/9 TD/10 JD/10 QD/10 KD/10 AH/1 2H/2 3H/3 4H/4 5H/5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT