Hello, I am currently doing an assignment where I have to read numbers from a .txt document into an array and later find the average of different lines of the .txt document sort them and what not. I'm not even close to the ladder parts of this assignment and I am stuck just getting the information in the .txt to the array. The .txt document looks like this:
3
3.55 2.55 1.55 4.55 5.55
4.55 3.55 2.55 5.55 6.55
5.55 4.55 3.55 6.55 7.55

I need to get each horizontal line into the array and do calculations on each separate line. (Excluding the first 3)

So far I have this:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>	//header file for file functions
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	// declare file objects
	ifstream in_stream;   //declare object that can read files
	ofstream out_stream;  //declare object that can write to files

	// tell file objects which files to access
	in_stream.open("Prog5.txt");
	out_stream.open("Prog5out.txt");

	// check that files can be accessed
	if (in_stream.fail())
	{
		cout << "Input file opening failed. \n";
		exit(1);
	}
	if (out_stream.fail())
	{
		cout << "Output file opening failed. \n";
		exit(1);
	}
	
    double linecount = 0;
    double arrayOne[20];

	// numbers in file set to variable number
	
	while(!in_stream.eof())
	{
		in_stream >> arrayOne[linecount];
		linecount++;
	}
     
	


	// close file objects
	in_stream.close();
	out_stream.close(); 
    
	
}

I just have no idea what to do, I'm so lost. Any help would be appreciated, thank you.

Recommended Answers

All 6 Replies

So what's the problem? What you have (with the bare description) looks OK so far.

When I try to build this it tells me that "the subscript is not of integral type." It works when I try using int instead of double for linecount and arrayOne but the numbers in the .txt are floating point and I need to keep the decimals.

The problem is that I have been trying to assign the values in the .txt file to the array for about 2 hours and I either get some crazy output, or an error of many kinds. The thing is, I have to assign each individual line in the .txt to a different array, I don't understand how to assign the numbers from the array at all let alone pick individual lines from a .txt file. I thought my description was fairly good...

When I try to build this it tells me that "the subscript is not of integral type." It works when I try using int instead of double for linecount and arrayOne but the numbers in the .txt are floating point and I need to keep the decimals.

Subscripts need to be integers.
If you are counting lines, what use is a floating point number? Which line is 3.274? It's either line 3 or line 4.

The problem is that I have been trying to assign the values in the .txt file to the array for about 2 hours and I either get some crazy output, or an error of many kinds.

Without knowing what you've tried, can't help you fix it.

The thing is, I have to assign each individual line in the .txt to a different array,

Can you use an array or arrays? Like values[#lines][#valsPerLine] ?

I don't understand how to assign the numbers from the array at all

I'm sure you do. It involves an = sign.

let alone pick individual lines from a .txt file.

Use fgets() and read 1 entire line. Then you can use sscanf() to read each number out of that line.

I thought my description was fairly good...

Subscripts need to be integers.
If you are counting lines, what use is a floating point number? Which line is 3.274? It's either line 3 or line 4.


Without knowing what you've tried, can't help you fix it.


Can you use an array or arrays? Like values[#lines][#valsPerLine] ?


I'm sure you do. It involves an = sign.


Use fgets() and read 1 entire line. Then you can use sscanf() to read each number out of that line.

I was unaware that I was counting the lines. I thought I had to make it double in order to keep the decimals from the .txt file. I'm fairly sure I am just using a one dimensional array, I don't know how to do anything else. I don't understand what you mean by fgets() and sscanf(). No clue where they go or how to use them. Thanks for the help.

When WaltP spoke of counting line and using needing to use an integer for the array index, he was (I assume) speaking of the variable linecount , not the array itself. The array can, and probably should, be of type array of double ; but for any array, the indices should be type int (or a related integral type such as unsigned or long ). If you think this through, it makes sense that this would be the case, as you cannot access (for example) the 1.5th element of an array.

As for fgets() and sscanf() , the former is a function for reading a line of text from a stream up to a maximum size, while the latter allows you to parse a string into it's components. However, given that you are using the C++ <iostream> functions for your user input, I would recommend using the C++ equivalents, which are getline() and the stringstream class.

On a final note, which I'm sure WaltP will mention, using while(!input.eof()) without also testing for input.fail() is liable to cause your program to either loop indefinitely, or else overcount the number of lines by one.

Well I have found out that what I need to do is assign in_stream to a variable to change the pointer location after the 3 so I can begin doing calculations on the first 5 numbers. With these number I will use an array that will only use the first five numbers, something like for(i=0,i<5,i++) should make that happen. And I will encase this all in a loop to come back and continue to do the calculations on the next 5 numbers and then the last 5 numbers. After that I should be mostly done.

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.