Hey, I've been struggling with this. It keeps asking for a main function but on our brief it lists no such thing. And It does not seem to run properly. I'm very new to C++ so any help is appreciated!
Brief:
Your program should read sales values from a file called ‘sales.txt’ and outputs a bar graph representing these values to a file called ‘graph.txt’. Create each bar in the bar graph by displaying a row of asterisks. Each asterisk should represent £100 of sales.
For example, ‘sales.txt’ file contained these values:
1000
500
1200
600
200
Here is what the ‘graph.txt’ file should contain after the program is executed:
SALES BAR CHART
(each * equals £100)
Store 1: **********
Store 2: *****
Store 3: ************
Store 4: ******
Store 5: **
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class SalesData
{
private:
ifstream inputfile; // input file handle
ofstream outputfile; // output file handle
vector<int> salesrecords; // store of sale records
public:
void loadDataFromFile(string filename);
void saveBarChartToFile(string filename);
};
void SalesData::loadDataFromFile(string sales)
{
ifstream inputfile;
vector<int> sales;
int salesdata = 0;
salesrecords.clear(); //clean out any old data
inputfile.open("sales.txt"); // open input file
if(!inputfile) // does file exist
{
cout << "Error! File " << sales << " does not exist! " << endl;
return;
}
while (inputfile >> salesdata) //whilst data remains
{
cout << salesdata << endl;
salesrecords.push_back(salesdata); //save to vector
}
inputfile.close();
}
void SalesData::saveBarChartToFile(string graph)
{
if (salesrecords.size() == 0) // No data is stored within the vector
{
cout << "Error! No sales data availble!" << endl;
return;
}
outputfile.open("graph.txt");
if (!outputfile) //does file exist?
{
cout << !"Error! File" << graph << " cannot be opened!" << endl;
return;
}
{
for (int count = 0; count << salesrecords.size(); count++)//every sames record in the vector
{
int noofstars = salesrecords[count] / 100; // calcuate the correct number of stars to the nearest 100
for (int starcount = 0; starcount < noofstars; starcount++) outputfile << "*"; //save stars to file
outputfile << endl; //go to next line
}
I believe my program should start running after: "void SalesData::loadDataFromFile(string sales)"
That seems incorrect. That's a class function. It can't run without an instance of the class being created first. Something, somewhere, has to create an instance of your class, and then call that function.
C++ programs start running at main(). No main(), no running. Maybe someone else is going to provide that, although if you have no way to test your code it's unlikely to do very well.
@Repeater I did find it strange that nothing had be included in our brief. Thank you for your help I will be sure to experiment about with putting a main in.
@Yolanda Was scratching my head for a while, I did try implementing something similar although it caused numerous problems further down.
I did find it strange that nothing had be included in our brief.
It shouldn't have to be. It's implicit in the fact that you've been asked to write a program. It's fundamental that every C++ program needs a main function. You can't create a program without one.