Arrays and Input File Help

I'm not exactly sure how to read a .txt file into the program. I tried just copying what my professor did off one of his slides but I get lots of errors and have no idea if I'm going the right direction. Also how does the program know where "C++NumbersInt.txt" even is on my computer? Do I have to drag that file into source files or something? (The .txt file has an unknown number of numbers in it, each with its own line, and I think it has up to or around 250 lines of numbers so for the array I used 500 to be safe. Also later in the program it's going to ask how many numbers there are and I don't know how to do that either)

//CS 1336 002: Programming Fundamentals
//This program is designed to display the lowest number in the array, the highest number in the array,
//the total of the numbers in the array, and the average of the numbers in the array.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main ()
const int ARRAY_SIZE = 500;
int numbers[ARRAY_SIZE];
int count = 0;
ifstream inputFile;

//Opens the file"C++NumbersInt.txt");

//Read the numbers from the file into the array
while (count < ARRAY_SIZE && inputFile >> numbers[count])

//Close the file
system ("PAUSE");
return 0;
The only error I see is that you're missing parentheses on the call to inputFile.close().

Also how does the program know where "C++NumbersInt.txt" even is on my computer?

Where it's expected to be when you run the program from the IDE is dependent on what IDE and settings you're using. Generally there is a "Working directory" or similar setting that specifies which directory to use. If you can't find it, you can always open a file for output, write to it, and look for that file.

If you're running from the command line, the file needs to be in the same directory as the executable.

Also later in the program it's going to ask how many numbers there are and I don't know how to do that either

What do you think count will be when you finish getting your input?
Last edited on
C++NumbersInt.txt is not really a valid file name. Yes you can create it in windows, but DOS will have a fit. I would remove the ++ for a command line program.

Ok so you don't believe me... type c:\temp> copy con C++NumbersInt.txt
It doesn't work
you can cheat and type c:\temp> copy con "C++NumbersInt.txt" and that works, but it shows you by doing that it's a special or long or non-dos file name. It will be easier just to not use special chars.

The program will only look in the current directory, or the directory where it expects it to be. If you want it to look somewhere else, you have to tell it."c:\\Temp\\numbers.txt");

You are writing command line files, I suggest you run the code from the command line. DOS Prompt.... Then you can get rid of that system ("PAUSE"); which most programmers don't like. If you have to pause a screen, try using getch(); instead. You will have to use #include <conio.h>

use code tags when you post your code so I can tell you on line XYZ you need to change.

There are many ways to do the next part, the easiest is
Define your vars
int LowestNum=0;
int LargestNum=0;
int SumNum=0;

Then put in a if statement like
if (count == 0)
    if (numbers[count] < LowestNum)
    if (numbers[count] > LargestNum)

show the output
cout << "Numbers in file: " << count << endl;
cout << "Lowest Number  : "<< LowestNum  << endl;
cout << "Largest Numbers: "<< LargestNum  << endl;
cout << "Average        : "<< SumNum/count  << endl;

If something gets messed up, put in a cout in each If statement showing what the values are. You may at times have to put them in when you read them to make sure they don't get changed somewhere else.
Last edited on
Topic archived. No new replies allowed.