Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Create a C++ program that will save data to a file (any file type). Use an ar

ID: 3865905 • Letter: 1

Question

1) Create a C++ program that will save data to a file (any file type). Use an array to pass the data to and from the files.

2) Open that same file to edit.

3) Prompt the user to change one of the string values to their favorite vaction spot.

4) Save the changes as a .csv file.

This should be simple, but there are string data types mixed in with doubles. Is it possible to mix the two data types? This is part of a larger project, but here's what I've got so far:

#include "stdafx.h"
#include <iostream>
#include <iomanip>  
#include <string>
#include <fstream>  
using namespace std;


int main()
{
//I have to use an array to store all data.
   double ray[2][3];
   string sray1[2],
       sray2[2];

//Initialize ray[2][3]
   ray[1][0] = 13; ray[1][1] = 70; ray[1][2] = 15;
   ray[2][0] = 70; ray[2][1] = 88; ray[2][2] = 23;

//Initialize the 2 string arrays
   sray1[1] = "This is an example"; sray1[2] = "example";
   sray2[1] = "Favorite place to"; sray2[2] = "vacation";

//create the file
   ofstream file_;
   file_.open("file.txt");

//save the data to a txt file
   for (int i = 0; i <= 2; i++) {              
       file_ << ray[i][0] << " " << sray1[i] << " " << ray[i][1] << " " << sray2[i] << " " << ray[i][2] << endl;
   }

   file_.close();

//open the txt file and change sray2[2] to the users favorite place to vacation
   ifstream edit;
   edit.open("file.txt");

//this is the part I can't figure out. How do I open the file, edit, and then save it as .csv?
   for (int i = 0; i <= 2; i++) {          
       edit >> ray[i][0] >> sray1[i] >> ray[i][1] >> sray2[i] >> ray[i][2];   //I tried getline() but still doesn't work
   }

   cout << " What is your favorite vacation spot?" << endl;
   cin >> sray2[2]; //change this arrays value

//print the changes to a .csv file
   ofstream print;
   print.open("final.csv");

   for (int i = 0; i <= 2; i++) {
       file_ << ray[i][0] << " , " << sray1[i] << " , " << ray[i][1] << " , " << sray2[i] << " , " << ray[i][2] << endl;
   }

   system("PAUSE");

   return 0;
}

Explanation / Answer

To demonstrate processing different datatypes in a file (string , int, double) lets do the following... (it will help better understand the program)
Let us say we first create a file resort.txt containing details of resorts . For each resort we store the name of the resort, the cost per person
and the rating for the resort (1-5) . The file is created from details stored in arrays. So we initially create a file where the format is
<resort1 name>
<resort1 cost>
<resort1 rating>
<resort2 name>
<resort2 cost>
<resort2 rating>
..... and so on
each detail is on a separate line.

Now after generating resort.txt, we read back the details into the arrays and then prompt the user which string to replace and get the inputs. We now iterate through the array and write the data back to resort.csv file replacing the string when a match occurs to search string.

The below code does what is explained above. Please don't forget to rate the answer if it helped. Thank you.


#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//names of resorts
string resorts[4] = {"Miami Beach Resort", "Turtle Bay Resort", "Hilton Fort Beach Resort", "Four Seasons"};
double cost[4] = { 99.99, 125.40, 150.60, 175.90}; //cost per person for each of the resort
int rating[4] = {3, 4, 4, 5}; //ratings correspondin to the resorts
int noResorts = 4;
  
//now all the arrays to a file , each detail on a separate line
ofstream outfile("resort.txt");
if(!outfile.is_open())
{
cout << "Could not create resort.txt" << endl;
return 1;
}
for(int i = 0; i < noResorts; i++)
{
outfile << resorts[i] << endl;
outfile << cost[i] << endl;
outfile << rating[i] << endl;

}
outfile.close();
  
cout << "Created resort details in a file resort.txt " << endl;

ifstream infile("resort.txt");
noResorts = 0; //reset to 0 and start reading from file back into arrays
  
while(getline(infile, resorts[noResorts]))
{
infile >> cost[noResorts] >> rating[noResorts] ;
infile.ignore(); //make sure to ignore the newline after rating line so that next getline() in loop does not read an empty line
noResorts++;
  
  
}
infile.close();
  
cout << "Read " << noResorts << " resorts from resort.txt" << endl;
cout <<"The resort names are - " << endl;
for(int i = 0; i < noResorts; i++)
cout << resorts[i] << endl;
  
string search, favorite;
cout << " Enter the resort name to be changed: ";
getline(cin, search);
  
cout << "Enter your name of your favorite spot: ";
getline(cin, favorite);
  
cout << endl << search << " will be replaced by " << favorite << endl;
  
for(int i = 0; i < noResorts; i++)
{
if(resorts[i] == search)
resorts[i] = favorite;
}
  
cout << "Creating the resort.csv file after replacement" << endl;
ofstream csvfile("resort.csv");
for(int i = 0; i < noResorts; i++)
{
csvfile << resorts[i] << "," << cost[i] << "," << rating[i] << endl;
}
csvfile.close();
cout << "Generated resort.csv" << endl;
system("pause");
return 0;
}

output

Created resort details in a file resort.txt
Read 4 resorts from resort.txt
The resort names are -
Miami Beach Resort
Turtle Bay Resort
Hilton Fort Beach Resort
Four Seasons

Enter the resort name to be changed: Turtle Bay Resort
Enter your name of your favorite spot: The Holiday Spot

Turtle Bay Resort will be replaced by The Holiday Spot
Creating the resort.csv file after replacement
Generated resort.csv

generated file resort.txt

Miami Beach Resort
99.99
3
Turtle Bay Resort
125.4
4
Hilton Fort Beach Resort
150.6
4
Four Seasons
175.9
5

generated file resort.csv

Miami Beach Resort,99.99,3
The Holiday Spot,125.4,4
Hilton Fort Beach Resort,150.6,4
Four Seasons,175.9,5