beginners frustration

Write a program that opens the file and reads all the numbers from the file. The file contains a list of random numbers. and calculate/determine the following:

1. Counts how many numbers were read from the file
2. The sum of all the numbers in the file (a running total)
3. The average of all the numbers in the file
4. Determine the lowest and highest values in the file
• The program must validate that the file was opened before reading from the file.
• If file does not exist then display a message telling the user that the file could not be opened and exit the program.

I am also having trouble knowing when to use the correct statement when needed





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

int main()

{
ifstream inputFile;
int numbers;
int running_total;
int count;
int highest =1;
int lowest = 0;
int size;
double sum = 0.0;
double average = 0.0;



// Openn the file.
inputFile.open("Random.txt");

// If the file opens successfully, process it
1
2
3
4
5
6
7
8
9
10
11
if (inputFile)
	{
		// Read the numbers from the file list and display them.
		// count how man numbers where read from the file.
		while (inputFile >> numbers)

		{
			cout << numbers << endl;
		}

	}

// Close the file.
inputFile.close();

//If the files does not exist display a messsage file doesnot exist.
1
2
3
4
if (inputFile)
		
		cout << "File does not exist. \n";
	}


// The sum of all the numbers in the file (running total)
cout << "The total numbers in the file ";
cin >> numbers;

// Calculate the sum of all numbers
1
2
3
4
5
while (inputFile >> numbers)
	{
		numbers++;
		sum += numbers;
	}


// Get the average of all numbers in the file
1
2
3
4
5
6
[code]{
		if (numbers > 0)
			average = sum / numbers;
		else
			average = 0.0;
	}
[/code]

// Determin the lowest and the highest values in the false
1
2
3
4
5
6
7
highest = numbers[0];
	for (count = 1; count < SIZE; i++)
	{
		if (numbers[count] > highest)
		{
			highest = numbers[count];
		}

return 0;

}
Last edited on
Please use code tags. Edit your post. Highlight the code and then click the <> button to the right of the edit window. This will cause the site to display line numbers on your code so we can refer to specific parts.

You sort of have the right idea, but you need to compute the sum, count, highest and lowest values as you read the file. Read the file one time only. Your loop will look something like this:

1
2
3
4
5
6
7
8
9
10
    while (inputFile >> number) {
        sum += number;
        ++count;
        if (number < lowest) {
            lowest = number;
        }
        if (number > highest) {
            highest = number;
        }
    }


One issue here is how to initialize lowest and highest. If you initialize lowest to a very large number then the first number in the file will be smaller and it will be set right. By the same argument you can initialize highest to a very small number. The right way is to #include <limits> and then declare them as
1
2
int lowest = numeric_limits<int>::max();
int highest = numeric_limits<int>::min();


One more thing: initialize count to zero.
I will make the corrections and see how it runs

Thank You
Here are some of the changes I have made if anyone can please help I am getting so frustrated with this it feels like I am chasing my tail on these error codes at the bottom of the program

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

int main()

{

ofstream outputFile;
outputstream inputFile;
ofstram outputfile(Random.text);
int numbers = 0;
int running_total = running_total;
int count = 0;
int highest =1;
int lowest = 0;
int size = 0;
int x = 0;
int sum = 0;
double average = 0.0;
int lowest = numeric_limits<int>::max();
int highest = numeric_limits<int>::min();

// Openn the file.
inputFile.open("C:\\data\Random.txt")

ifstream inputFile;
int number;
inputFile.open("random.txt");

1
2
3
4
5
6
7
8
9
10
[code]if (!inputFile)
	{
		cout << "Error opening file." << endl;
	}
	else
	{
		while (inputFile >> number)
		{
			cout << number << endl;
		}


inputFile.close();
}[/code]

// If the file opens successfully, process it
1
2
3
4
5
6
7
8
9
10
	if (inputFile)
		
		// Read the numbers from the file list and display them.
		// count how man numbers where read from the file.
		while (inputFile >> numbers)

			{
				cout << numbers << endl;
			}
}


//If the files does not exist display a messsage file doesnot exist.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (inputFile)
		while (inputFile >> Random. txt)
			cout << "File does not exist. \n";
	
		
		// The sum of all the numbers in the file (running total)
		cout << "The running_total in the file ";
	

			while (count <= runing_numbers)
			cin >> numbers;
		
			{
				cin >> number;
				sum += running_numbers;
			}
		

// Calculate the sum of all numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
if (numbers == 0)
				while (inputFile >> numbers)
		}
				{
					sum += numbers;
					++count;
					if (number < lowest) {
						lowest = number;
					}
					if (number > highest) {
						highest = number;
					}
				}


// Get the average of all numbers in the file
1
2
3
4
5
6
{
				if (numbers > 0)
					average = sum / numbers;
				else get average = 0.0;
}
			{

// Determin the lowest and the highest values in the false
1
2
3
4
5
6
7
8
9
 highest = numbers;

		if (count = 1; count < SIZE; i++)
		
		
		if (numbers[count] > highest)
		{
			highest = numbers;
		}


// Close the file.
inputFile.close();

return 0;

}

build errors

error C2065: 'outputstream': undeclared identifier
'outputstream': undeclared identifier
error C2146: syntax error: missing ';' before identifier 'inputFile'
error C2065: 'inputFile': undeclared identifier
error C2065: 'ofstram': undeclared identifier
error C2146: syntax error: missing ';' before identifier 'outputfile'
error C2065: 'Random': undeclared identifier
error C2228: left of '.text' must have class/struct/union
note: type is 'unknown-type'
error C3861: 'outputfile': identifier not found
error C2374: 'lowest': redefinition; multiple initialization
note: see declaration of 'lowest'
error C2374: 'highest': redefinition; multiple initialization
note: see declaration of 'highest'
error C2065: 'inputFile': undeclared identifier
error C2228: left of '.open' must have class/struct/union
note: type is 'unknown-type'
warning C4129: 'R': unrecognized character escape sequence
error C2146: syntax error: missing ';' before identifier 'ifstream'
error C2059: syntax error: 'if'
error C2143: syntax error: missing ';' before '<<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2059: syntax error: 'while'
error C2447: '{': missing function header (old-style formal list?)
error C2059: syntax error: 'if'
error C2143: syntax error: missing ';' before '}'
error C2059: syntax error: '}'
error C2143: syntax error: missing ';' before '{'
error C2447: '{': missing function header (old-style formal list?)
error C2447: '{': missing function header (old-style formal list?)
error C2447: '{': missing function header (old-style formal list?)
To give you some food for thought:
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
#include <iostream>
#include <fstream>
#include <iomanip>// Why do you have this one?
#include <cmath> // Why do you have this one?
#include <string> // Why do you have this one?
using namespace std;

int main()

{

ofstream outputFile;// Are you trying to write to the file? If not, delete it.
outputstream inputFile; // What is outputstream? If you don't know, delete it.
ofstram outputfile(Random.text); // What is ofstram?? Is this the right format to write to the file?
int numbers = 0;
int running_total = running_total;// What is the value of running_total? If the answer is unknown, then you need to delete this or initialize it with a known value.
int count = 0;
int highest =1; // Why are you declaring/initializing this twice?
int lowest = 0;// Why are you declaring/initializing this twice?
int size = 0;
int x = 0;
int sum = 0;
double average = 0.0;
int lowest = numeric_limits<int>::max();// Why are you declaring/initializing this twice? Also, you need to #include <limits> above
int highest = numeric_limits<int>::min();// Why are you declaring/initializing this twice? Also, you need to #include <limits> above
// Openn the file.
inputFile.open("C:\\data\Random.txt") // Why are you trying to open the file twice?

ifstream inputFile;
int number; // Why are you declaring the variable twice?
inputFile.open("random.txt");// Why are you trying to open the file twice? Verify the file name, it is case sensitive. 


The output:
There are 200 numbers in the file.
The sum of all the numbers in the file is: 105527
The average of all the numbers in the file is: 527
The lowest value in the file is: 7
The highest value in the file is: 1000
Press any key to continue...
Last edited on
I am just suppose to read this file and I see I kneed to pay more attention to detail, but I am still getting the following errors and even more frustrated. I had it down to jus

error C2065: 'Random': undeclared identifier
error C2228: left of '.txt' must have class/struct/union
note: type is 'unknown-type'
error C2059: syntax error: '}'
errorC2146: syntax error: missing ';' before identifier 'average'
errorC4551: function call missing argument list
error C2143: syntax error: missing ')' before ';'
error C2065: 'SIZE': undeclared identifier
error C2065: 'i': undeclared identifier
error C2059: syntax error: ')'
error C2143: syntax error: missing ';' before 'if'
fatal error C1075: the left brace '{' was unmatched at the end of the file

Here is the program I keep making worst @ checofeo you ran can you walk me through my mistakes or someone so i can understand them.


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

int main()

{
int numbers = 0;
int running_total = 0;
int count = 0;
int size = 0;
int x = 0;
int sum = 0;
int lowest = numeric_limits<int>::max();
int highest = numeric_limits<int>::min();
double average = 0.0;


// Openn the file.
ifstream inputFile;
inputFile.open("Random.txt");

1
2
3
4
5
6
7
8
9
10
if (!inputFile)
	{
		cout << "Fille could not open." << endl;
	}
	else
	{
		while (inputFile >> numbers)
		{
			cout << numbers << endl;
		}

inputFile.close();
}

// If the file opens successfully, process it
1
2
3
4
5
6
7
8
9
if (inputFile)

		// Read the numbers from the file list and display them.
		// count how man numbers where read from the file.
		while (inputFile >> numbers)

		{
			cout << numbers << endl;
		}

1
2
3
4
5
6
{
		//If the files does not exist display  a message file does not exist.
		if (inputFile)
			while (inputFile >> Random.txt)
				cout << "File does not exist. \n";
	}


1
2
3
4
5
6
7
8
9
10
11
{
		[code]// The sum of all the numbers in the file (running total)
		cout << "The running_total in the file \n ";

		if (inputFile)
			while (count <= running_total)
				cin >> numbers;

		cin >> numbers;
		sum += running_total;
	}


// Calculate the sum of all numbers
1
2
3
4
5
{
		if (numbers == 0)

			while (inputFile >> numbers)
	}

1
2
3
4
5
6
7
8
9
10
11
{
		sum += numbers;
		++count;
		if (numbers < lowest)

			lowest = numbers;

		if (numbers > highest)

			highest = numbers;
	}


1
2
3
4
5
6
7
{
		// Get the average of all numbers in the file
		if (numbers > 0)
			average = sum / numbers;
		else get average = 0.0;
	}
	{

// Determin the lowest and the highest values in the file
1
2
3
4
5
6
7
8
9
10
highest = numbers;

		if (count = 1; count < SIZE; i++)


			if (numbers> highest)
			{
				highest = numbers;
			}
		{

// Close the file.
inputFile.close();
return 0;
}
I am seeing a lot of if (inputFile).

To open the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (!inputFail)
{
    cout << "Error opening the file.\n";
}
else
{
    // The while loop is reading each number.
    while (inputFile >> number) 
       {
        sum += number; // It is adding each number to the sum (running total).
        ++count; // It is counting each number available in the file.
        if (number < lowest) // If the number is lower than the lowest.
        {
            lowest = number; // Then. the number will become the lowest.
        }
        if (number > highest) // If the number is higher than the highest.
        {
            highest = number; // Then. the number will become the highest.
        }
     }

}


You do not need to keep using if (inputFile) more than once.
Take a look at the comments i placed in the while loop. Now, think about if it meets the requirements of the homework. Take a look at the codes that you dont understand. You may not need it at all.

For instance,

What are you trying to do here?
1
2
3
4
5
6
7
8
9
10
highest = numbers;

		if (count = 1; count < SIZE; i++)


			if (numbers> highest)
			{
				highest = numbers;
			}
		{


Go back line by line in the code and ask, What is the purpose of this line? Does it help to meet the homework requirements? You are really close to complete the homework, but you will have to delete some of the code that is serving no purpose.

Topic archived. No new replies allowed.