Question

Loading and Saving a Survey

For this lab activity, you are going to practice creating a program that reads and writes data to a file. Download the lab24.zip file to get started.

The main.cpp file is completely written for you; there's nothing you need to change in there. The main function provides the menu loop for the user to pick either loading or saving the survey responses. Additionally, the funcs.h header file is also done for you. The tasks to complete lie in the funcs.cpp file. There are three functions that you will need to implement.

The loadFile function should:

  • prompt/store the user to enter the filename
  • create a fstream variable
  • try opening the requested file
    • if the file failed in opening, display an error message and leave the function
  • display all the data in the file
  • close the file

The saveFile function should:

  • prompt/store the user to enter the filename
  • prompt/store the user to enter which mode to use for writing (output or append)
  • create a fstream variable
  • try opening the requested file in the mode the user requested
    • if the file failed in opening, display an error message and leave the function
  • call the survey function, passing in the file as an argument
  • close the file

The survey function is actually simple (if you did the previous lab). You can just reuse the same survey code you did previously.


Sample Run

The following is a sample run that you can use to model your program (but use different questions!):

1. Load survey responses
2. Save survey responses
3. Exit
Enter your choice: 2
Enter the filename with extension that you want to load:
surveyResp.txt
Enter 0 for output (overwrite) mode or 1 for append mode:
0

Welcome to my survey!
I will be asking you a series of 5 questions.
Your responses will be saved for future reference.
++++++++++++++++++++++++++++++++++++++++++++++++++
What is your name?
Angela Vaughn
What is your age?
24
Do you like dogs?
yes!
How many pairs of shoes do you own?
6
Do you like vegan tacos?
yaaaaaaaas
Thank you for taking my survey!
Would you like to start the survey again?
no

1. Load survey responses
2. Save survey responses
3. Exit
Enter your choice: 1
Enter the filename with extension that you want to load:
surveyResp.txt

Name: Angela Vaughn
Age: 24
Likes dogs: yes!
Pairs of shoes: 6
Likes vegan tacos: yaaaaaaaas

1. Load survey responses
2. Save survey responses
3. Exit
Enter your choice: 2
Enter the filename with extension that you want to load:
surveyResp.txt
Enter 0 for output (overwrite) mode or 1 for append mode:
1

Welcome to my survey!
I will be asking you a series of 5 questions.
Your responses will be saved for future reference.
++++++++++++++++++++++++++++++++++++++++++++++++++
What is your name?
manuel
What is your age?
31
Do you like dogs?
n
How many pairs of shoes do you own?
12
Do you like vegan tacos?
NO
Thank you for taking my survey!
Would you like to start the survey again?
y
++++++++++++++++++++++++++++++++++++++++++++++++++
What is your name?
Raine B
What is your age?
18
Do you like dogs?
of course :)
How many pairs of shoes do you own?
4
Do you like vegan tacos?
Yeah, they're okay I guess.
Thank you for taking my survey!
Would you like to start the survey again?
no thanks

1. Load survey responses
2. Save survey responses
3. Exit
Enter your choice: 1
Enter the filename with extension that you want to load:
badFile.png
Error, unable to open that file!

1. Load survey responses
2. Save survey responses
3. Exit
Enter your choice: 1
Enter the filename with extension that you want to load:
surveyResp.txt

Name: Angela Vaughn
Age: 24
Likes dogs: yes!
Pairs of shoes: 6
Likes vegan tacos: yaaaaaaaas
Name: manuel
Age: 31
Likes dogs: n
Pairs of shoes: 12
Likes vegan tacos: NO
Name: Raine B
Age: 18
Likes dogs: of course :)
Pairs of shoes: 4
Likes vegan tacos: Yeah, they're okay I guess.

1. Load survey responses
2. Save survey responses
3. Exit
Enter your choice: 3


Submission

To receive credit, you need to submit a .zip file that contains all of your files for the program.


here is lab24.zip please make code copyable and in c++
#include funcs.h #include <iostream> using namespace std; int main() { // variables int userChoice = 0; // menu loop do //
#ifndef FUNCS H #define FUNCS H include <stream using namespace std; // function prototypes void loadFile(); void saveFile();
Pinelude funcs.h #include <iomanip> #include <iostream> include <string> using namespace std; void loadfile() { // TODO voi
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 #include "funcs.h" #include <iomanip> #include <iostream> #include <string> #include <fstream> using namespace std; //loadFile function void loadFile(){ string filename; string data; cout<<"Enter a Filename: "; //prompt the user to enter the filename getline(cin,filename); fstream myfile; //fstream variable myfile.open(filename); //try opening the requested file if (myfile.is_open()){ //display all the data in the file while(!myfile.eof()){ myfile>>data; cout<<data<<endl; } myfile.close();//close the file } else { cout<<"Error opening file"; //display an error message } } void saveFile(){ string filename; string mode; cout<<"Enter a Filename: "; getline(cin,filename); cout<<"Enter which mode to use for writing (out or app ): "; getline(cin,mode); fstream myfile; myfile.open(filename,mode); if (myfile.is_open()){ survey(myfile); //call the survey function, passing in the file as an argument myfile.close(); //close the file } else { cout<<"Error opening file"; } } void survey(fstream& outputFile){ //You can just reuse the same survey code you did previously. } 


12 - 1 #include funcs.h 2 #include <iomanip> 3 #include <iostream> 4 #include <string> 5 #include <fstream> 6 using namespa

45 49 AWN o o o o o o o o o o o o o 000 44 - void saveFile(){ string filename; //string variable for storing filename 46 stri

Please Upvote the Answer

Add a comment
Know the answer?
Add Answer to:
Loading and Saving a Survey For this lab activity, you are going to practice creating a...
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
  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

  • Hello, I needed help on the following In Java: Note that if you intend to append...

    Hello, I needed help on the following In Java: Note that if you intend to append data to the end of a file, you need to create an instance of the "FileWriter" class rather than the "File" class: FileWriter fwriter = new FileWriter("RunningList.txt", true); PrintWriter outfile = new PrintWriter(fwriter); Remember that PrintWriter objects can throw an exception, so make sure to amend your main method header with the clause "throws IOException". (Cannot use try-catch, must include clause "throws IOException") Write...

  • Code in C++ Function Prototypes For the second part of the lab activity, you will be...

    Code in C++ Function Prototypes For the second part of the lab activity, you will be practicing writing function prototypes. Continue working on the same project from part A. First, rewrite the code so that you are using function prototypes for the functions getNumber.getMessage, and repeat. Remember that the prototypes go at the top of the file, followed by main, then the definitions of the three functions at the end. Second, you will be creating a new function (procedure) called...

  • 23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4...

    23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director,...

    Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director, Genre, Year Released, Running Time) into a vector of structures and perform the following operations: Search for a specific title Search for movies by a specific director Search for movies by a specific genre Search for movies released in a certain time period Search for movies within a range for running time Display the movies on the screen in a nice, easy to read...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • Please code in C++. link to continue the code is this below or you can make...

    Please code in C++. link to continue the code is this below or you can make your own code if you wish(fix any mistakes if you think there are any in it): cpp.sh/3qcekv 3. Submit a header file (project3.h), a definition file (project3.cpp), a main file (main.cpp), and a makefile to compile them together. Make sure to run the command make and produce an application file and include it with your submission. For this project, you are required to create...

  • This program is in C++ which reserves flight seats. It uses a file imported to get...

    This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...

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