Answer to Question #180758 in C++ for Ishan

Question #180758

There are following two major issues associated with CPP programs:

When a program is terminated, the entire data is lost. 

If you have to enter a large number of data, it will take a lot of time to enter them all in the different programs.

Suggest a solution and elaborate the same with the help of suitable examples.


1
Expert's answer
2021-04-15T14:43:25-0400
// In order not to need to re - enter all the data, you can use either the databases where the values ​​will be saved 
// or the easiest way to implement is to save the data to a file


#include <iostream>
#include <string>
#include <fstream>


using namespace std;


int main()
{
	ofstream fout;
	fout.open("Data.txt");
	string set[3];
	for (int i = 0; i < 3; i++)
	{
		cout << "Enter string: ";
		cin >> set[i];
		if (i == 2)
		{
			fout << set[i];
			break;
		}
		fout << set[i] << " ";	
	}
	fout.close();
	cout << endl;
	cout << "Now will get data from file!" << endl;
	ifstream fin;
	fin.open("Data.txt");
	string temp;
	while (!fin.eof())
	{
		fin >> temp;
		cout << temp << endl;
	}
	fin.close();
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS