0

I'm trying to finish an assignment. which is a program that reads sales data from a text file, sorts the data and outputs the sorted data to a second file. The input file is a comma-seperated value file (CSV), with the first item being the number of sales data items to be processed, followed by the list of sales data on one line (items are seperated by commas). Each sales data item is decimal number. but I have to do this in 4 functions including the main() and that's where I'm having the most issues

Sample Input:

5,453.67,8769.57,221.87,600.28,8123.00

I'm confused on how reading in a file works and out put the data sorted.

int main() {
string filename, newFilename;
ifstream infile("salesData.csv");
//cout << "What file do you want to open? " << endl;
//cin >> filename;
int size = 0;

getSalesData(size, filename);
selectionSortArray(filename, size);
ofstream outputFile;
writeTofile(outputFile, filename);

system("pause");

return 0;
}


double * getSalesData(int &size, string &filename)
{
    ifstream inputfile;
    inputfile.open(filename);
    double * salesData = new double[size];
    inputfile.close();
    return salesData;

}

//******************************************************************
// selectionSortArray
// task: to sort values of an array in ascending order // data in: the array, the array size // data out: the sorted array
//******************************************************************
void selectionSortArray(string &filename, int length){}


// copy data to output file 
void writeTofile(ofstream &outputFile, string x)
{

    outputFile.open(x.c_str());


    outputFile.close();
}

I expect to see the sample input 5,453.67,8769.57,221.87,600.28,8123.00 to be sorted

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
CAP_BLUE
  • 1
  • 1
  • Please [get a couple of good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). That should teach you all you need to know about how to read from files. It should also tell you to use `std::vector` for "dynamic arrays" instead. – Some programmer dude Feb 18 '19 at 07:07
  • 1
    Please turn this into a focused question on a single programming problem. E.g. first get the reading in working. Ask a question about the first serious, focused programming problem. Then start working (maybe asking) about sorting. Then start working (maybe asking about output). Maybe work on input, output, sorting, in that order. – Yunnosch Feb 18 '19 at 07:17
  • I would like to restate what Some programmer dude said. Functions are vital and necessary to good programming practices. What is a function? A function is something you expect in return. f(x) = 2x + 1. What happens when you plug in x = 2? How about x = 4? When you give a function something, you should expect something in return. In this case, you know what your three functions should do, but what should your input be? – Sailanarmo Feb 18 '19 at 07:21
  • In order to turn this into a question on input. Please give more details about what confuses you. What does not work? Can you read and output directly? (i.e. simplify by not trying to actually store what you read. – Yunnosch Feb 18 '19 at 07:22
  • @Sailanarmo I get the impression that you misunderstood Somes intention. Consider pinging him by using the "@" notation. – Yunnosch Feb 18 '19 at 07:23
  • @Yunnosch no, I never finished typing that thought. I meant to say, that OP should find himself a couple of good books to read. I then went on an explained what the purposes of functions are. – Sailanarmo Feb 18 '19 at 07:25
  • If the comment is not what you intended to write, then consider deleting it, in order to reduce confusion for people like me. @Sailanarmo – Yunnosch Feb 18 '19 at 07:30
  • I'm on mobile now and my comment still has vital information in it about functions. I'll edit my comment in the morning as it is still a good comment in my opinion. @Yunnosch – Sailanarmo Feb 18 '19 at 07:35
  • @Sailanarmo You cannot edit. You will have to delete and repost. Otherwise Ok. – Yunnosch Feb 18 '19 at 07:36

1 Answers1

0

I'll just deal with your main issue. You're clearly struggling with the concepts of passing parameters to functions, and returning values from functions.

Your getSalesData function is good, in that it returns the sales data but what you've missed out is that you should catch that returned value in a variable in main. Like this

int main() {
    ...
    int size = 0;
    double* salesData = getSalesData(size, filename);
    ...
}

double * getSalesData(int &size, string &filename)
{
    ...
    return salesData;
}

Once you've captured the return value you can pass it as a parameter to your other functions, e.g.

selectionSortArray(salesData, size);
writeTofile(filename, salesData, size);

Notice that you don't need to pass a file name to the sorting function, and I've just passed the file name (and not the ofstream) to the writing function. The ofstream is only needed in the writing function, so there's no need to declare it in the main function.

Every function you write, think what does this function need as parameters, and what does it return as results. Then declare that function with only those things. Variables that a function needs internally, should be declared in the function itself.

Finally your code is missing some function protoypes, normally you put these at the top of your code. Like this

double * getSalesData(int &size, string &filename);
double * selectionSortArray(double* salesData, int size);
void writeTofile(string& filename, double* salesData, int size);

This avoids any errors you might get about undeclared functions.

john
  • 85,011
  • 4
  • 57
  • 81
  • Yes you are correct, i am terrible at passing parameters to other functions. but I think I got everything together now, except I'm confused on how you have the protypes for selectionSortArray and writeToFile int my selection sort I get an error with conversion form double to int – CAP_BLUE Feb 18 '19 at 23:34
  • Thank you John for helping me except now my question is writeing to file as a function I thought I was on the right track, but then I saw your protoype and I'm curious how you can use those parameters – CAP_BLUE Feb 18 '19 at 23:46
  • @CAP_BLUE If you have more questions it's best to post a new question to SO. – john Feb 19 '19 at 08:00