Function Templates w/ Files
Apr 13, 2013 at 6:50am UTC
Could someone please help me find the bug here? I'm trying to read a bunch of doubles into an array and print it using a template function. Ideally, I want this function to be able to print any numeric-type array. I've already tested it with integers, but for some reason it doesn't work here. Thanks in advance!
mjc_doubles.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1.0
2.2
30.30
583.22
123.56
899.2313
666.56123
555.2
777.1
210.7
734.8
248.9
512.66
1116.24
1985.666
2012.1212
8.899
16.12
2323.5
2222.0022
21.7777
88.222222
99.25646
256.3
1945.1
1549.665544
27.9
28.6
2004.14
2017.17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// File: mjcfunctemp.h
// Operating System: Linux
// Compiler: g++
// Description: Function template that will sort numeric data types
#ifndef MJCFUNCTEMP_H
#define MJCFUNCTEMP_H
#include <iostream>
// template function declarations
// outputting an array
template <typename Tp>
void mjcOutputArray(Tp arr[], const int &size)
{
int index = 0;
std::cout << arr[index] << std::flush;
for (index = 1; index < size; index++)
std::cout << ", " << arr[index] << std::flush;
std::cout << std::endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// File: mjctmpsrt.cpp
// Operating System: Linux
// Compiler: g++
// Description: Control file that demonstrates my number-sorting function template
// system header files
#include <iostream>
#include <fstream>
#include <cstdlib>
// programmer defined header files
#include "mjcfunctemp.h"
// global constants
const int SIZE_DARR = 20;
// control the program
int main()
{
// define variables
double darr[SIZE_DARR];
int index = 0;
std::ifstream infile;
infile.open("mjc_doubles.txt" );
if (infile.fail())
{
std::cerr << "Unable to read file" << std::endl;
exit(1);
}
else
{
index = 0;
while (!infile.eof() || index < SIZE_DARR)
{
infile >> darr[index];
index++;
}
}
infile.close();
mjcOutputArray(darr, SIZE_DARR);
return 0;
}
Last edited on Apr 13, 2013 at 6:51am UTC
Apr 13, 2013 at 7:32am UTC
The problem is the invalid loop condition
1 2 3 4 5 6
index = 0;
while (!infile.eof() || index < SIZE_DARR )
{
infile >> darr[index];
index++;
}
Shall be
1 2 3 4 5 6
index = 0;
while (!infile.eof() && index < SIZE_DARR )
{
infile >> darr[index];
index++;
}
I would write the code the following way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int main()
{
// define variables
double darr[SIZE_DARR] = {} ;
std::ifstream infile;
infile.open("mjc_doubles.txt" );
if (infile.fail())
{
std::cerr << "Unable to read file" << std::endl;
exit(1);
}
int index = 0;
while ( index < SIZE_DARR && infile >> darr[index] ) index++;
infile.close();
mjcOutputArray(darr, index );
return 0;
}
Last edited on Apr 13, 2013 at 7:33am UTC
Apr 14, 2013 at 1:27am UTC
That fixed it. Thanks a lot! Figured I was missing something obvious. Until next time the fail strikes :)
Topic archived. No new replies allowed.