How would I, if it's even possible, create an array to read/store the input values from a input text file? Here is what I was thinking, but I want to pretend as if I don't know the amount of values in the file.

Input File Has: 21.22, 13.23, 43.12, 123.54, 656.10
But the program should be unaware of the total...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
	double num[100];
	int j;

	ifstream Input;

	Input.open("InputNumbers.txt");

	if (Input.fail())
	{
		cout << "Can't Open Input File!" << endl;
		exit(1);
	}

	for (j=0;j<100;j++)
	{
		Input >> num[j];
		cout << num[j];
		j++;
	}

	return 0;
}

use vectors. Here is an example :

#include <iostream>
#include <fstream>
using namespace std;
int main(){
 vector<float> inputValues;
 ifstream file("num.txt");
 while(!file.eof() || !file.fail()){
  float num = 0.0f;
  file >> num;
  inputValues.push_back(num);
 }
}

use vectors. Here is an example :

#include <iostream>
#include <fstream>
using namespace std;
int main(){
 vector<float> inputValues;
 ifstream file("num.txt");
 while(!file.eof() || !file.fail()){
  float num = 0.0f;
  file >> num;
  inputValues.push_back(num);
 }
}

The only thing, and I apologize for not mentioning this, is that we haven't been taught vectors yet, so I'm pretty much stuck to the basic stuff.

In this case, you should use a container from STL to hold the reading data, like std::vector , std::list , std::deque , and so on. If you don't fimiliar with the STL(The C++ Standard Template Library), you'd better to learn it.

The only thing, and I apologize for not mentioning this, is that we haven't been taught vectors yet, so I'm pretty much stuck to the basic stuff.

The best way to solve this is using something like a dymanic array or linked list. If you have been taught struct or class and pointer, you can build one manually. And if you don't familiar with them, there is another solution to solve it, but still need pointer knowledge, and it's less efficient. (Hint: To read the input file twice, the first read gets the count of numbers, and second read get the real content.)

The best way to solve this is using something like a dymanic array or linked list. If you have been taught struct or class and pointer, you can build one manually. And if you don't familiar with them, there is another solution to solve it, but still need pointer knowledge, and it's less efficient. (Hint: To read the input file twice, the first read gets the count of numbers, and second read get the real content.)

What if I kept it like this

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
	double num[100];
	int j;

	ifstream Input;

	Input.open("InputNumbers.txt");

	if (Input.fail())
	{
		cout << "Can't Open Input File!" << endl;
		exit(1);
	}

	while (!Input.eof())
	{
		for (j=0;j<8;j++)
		{
			Input >> num[j];
			cout << num[j] << " ";
			j++;
		}
	}


	cout << endl;
	cout << num[0] << " " << num[1];

	Input.close();
	return 0;
}

with a known amount of data...how do I get rid of the whitespaces within the file when I process "cout << num[0] << " " << num[1];"?
(my input from this is as follows:

12.45 45.32 23.46 41.87 67.65 65.12 78.1 100.01
67.65 -9.25596e+061Press any key to continue . . .

)
which has all my numbers, but is showing the incorrect output for what should be num[0],num[1]....

I'm such an idiot...I was increasing j in the loop and then increasing it again on the next iteration....so instead of doing a sequential count (i.e. 0, 1, 2, 3 ... etc), I was jumping a number (i.e. 0,2,4,6....etc.) this code works correctly now!

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
	double num[100];
	int j,count;

	ifstream Input;

	Input.open("InputNumbers.txt");

	if (Input.fail())
	{
		cout << "Can't Open Input File!" << endl;
		exit(1);
	}

	count =0;
	while (!Input.eof())
	{
		for (j=0;j<=7;j++)
		{
			Input >> num[j];
			cout << num[j] << " ";
			count++;
		}
	}


	cout << endl;
	cout << num[0] << " " << num[1] << " " << num[2];

	Input.close();
	return 0;
}

output is:

12.45 45.32 23.46 41.87 67.65 65.12 78.1 100.01
12.45 45.32 23.46Press any key to continue . . .

Why are you putting a for loop inside the while loop? The for loop will get in your way if there are less than 8 values.

Get the size of the file first and dynamically allocate an array of that size.
Example :

void read(const std::ifstream file){
 size_t fileSize = 0;
 file.seekg(0,std::ios_base::end); //set the point to the end of the file
 fileSize = file.tellg(); //get the position
 file.seekg(0,std::ios_base::beg); //reset the file to starting point

 float *input = new float[fileSize+1]; //just to be safe we add +1
 //read file and do stuff
 
 delete [] input;
}
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.