for a c++ assignment I need to create a program that reads 2 arrays into a file,
ID: 3552110 • Letter: F
Question
for a c++ assignment I need to create a program that reads 2 arrays into a file, which I have done below. What I do not understand how to do next is make a seoerate program that reads the data from the file and displays it.
#include "stdafx.h"
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_LOCATIONS = 5;
const int NUM_COLS = 4;
void WriteData( const string[], const int[][NUM_COLS], int size );
int main()
{string locations[] = {"Albet","Deli","Gerry's","Johnny's","Moe's"};
int money [][NUM_COLS] =
{ {10000, 20000, 30000, 40000},
{5000, 4000, 5000, 6000},
{25000, 26000, 27000, 28000},
{36000, 2000, 33000, 34000},
542000, 43000, 74000, 45000}};
?
WriteData(companies, sales, NUM_LOCATIONS);
system("Pause");
return 0;
}
void WriteData( const string locations[], const int money[][NUM_COLS], int size)
{ofstream outFile;
outFile.open("c:\data\ex.txt");
for( int x = 0; x < size; ++x )
{
outFile << locations[x]<<" ";
for( int y = 0; y < NUM_COLS; ++y )
outFile << money[x][y]<<" ";
}
outFile.close();
}
Explanation / Answer
Dear,
Here is the code which reads data from file and displays on screen:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
const int NUM_LOCATIONS = 5;
const int NUM_COLS = 4;
void ReadData();
int main()
{
ReadData();
system("Pause");
return 0;
}
void ReadData()
{
ifstream inFile;
string location;
int money;
inFile.open("c:\data\ex.txt");
for( int x = 0; x < NUM_LOCATIONS ; ++x )
{
inFile >> location;
cout << location<<" ";
for( int y = 0; y < NUM_COLS; ++y )
{
inFile >> money;
cout<< money<<" ";
}
cout<<endl;
}
inFile.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.