Question

c++ Create a program that saves data beyond the life time of the program. Utilize the...

c++

Create a program that saves data beyond the life time of the program.

Utilize the following struct:

struct PetRock {
    string name;
    int age;
};

When the program runs, it first checks to see if a file contains data (PetRocks). If the file contains data then the data will be read from the file into an array of size 3 and will be printed (assume there will be no more than 3 objects saved in the data file). If no data is contained within the file, 3 PetRock objects will be created and written to the file.

Sample Output

First time executing the program

No data in file
Collecting Pet Rocks...
Storing Pet Rocks...

Second time executing the program

Data found in file
Storing Pet Rocks into array...

Pet Rocks:
Name: Pete
Age: 1032

Name: Eugene
Age: 124832

Name: Dwayne the Rock
Age: 2374

Homework Answers

Answer #1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct PetRock {
    string name;
    int age;
};

int main() {
    ifstream in("pet_rocks.txt");
    PetRock data[3];
    if(in.is_open()) {
        string temp;
        cout << "Data found in file" << endl;
        cout << "Storing Pet Rocks into array..." << endl;
        for(int i = 0; i < 3; i++) {
            getline(in, data[i].name);
            in >> data[i].age;
            getline(in, temp);
        }
        // print data
        cout << endl << "Pet Rocks:" << endl;
        for(int i = 0; i < 3; i++) {
            cout << "Name: " << data[i].name << endl;
            cout << "Age: " << data[i].age << endl << endl << endl;
        }
        in.close();
    } else {
        cout << "No data in file" << endl;
        cout << "Collecting Pet Rocks..." << endl;
        data[0].name = "Pete";
        data[0].age = 1032;
        data[1].name = "Eugene";
        data[1].age = 124832;
        data[2].name = "Dwayne the Rock";
        data[2].age = 2374;
        cout << "Storing Pet Rocks..." << endl;
        ofstream out("pet_rocks.txt");
        for(int i = 0; i < 3; i++) {
            out << data[i].name << endl;
            out << data[i].age << endl;
        }
        out.close();
    }
    return 0;
}
Know the answer?
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
Similar Questions
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
When doing the File IO_struct program, did you notice that the design of the data structure...
When doing the File IO_struct program, did you notice that the design of the data structure can be improved? The problem is that some data are redundant in the struct/array structure. For example, the word “Amy” appears three times in the data structure. In real industry systems, no redundant data is allowed, since data in this kind of systems cannot be easily updated, deleted, added or saved. It is extremely error-prone. Thus in practice this type of designs should be...
You are to write a C++ program to produce an inventory report for a local company....
You are to write a C++ program to produce an inventory report for a local company. Your input will be item name, item number, quantity, price per item, safe stock value. The following shows which columns the input will be in: item name             item number         quantity                  price                      safe stock 20 chars                 5 char                     3 char                      6 chars                 3 chars Output will be as follows: item number         item name    quantity   price     price*quantity   %ofStock    flag You will put a symbol in the...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the accounts. Its main() method uses the CustomerAccounts class to manage a collection of CustomerAccount objects: reading customer accounts data & transactions, and obtaining a String showing the customer accounts data after the operations are complete. You will need to complete the readCustomerAccounts () and applyTransactions() methods in the CustomerAccounts class. First, please fill in your name in the...
-Data Structure in C++ (Review for C++) -using vector and class In this assignment you’re going...
-Data Structure in C++ (Review for C++) -using vector and class In this assignment you’re going to build a simple “register machine”, a kind of minimal computer that only supports storing a fixed number of values (i.e., no randomly-accessible “main memory”). Your machine will consist of an input loop that reads lines of input from the user and then executes them, stopping when the user quits (by executing the stop command). Each line of input consists of a command followed...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
c++ Program Description You are going to write a computer program/prototype to process mail packages that...
c++ Program Description You are going to write a computer program/prototype to process mail packages that are sent to different cities. For each destination city, a destination object is set up with the name of the city, the count of packages to the city and the total weight of all the packages. The destination object is updated periodically when new packages are collected. You will maintain a list of destination objects and use commands to process data in the list....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT