Question

In: Computer Science

You need a data structure that contains fields for name (char array), color (char array), age...

You need a data structure that contains fields for name (char array), color (char array), age (int), and height (int). Name the struct Info when you define it.

Create a function named getUserInfo() that asks the user for name, color, age, and height and then returns a data structure containing that information. It should do the following:

What is your name? George
What is your favorite color? Green
What is your age? 111
What is your height in inches? 72

struct Info getUserInfo( void )
{
// Do stuff here
}

Create a function named saveUserInfo() that takes an Info data structure and a file object as a parameter and saves the structure to disk.

void saveUserInfo( struct Info info, FILE *fp )
{
// Do stuff here.
}

Assume that the user will enter five records.

In main begin by telling the user that they will enter five user information records. Then loop through five times calling the getUserInfo() function asking their user for the next information, then saving it to disk with the saveUserInfo() function.

Remember to open the file (with filename data.txt) before you begin the loop and close the file after you end the loop.

Solutions

Expert Solution

I have implemented C program which ask the 5 user details and store it into the "data.txt file".

Here, I create data structure "struct Info" which will store name, color, age and height in inches.

// create struct info which will store user info
struct Info{

    // store user name
    char name[50];

    // store user favorite color
    char color[50];

    // store user age
    int age;

    // store user height
    int height;
};

I also created two functions::

1> struct Info getUserInfo() :- This function ask all the information fromthe user and return struct variable to the main().

2> void saveUserInfo(struct Info info, FILE *fp) :- This function save the each user data into the "data.txt" file in while loop.

First, we have to open the file using FILE *fp; then open "data.txt" file using "fp" then close that file at the end of the file.

I have attached C program which will ask the user info and store to the "data.txt" file.

C Pogram:-

#include<stdio.h>

// create struct info which will store user info
struct Info{

    // store user name
    char name[50];

    // store user favorite color
    char color[50];

    // store user age
    int age;

    // store user height
    int height;
};


// This function ask the user info from the user
struct Info getUserInfo(){

    // create struct variable which contain all the details of Info
    struct Info temp;

    // ask the name from the user
    printf("What is your name? ");
    scanf("%s", &(temp.name));

    // ask the favorite color from the user
    printf("What is your favorite color? ");
    scanf("%s", &(temp.color));

    // ask the age from the user
    printf("What is your age? ");
    scanf("%d",&(temp.age));

    // ask the height in inches from the user
    printf("What is your height in inches? ");
    scanf("%d", &(temp.height));

    printf("\n\n");

    // return entered user detail
    return temp;
}


// This function save the user data to the data.txt file
void saveUserInfo(struct  Info info, FILE *fp){

    fprintf(fp, "------------User Detail---------------\n");

    // write user name into the data.txt
    fprintf(fp, "Name : %s\n", info.name);

    // write user favorite color into the data.txt
    fprintf(fp, "Favorite color : %s\n", info.color);

    // write user age into the data.txt
    fprintf(fp, "Age : %d\n", info.age);

    // write user height into the data.txt
    fprintf(fp, "Height in inches : %d\n\n", info.height);
}


void main(){

    // assume that user will enter five recoeds
    int numberOfUser = 5;
    int number = 1;

    FILE *fp;

    // open the "data.txt" file in write mode
    fp = fopen("data.txt", "w");

    // check whether file is opened or not
    if(fp == NULL){
        printf("File is not opened");

    }else{

        // run the loop untill the numbeOfUesr does not become 0
        while(numberOfUser != 0){

            printf("Enter user %d detail :\n", number);

            // call the getUserInfo() which will ask the info from the user
            struct Info userInfo = getUserInfo();

            // call the saveUserInfo() which will save usr info to the data.txt file
            saveUserInfo(userInfo, fp);

            // decrement numberOFUser
            numberOfUser--;

            // increment number
            number++;
        }

        // check whether data is wrrited successfully or not
        if(fwrite != 0)
            printf("contents to file written successfully !\n");
        else
            printf("error writing file !\n");

        // close the file
        fclose(fp);
    }
}

Output:-

1> Program console:-

2> "data.txt" file:-

I hope you will understand how to create struct and store the user data into the text file using fprintf() function.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

Create a structure array that contains the following information fields concerning the road bridges in a town
Create a structure array that contains the following information fields concerning the road bridges in a town: bridge location, maximum load (tons), year built, year due for maintenance. Then enter the following data into the array:
***IN C++*** Create student structure with the following fields:  Name (cstring or null-terminated character array)...
***IN C++*** Create student structure with the following fields:  Name (cstring or null-terminated character array)  Student ID (int – unique random value between 1000 and 9999)  grade (char – Values A thru F)  birthday (myDate – random value: range 1/1/2000 to 12/31/2005)  Home Town (string) Create an array of pointers to students of size 10. Example: Student *stuPtr[10]; Write a function that populates the array with 10 students. Example: populate(stuPtr); Write a display function that...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name and print it out. 2) Create an uninitialized char array and copy into it      the char array containing your full name and print it out. 3) Create a Character array and copy into it the char array containing      your full name and print it out. 4) Into the Character array, use toUpperCase() to copy the char array containing     your full name...
3) Create a nested structure (one structure that contains another) and print out all the fields...
3) Create a nested structure (one structure that contains another) and print out all the fields in both structures. The main structure should have fields for: movie name and the year it was released. The extended structure should include the original structure as well as fields for: Lead actor, genre and runtime. In C 4) Create a structure for an employee which contains a field for: first name, last name, id and salary. Then use printf and scanf to fill...
What is an array data structure? What is an array index? What are the benefits of...
What is an array data structure? What is an array index? What are the benefits of array structures? What are the drawbacks of array structures? What is a grid structure? Give examples of when an array could be used. Give examples of when a grid could be used.
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20];...
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20]; int age; char country[20]; char Position[20]; double Salary; double Rating; }; First, create an array of BarcelonaPlayer structures. Now, write a function that takes an array of BarcelonaPlayer structures as input and find out the highest paid player among all the players. void highestPaidPlayer(struct BarcelonaPlayer *pl, int size); Create another function that finds all the players from Argentina. void findPlayers(struct BarcelonaPlayer *pl, int size);
language c++(Data structure) You have to read a file and store a data in character array...
language c++(Data structure) You have to read a file and store a data in character array ( you cant initialize a character array, you have to code generically) code must be generic u must read a file onece u cant use built in function etc string, code in classes if u initialized a char array or ur code doesn't run i will dislike and report u you can use link list to store data
R code Need assistance in getting this R code to run variables (Process) char ( Age)...
R code Need assistance in getting this R code to run variables (Process) char ( Age) char tmp <- expand.grid(Process = unique(words$Process),Age =unique(words$Age)) X <- model.matrix(~ factor(Process):factor(Age), data = tmp) glht(mod, linfct = X)
In your own words, describe how a population model that contains age structure information (i.e. like found in an age structure “pyramid”)
In your own words, describe how a population model that contains age structure information (i.e. like found in an age structure “pyramid”) may be better at predicting future population growth better than a population model that does not contain age structure information. Age structure information includes information on the # of individuals (or the %/proportion) in the population that are: juveniles/not able to reproduce yet, adults that are able to reproduce, and adults that are past reproductive age.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT