I am a student of C++. I am working through the book, "Starting Out With C++ Early Objects (9th Edition). Example 27 from Chapter 6 (on Functions) reads data from a file but will not compile. Here is the full code:
// Program 6-27
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
// Function prototype
bool readData(ifstream &someFile, string &city, double &rain);
int main()
{
ifstream inputFile;
string city;
double inchesOfRain;
// Display table headings
cout << "July Rainfall Totals for Selected Cities \n\n";
cout << " City Inches \n";
cout << "_________________ \n";
// Open the data file
inputFile.open("rainfall.dat");
if (inputFile.fail())
cout << "Error opening data file.\n";
else
{
// Call the readData function
// Execute the loop as long as it found and read data
while (readData(inputFile, city, inchesOfRain) == true)
{
cout << setw(11) << left << city;
cout << fixed << showpoint << setprecision(2)
<< inchesOfRain << endl;
}
inputFile.close();
}
return 0;
}
bool readData(ifstream &someFile, string &city, double &rain)
{
bool foundData = someFile >> city >> rain;
return foundData;
}
And here's the accompanying data for the data file Rainfall.dat:
Chicago 3.70
Tampa 6.49
Houston 3.80
The problem lies with this line in the "bool readData" function:
bool foundData = someFile >> city >> rain;
I am using Visual Studio Community 2017. "someFile" gets a red squiggly line and the dropdown displays the following error:
no suitable conversion function from "
std::basic_istream<char, std::char_traits<char>>
" to "bool
" exists
I don't really understand the error message but have managed to get this program working with:
A simple cast:
bool readData(ifstream &someFile, string &city, double &rain)
{
return static_cast<bool>(someFile >> city >> rain);
}
Or this as an alternative:
bool readData(ifstream &someFile, string &city, double &rain)
{
if(someFile >> city >> rain)
return true;
else
return false;
}
So, my real questions are:
- Are my solutions ok or is there a better way?
- why is an error being thrown at all on Educational material that you would imagine should have been thoroughly tested first. Or is this just Visual Studio (intelliSense) specific, but works just fine on other compilers?