Hello my name is roman I have this project to do that involves the following:
I need to take a text document full of numbers and output the lowest of those numbers and the highest of those numbers but they first have to be put into an array then outputted... I have all the code for locating and outputting the text file...I also have outputted the highest and lowest values using if statements but could someone share how I can put them into an array?
This is all the code that i have so far with no errors:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream indata;
int num=0, high=0, low=10000, enter;
char chars[50];
char newline = '\n';
indata.open("distance.txt");
if(!indata) {
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >> num;
cout<<"Type read and press enter to read info for 'distance.txt'"<<endl;//need to clarify if and else statements
cin>>enter;
while (enter = !indata.eof() )
{
cout << " " << num << endl;
indata >> num;
if(high<num)
{
high=num;
}
if(low>num)
{
low=num;
}
}
indata.close();
cout << "End-of-distance.txt....." << endl;
cout<<"The high value is "<<high<<endl;
cout<<"The low value is "<<low<<endl;
return 0;
}

Recommended Answers

All 4 Replies

Hello my name is roman I have this project to do that involves the following:
I need to take a text document full of numbers and output the lowest of those numbers and the highest of those numbers but they first have to be put into an array then outputted... I have all the code for locating and outputting the text file...I also have outputted the highest and lowest values using if statements but could someone share how I can put them into an array?

First, think about how you would normally create and put numbers into an array. How will you know how big to make the array so that it can hold all the numbers you read in? You should probably dynamically allocate the array with the new operation. However, if you don't give the array enough memory, you will have to reallocate. This isn't particularly clean or easy. You could consider using a vector from the standard template library, but it doesn't sound like you can use one for this assignment. What you should probably do is set a maximum number of values that can be read in, and then allocate your array to that size: int* inputValues = new int[MAX_INPUTS]; ( don't for get to delete [] inputValues when you are done with the array ).

How will you know where to put the incoming numbers into the array? You should probably use a counter that keeps track of where new items should be copied in the array. You would simply increment this counter every time you add an item. Again, be careful that you don't overrun the end of your array.

Give it some thought, try some simple experiments in a simple test program, and then see how you can apply the priniciples.

First, think about how you would normally create and put numbers into an array. How will you know how big to make the array so that it can hold all the numbers you read in? You should probably dynamically allocate the array with the new operation. blah blah blah.

In other words, create an array large enough to hold the data. As you read each value, add it to the array, starting at index 0. Simple as that.

Could you possibly write a simple example to show me how this would look i am very new to arrays.....

i = 0;
while (enter = !indata.eof() )
{
    indata >> num[i];
    i++;
}

Also, read this about .eof() (which is identical to C's feof()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.