Help with Number Analysis Program

Hi, I have this code written to analyze a list of 12 numbers. The numbers are stored in a file called numbersfinished.txt and the program is asking the user to input a file name, and if it matches that it should calculate the total numbers, average, highest number and lowest number and the array is supposed to match up with those numbers. No matter how hard I try, my program runs but it keeps outputting total gibberish numbers. Here is the code:

// Week 9 Labs
// Number Analysis Program
// Michael Orlando
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()

{
// Variable Declarations
const int arraysize = 12; // defining array size
int numbers[arraysize]; // defining array
int count = 0; // loop counter variable
ifstream inputfile; // stream file object
string fileName; // variable for file user inputs

cout << "Please enter the file name: "; // asking user for the file name
cin >> fileName; // collecting file name


inputfile.open("fileName");

int highest;
int lowest;

double average;

int total = 0;

while (count < arraysize && inputfile >> numbers[count])
{
count++;
}
inputfile.close();


highest = numbers[0];
lowest = numbers [0];
for (count = 0; count < arraysize; count++)
{

if (numbers[count] < lowest)
lowest = numbers[count];

else (numbers[count] > highest);
highest = numbers[count];
}
for (count = 0; count < arraysize; count++)
{
total += numbers[count];
}
average = total / arraysize;

cout << "The lowest number in the file is: " << lowest << endl;
cout << "The highest number in the file is: " << highest << endl;
cout << "The total of all numbers in the file is: " << total << endl;
cout << "The average of all numbers in the file is: " << average << endl;
}

@Orlando1130

You are trying to open a file named "fileName", as shown in inputfile.open("fileName");
Try removing the quotes around the word filename, and then the program will open the file inputted in the variable 'filename'

And regarding this line
else (numbers[count] > highest);
That is not an if statement nor correct, One, you have a semicolon at the end of the line, signifying the end of the statement. Highest will then be whatever is in number[count], whether or not, it is in fact higher or lower. Remove the semicolon. Plus it should be else if (numbers[count] > highest)

You could also insert total += numbers[count]; in the first for loop and get rid of the second for loop entirely.

Last edited on
Hi, thank you for your help... unfourtunatly, removing the quote from fileName when opening gives me an error :( which is worse than where i was at beforeC do you have any other thoughts?
@Orlando1130

In what way does it give error? When you type in the filename, do you remember to add the extension, as in '.txt'?

I have running your code with NO problems, after correcting the semicolon issue I mention in my previous post.

Here is the code I have, after my editing..
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
47
48
49
50
51
52
53
54
55
56
57
// Week 9 Labs
// Number Analysis Program
// Michael Orlando
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()

{
	// Variable Declarations
	const int arraysize = 12; // defining array size
	int numbers[arraysize] = [0]; // defining array ( And initialize to 0 )
	int count = 0; // loop counter variable
	ifstream inputfile; // stream file object
	string fileName; // variable for file user inputs

	cout << "Please enter the file name: "; // asking user for the file name
	cin >> fileName; // collecting file name


	inputfile.open(fileName);

	int highest;
	int lowest;

	float average = 0.0;

	int total = 0;

	while (inputfile >> numbers[count])
	{
		count++;
	}
	inputfile.close();


	highest = numbers[0];
	lowest = numbers [0];
	for (count = 1; count < arraysize; count++)
	{
		total += numbers[count];
		if (numbers[count] < lowest)
			lowest = numbers[count];

		if(numbers[count] > highest)
			highest = numbers[count];
	}

	average = total / arraysize;

	cout << "The lowest number in the file is: " << lowest << endl;
	cout << "The highest number in the file is: " << highest << endl;
	cout << "The total of all numbers in the file is: " << total << endl;
	cout << "The average of all numbers in the file is: " << average << endl;
}
PLEASE learn to use code tags, it makes reading and commenting on your code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: You can edit your post and add the tags.

removing the quote from fileName when opening gives me an error

And the error is? If it is a compile-time error, copy and paste it.

If it is a run-time error then your program is likely unable to find the input file to open it.

You are not checking to see if the file was opened successfully or not. Your code tries to open the file and then acts as if it did.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <string>

int main()
{
   // Variable Declarations
   const int arraysize { 12 }; // defining array size
   int numbers[arraysize] { }; // defining array
   int count { }; // loop counter variable
   std::ifstream inputfile; // stream file object
   std::string fileName; // variable for file user inputs

   std::cout << "Please enter the file name: "; // asking user for the file name
   std::cin >> fileName; // collecting file name
   std::cout << '\n';

   inputfile.open(fileName);

   if (!inputfile.is_open()) // if the file was NOT opened
   {
      std::cout << "\n*** Unable to open file...aborting! ***\n";

      return -1;
   }

   int highest;
   int lowest;
   double average;
   int total = 0;

   while (count < arraysize && inputfile >> numbers[count])
   {
      count++;
   }
   inputfile.close();

   highest = numbers[0];
   lowest = numbers[0];

   for (count = 0; count < arraysize; count++)
   {

      if (numbers[count] < lowest)
         lowest = numbers[count];

      else (numbers[count] > highest);
      highest = numbers[count];
   }

   for (count = 0; count < arraysize; count++)
   {
      total += numbers[count];
   }
   average = total / arraysize;

   std::cout << "The lowest number in the file is: " << lowest << '\n';
   std::cout << "The highest number in the file is: " << highest << '\n';
   std::cout << "The total of all numbers in the file is: " << total << '\n';
   std::cout << "The average of all numbers in the file is: " << average << '\n';
}

Please enter the file name: test

*** Unable to open file...aborting! ***

We don't know what your test file's contents are, so I created one with some sample numbers (named it "test.txt"):
1 2 3 30 4 5 6 20 7 8 9 10

Rerun the program, providing the correct file name:
Please enter the file name: test.txt

The lowest number in the file is: 1
The highest number in the file is: 10
The total of all numbers in the file is: 105
The average of all numbers in the file is: 8

How your are determining the highest number is borked, line 47 should be if (numbers[count] > highest).

Once fixed the output is:
Please enter the file name: test.txt

The lowest number in the file is: 1
The highest number in the file is: 30
The total of all numbers in the file is: 105
The average of all numbers in the file is: 8


Hi guys, thank you very much for your help and advice. Unfortunately, while I am not getting any error messages, I still keep getting random numbers that make no sense for my output. I am assuming this has something to do with my file that I am using but I typed it in exactly as it is called "numbersfinished.txt" and each and every time, even with the additions you guys gave, i remain getting gibberish output instead of the program reading from the actual list. numbersfinished.txt is already saved under the "others" folder and has it's own tab, so i am not sure why the program is not reading the numbers from it, only giving me a random output. Any suggestions?
@Orlando1130

Is your text file located in the SAME directory as your source code? Otherwise, your program won't be able to find it.
||=== Build: Debug in NumberArrayProblemFinshed (compiler: GNU GCC Compiler) ===|
||warning: C:\Users\Orlando\Documents\numbersfinished.txt: linker input file unused because linking not done|
C:\Users\Orlando\Documents\Lab1\NumberArrayProblemFinshed\main.cpp||In function 'int main()':|
C:\Users\Orlando\Documents\Lab1\NumberArrayProblemFinshed\main.cpp|24|error: no matching function for call to 'std::basic_ifstream<char>::open(std::__cxx11::string&)'|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\fstream|595|note: candidate: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\fstream|595|note: no known conversion for argument 1 from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*'|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

This is a full list of the errors I get after removing the quotes around fileName
So I did move my file into the same folder as my project,
But the issue I keep having is if I open the file as "fileName"
it runs but gives me BS numbers

If I open the file as fileName without the quotes I get this error:

C:\Users\Orlando\Documents\Lab1\NumberArrayProblemFinshed\main.cpp|24|error: no matching function for call to 'std::basic_ifstream<char>::open(std::__cxx11::string&)'|
C:\Program Files
Last edited on
You can try
inputfile.open( fileName.c_str() );

However, if that works then it is time that you updated your compiler to C++11 at least (preferably later).


The better declare-and-open-in-one-go statement is
ifstream inputfile( fileName );
@lastchance

So it actually does run with the text you said to try...

But again, I keep getting random numbers, it always says there are 0 numbers in the file and then the average and sum are negative random numbers with the letter e in and them and it's just so frustrating :(
@Orlando1130,
Please post the latest version of your code ... IN CODE TAGS (first item on the format menu).

Please also post your input file (which should have 12 numbers in, or your code definitely won't work).
Hi guys update: I finally got my code to work! Not 100% sure what I did wrong before but starting a new project and inputting a new file got things running smoothly! Thank you againf or all your help and advice, much appreciated!
Topic archived. No new replies allowed.