Reputation: 3
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;
struct Weather
{
string month[12];
double rainfall;
double hitemp;
double lotemp;
};
int main()
{
Weather month[12] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber"};
for (int i = 0; i < 12; i++)
{
cout << "month rainfall" << month[i] << endl;
cin >> month[i].rainfall;
}
cout << month[3].rainfall;
}
Really new to c++ using GDB on my home desktop i use codeblocks in class though. Attempting to do an assignment involving storing data on weather for each month. I currently am running into this error. Should i use pointers ? or can you not use a pre-filled array and ill have to ask the user for the month?
main.cpp: In function ‘int main()’: main.cpp:25:34: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘Weather’)
cout << "month rainfall" << month[i]<< endl;
Edit: i apologize i wasn't clear about my intention. I was attempting to have month[12] be all the months and then store the rainfall, highest temp and lowest temp, month by month by indexing through the array. Is it possible for each element in the array to be given a structure? Will i have to assign each months data individually? I apologize im not too well versed in the terminology.
Upvotes: 0
Views: 267
Reputation: 173
As others have pointed out already, you have a couple initialization issues. You're also leaving a lot of your structure's data unset.
I haven't touched on the second issue, but this fixes the first while staying strictly within the bounds you defined (ie an array of a simple structure):
#include <iostream>
#include <string>
using namespace std;
struct Weather
{
string month;
double rainfall;
double hitemp;
double lotemp;
};
int main()
{
string monthName[12] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber" };
Weather month[12];
for (int i = 0; i < 12; i++)
{
cout << "Rainfall for " << monthName[i] << endl;
month[i].month = monthName[i];
cin >> month[i].rainfall;
month[i].hitemp = month[i].lotemp = 0;
}
int outMonths[3] = { 3, 7, 11 };
for (int i = 0; i < 3; i++) {
cout << month[outMonths[i]].month << " " << month[outMonths[i]].rainfall << endl; // Outputs: April, August, & December
}
Personally, I'd go another step and add a constructor, maybe even use std::array just because. :-)
#include <iostream>
#include <string>
#include <array>
using namespace std;
struct Weather
{
string month;
double rainfall;
double hitemp;
double lotemp;
Weather() {
month = "";
rainfall = hitemp = lotemp = 0;
}
};
int main()
{
string monthName[12] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber" };
array<Weather, 12> month;
for (int i = 0; i < 12; i++)
{
cout << "Rainfall for " << monthName[i] << endl;
month[i].month = monthName[i];
cin >> month[i].rainfall;
}
int outMonths[3] = { 1, 6, 11 };
for (int i = 0; i < 3; i++) {
cout << month[outMonths[i]].month << " " << month[outMonths[i]].rainfall << endl; // Outputs: February, July, & December
}
Upvotes: 0
Reputation: 1940
Firstly your initialization is incorrect
Weather month[12] = {"January", "Febuary", "March","April","May","June","July","August","September","October","November","Decemeber"};
You have declared an array named month which can store 12 Weather
structures. I doubt this was your intention.
On the contrary you intended to initialize the month
member of the Weather
struct
Coming to the actual error you are seeing
cout << "month rainfall" << month[i]<< endl;
The compiler thinks you are trying to output the entire structure because month[i]
refers to the ith element in the array of structures you've declared.
What you should be doing is something like this
#include <iostream>
#include <string>
struct Weather
{
std::string month;
double rainfall;
double hitemp;
double lotemp;
};
int main()
{
Weather yearlyweather[12];
for (int i = 0; i < 1; i++)
{
std::cin >> yearlyweather[i].month;
std::cin >> yearlyweather[i].rainfall;
std::cin >> yearlyweather[i].hitemp;
std::cin >> yearlyweather[i].lotemp;
}
for (int i = 0; i < 1; i++)
{
std::cout << yearlyweather[i].month << "\n" << yearlyweather[i].rainfall << "\n" << yearlyweather[i].hitemp << "\n" << yearlyweather[i].lotemp;
}
return 0;
}
Upvotes: 2
Reputation: 998
Your array is the problem. C++ thinks you are trying to print out a struct Weather
.
You've made an array Weather months[12]
consisting of 12 Weather
structs. You then index that array, months[i]
, but C++ does not know how to print a Weather
.
Instead you need to access, say, the month
attribute on the Weather
.
Weather months[12];
months[0].month = "January";
// ETC
// To access the month value
months[i].month;
I also don't believe you'd want there to be a string array inside the weather struct.
Perhaps :
struct Weather {
string month;
};
Upvotes: 0