TXT File to Array C++

Hello, I recently recieved this lab for my computer science class, but I was absent the day we learned how arrays work, so I am having a tought time with this one. I have searched the bowels of the internet as well as my textbook and it all is mandarin too me as I am still lost. The project entails I-
Read in a file of sales dollar amounts (doubles) and store them in an array.
The maximum number of amounts that the file could
contain is 20, but it could have less, and
zero (0.00) marks the end of the valid data.

Your program should read in the data and :
Determine the smallest amount in the file.
Determine the largest amount in the file
Sort the amounts from lowest to highest.

The program will output to a file the following items:
"The number of sales amounts in the file is "
"The lowest sales amount is "
"The highest sales amount is "
"Here is the list of amounts sorted from lowest to highest:"

Any help is much appreciated as I am currently working on it because it is do soon. Thank You for your time.
Last edited on
I've gotten this far so far, #include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;


int main()
{
ifstream InStream;
InStream.open("file.txt");
if (InStream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}

}
Firstly, I do not advise using exit(1) for real programs, as it is not cross-platform. Secondly, how is the input file structured? Like this:

16
1
0.3
5
8

or something like:

3, 4, 6, 2, 19, 15

Please elaborate in your question.
I'm sorry I was under the impression the numbers had copied into my question.
Create and use a test file with the following values in the order that they appear below:
29.95, 3.55, 34.70, 12.34, 2.67, 33.89, 24.99, 18.45, 4.99, 9.95, 0.00
According to the instructions they are just one line
@OvO: std::exit is perfectly cross-platform. The value 1 as its argument is not. If you are going to point out that it's wrong, at least say why: the constant EXIT_FAILURE should be used instead.
@LB Sorry, did not think it was necessary, your remark was duly noted! (so formal, huh?)
Ah, I have a bad habit of forgetting that certain statements imply certain intonations. I didn't mean to sound rude or anything ;)
@LB Never mind, I took no offense, just justifying my motives! I suggest first reading the file into a string and then converting it to a char, then using a for loop, go through the char and extract numbers from it, convert char to integer ( int ) and insert them into an array, then compare them. I have not had the liberty of time to give complete code, but I will give you reading the whole file into a std::string and a link to how to convert a std::string to a char * and converting char to int.
Reading to std::string:
1
2
ifstream issue("/etc/issue.net");
    string dis((istreambuf_iterator<char>(issue)), istreambuf_iterator<char>());

Link to std::string to char * question is here: http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char
Link to char to int: http://stackoverflow.com/a/5030086/3095977
I hope LB or any other contributor, could guide you in the rest of the code, or suggest an alternate way.
Last edited on
Topic archived. No new replies allowed.