Hey guys. I am trying to work on an assignment where I have to read a set of numbers from a file and output them to the screen and display a count of the numbers... I am having no issues with this first part. The second part however is where I am getting stumped...
It asks me to then have it evaluate the numbers as it reads them and at the end output the maximum value of the numbers it read from the file.
I can get it to do 1 or the other but not both. I hope this makes sense
If I do this it reads and outputs the maximum number properly but will not display the list of numbers.
#include<fstream>
#include<string>
void main()
{
//Declare Variables
string Filename = " ", Fileloc = "C:\\Users\\Kyle\\Desktop\\School Work\\C++\\Practice 21\\";
int Number, Max = 0 , i;
unsignedshort counter = 0;
ofstream fout;
ifstream fin;
cout << " **** Please enter the file name containing the data to be analyzed. ****\n\n";
cin >> Filename; cout << endl << endl;
//file open initiation
fin.open(Fileloc + Filename);
if (!fin.is_open())
{
cout << "\n *********** Error: File Failed to open. **************\n\n";
}
elseif (fin.is_open())
cout << "\n\nThe file's contents are: \n\n";
while (fin >> Number)
if (Number > Max)
Max = Number;
{
cout << Number << endl;
counter++;
}
cout << "\n\nThere are a total of " << counter << " number(s) in the file. \n\n\n";
cout << "The Maximum Value of the List is " << Max <<".";
// close fin
fin.close();
cout << "\n\n**************************************************\n\n";
}
But if I do this it displays the list of numbers, but not the correct Max value...
#include<iostream>
usingnamespace std;
#include<fstream>
#include<string>
void main()
{
//Declare Variables
string Filename = " ", Fileloc = "C:\\Users\\Kyle\\Desktop\\School Work\\C++\\Practice 21\\";
int Number, Max = 0 , i;
unsignedshort counter = 0;
ofstream fout;
ifstream fin;
cout << " **** Please enter the file name containing the data to be analyzed. ****\n\n";
cin >> Filename; cout << endl << endl;
//file open initiation
fin.open(Fileloc + Filename);
if (!fin.is_open())
{
cout << "\n *********** Error: File Failed to open. **************\n\n";
}
elseif (fin.is_open())
cout << "\n\nThe file's contents are: \n\n";
while (fin >> Number)
{
cout << Number << endl;
counter++;
}
if (Number > Max)
Max = Number;
cout << "\n\nThere are a total of " << counter << " number(s) in the file. \n\n\n";
cout << "The Maximum Value of the List is " << Max <<".";
// close fin
fin.close();
cout << "\n\n**************************************************\n\n";
}
And I do realize that my formatting and organization is bad... I'm trying to work on it, but I'm new to programming and have never done it before this class... I realize I have a lot of work to do.
I can actually upload the notepad file if needed. When I do the first one code that I uploaded it displays it just fine... but the Max number is wrong.
However when I try the second code that I uploaded it only displays the last number and a count of 1 number.. but it displays the correct Maximum number.
Here are the actual instructions.
1) Write a well-documented C++ program that asks the user to enter the name of a file, reads and displays on screen the count and the numbers in the file in an informative and easy-to-understand format. Note: as always, if a file fails to open, the program should display an appropriate error message and terminate; a file should be closed before the program terminates.
2) Modify your program so that when the count is not zero, it also finds the maximum value stored in the file (excluding the count, i.e. the first number; for example, the maximum value in data2.txt is 13.51) and displays it on screen in an informative and easy-to-understand format. (Hint: since you read the numbers from a file one at a time, so you may keep tracking the maximum value among the data items read so far at each step. Also, to ensure your program working correctly regardless the range of the numbers stored in a file, all data comparisons should be between two numbers actually from the file, instead of some artificially initialized maximum or minimum values).
3) Modify your program one more time so that when the count is not zero, it finds both the maximum and minimum values stored in the file (again, excluding the count) and displays them on screen in an informative and easy-to-understand format (Hint: keep tracking both maximum and minimum values among the data items read so far at each step)
Part 1 I have no problem with ... its part 2 that is just stopping me dead in my tracks. If I can get part 2 figured out I have no doubt that I can get part 3 by using a similar step and just adding a min value.
Yea, that did it. I could have swore that I had tried that before and it was spitting out 32 or some other weird number... I think I'm seeing my what my problem was... I needed to have it inside the brackets with the while statement?
Thank you so much by the way.... I've been pulling my hair out for like the last 3 hours or so trying to get it to work, trying if/else, for, while, do while... you name it.
This class is by far my most hated course hahah. So glad all I have to do is this intro class, it just gets so confusing so fast.
right, because a while statement can also be declared on one line without braces. what you did was tell it to enter an if switch until max was found. in logical order it went while, if, find max, break.
example:
1 2 3 4 5 6 7 8
while(num<max)
cout<<num; //since this is the only line beneath it, and there are not braces this is all it will do until num is greater than max.
while(num<max)
{
//when you structure with braces everything inside these braces is performed until the sentinel (num<max) finds an exception, in which case it breaks.
}
other control structures can also be one liners. like if and for as well. when writing these, ask yourself what you want it to do, one thing only or many. that will help you keep your loops, and switchs in check.