String Conversion
Nov 12, 2012 at 4:38am UTC
Attempting to read in the file data which is a set of temperatures in a year. For my functions displayAvgTemp, displayLowestTemp, and displayHighestTemp I'm getting an error: no match for operator< in 'Print[x] < lowestTemp'
error: cannot convert 'std::string' to 'int' in assignment
Any ideas on how I can fix this?
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#include <stdlib.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
//Function Prototype\\
int mainMenu(int );
int menuOptionOne(int );
int menuOptionTwo(int );
void displayMonthlyTemp();
void displayAverageTemp();
void displayLowestTemp();
void displayHighestTemp();
///////////////////////////////////////////////////
//Name: display average temp
//Purpose: display the monthly temperature for all
//months in the year
//Parameters: ifstream, int,
//Returns: tempratures for the month
///////////////////////////////////////////////////
void displayAverageTemp()
{
//variable delcaration
string yearFile; //user entered
int year;
ifstream fin;
int userYear=0;
fin.open("RocTemp.txt" );
string print[12];
int result[12];
int totalTemp;
int avgTemp;
int temp;
//prompt user for year
cout << "Enter year: " ;
cin >> year;
//Validate yera
while (!(year >=1940 and year <=2011) )
{
cout << "\tInvalid Option - Must be between 1940-2011" <<endl;
cout << "Enter option: " ;
cin >> year;
}
while (!fin.eof())
{
fin>>yearFile;
userYear=atoi(yearFile.c_str());
if (year == userYear)
{
for (int x=0; x<12; x++)
{
fin>>print[x];
istringstream(print[x]) >> result[x];
totalTemp += result[x];
avgTemp = (totalTemp/12);
}
}
}
cout << avgTemp;
}
///////////////////////////////////////////////////
//Name: lowestTemp
//Purpose: displays lowest temp of year
//Parameters:
//Returns: lowest temp
///////////////////////////////////////////////////
void displayLowestTemp()
{
//variable delcaration
string yearFile; //user entered
int year;
ifstream fin;
int userYear=0;
fin.open("RocTemp.txt" );
string print[12];
int lowestTemp = 999;
//prompt user for year
cout << "Enter year: " ;
cin >> year;
//Validate yera
while (!(year >=1940 and year <=2011) )
{
cout << "\tInvalid Option - Must be between 1940-2011" <<endl;
cout << "Enter option: " ;
cin >> year;
}
while (!fin.eof())
{
fin>>yearFile;
userYear=atoi(yearFile.c_str());
if (year == userYear)
{
for (int x=0; x<12; x++)
{
fin>>print[x];
if (print[x] < lowestTemp)
{
lowestTemp = print[x];
}
}
}
}
cout << lowestTemp;
}
///////////////////////////////////////////////////
//Name: highestTemp
//Purpose: displays highest temp of year
//Parameters:
//Returns: highest temp
///////////////////////////////////////////////////
void displayHighestTemp()
{
//variable delcaration
string yearFile; //user entered
int year;
ifstream fin;
int userYear=0;
fin.open("RocTemp.txt" );
string print[12];
int highestTemp = -999;
//prompt user for year
cout << "Enter year: " ;
cin >> year;
//Validate yera
while (!(year >=1940 and year <=2011) )
{
cout << "\tInvalid Option - Must be between 1940-2011" <<endl;
cout << "Enter option: " ;
cin >> year;
}
while (!fin.eof())
{
fin>>yearFile;
userYear=atoi(yearFile.c_str());
if (year == userYear)
{
for (int x=0; x<12; x++)
{
fin>>print[x];
if (print[x] > highestTemp)
{
highestTemp = print[x];
}
}
}
}
cout << highestTemp;
}
Nov 12, 2012 at 5:26am UTC
@mHart
print is an array of string but you are comparing it to an int. Hence the error & the clear error message.
So print should be an array of int. Although I would have thought Temperatures should be double.
Also this:
1 2 3 4 5 6 7 8 9 10 11 12
//prompt user for year
cout << "Enter year: " ;
cin >> year;
//Validate yera
while (!(year >=1940 and year <=2011) )
{
cout << "\tInvalid Option - Must be between 1940-2011" <<endl;
cout << "Enter option: " ;
cin >> year;
}
Should be a function, because it appears 3 times in your code.
Hope all goes well.
Topic archived. No new replies allowed.