Processing Data From a File

For this problem I am supposed to write a program that will not only invoke a file but also process the data within the file. The problem I am having is I don't know how to invoke the file. I'm not sure if it's not saved in the right place or how I am supposed to get it into the program. Please HELP!

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

// Constant declarations
const int HOT = 85;
const int COLD = 60;
const int SENTINEL = -1;

// Function prototypes

void main()
{
// Variable declarations
ifstream infile; char end; string qual, file;
int temp, count = 0, hotCount = 0, pleasantCount = 0, coldCount = 0, sum = 0;

while (true)
{
cout << "Enter temp file name: "; cin >> file;
file += ".txt"; infile.open(file);
if (!infile.fail())
{
cout << fixed << setprecision(2);
cout << file;
infile >> temp;
while (temp >= 0)
{
sum += temp; count ++;
cout << setw(3) << count << setw(4) << temp << '-';
if (temp >= HOT)
{
hotCount ++; qual = "HOT";
}
else if (temp >= COLD)
{
pleasantCount ++; qual = "Pleasant";
}
else
{
coldCount ++; qual = "COLD";
}
cout << qual << endl;
infile >> temp;
}
infile.close();
cout << endl << "Counters " << "Hot = " << hotCount << "Pleasant = " << pleasantCount <<
"Cold = " << coldCount;
cout << "Average temp = " << double(sum) / count << endl;
}
else
cout << endl << "Invalid File Name";
cout << endl << "Press any key and enter to end!"; cin >> end;
}
}

And here is the data to be used from the file
55
62
68
74
59
45
41
58
60
67
65
78
82
88
91
92
90
93
87
80
78
79
72
68
61
59
-1
-1 being the sentinel value
Please edit and use code tags to format your code.

and change void main to int main
There is no return 0; at the end of main
There are no {} after the last else statement
shouldn't while (temp >= 0) be -1 ?
while (true) is not good, I would never ever use that.
Maybe while (theEarthExist) but not while true.
Do you really really want the user to enter the file name over and over and over forever. ?


The code doesn't compile. This is my advise you can take it or leave it...
You make all these coding errors, it doesn't work and you feel overwhelmed to figure it out.

Take everything out, or start a new program, add just what you need to open the file and read it, make sure that works.
Then add a while statement to read the next string line value.
Read the entire file, make sure it exits
The put in your math and output statements.

In my opinion, it's faster to write code in small sections, than to write a entire program and spend hours or days to debug why it doesn't work.

Oh last thing, writing code like
ifstream infile; char end; string qual, file;

While it works, it's very hard to read when your debuging.

I recommend 1 command per line, once your code works, you can format it all one line if you like.
Last edited on
"invoke" is meaningless in this context.
Valid file operations include open, close, read, write, create, delete etc.
Topic archived. No new replies allowed.