Need help with a data structure program

So I have to do this project for our C++ programming class but I'm having a few problems with it.

I have it in the format that the teacher wants (for the most part) So I don't need tips on how to shorten it or anything like that.

Mostly the problem I'm having is with older bits of the code and bigger problems with the entire code in general.

Essentially the program is to take in the Rainfall for each month of a year, the highest and lowest temeperature too. Then it takes the high and low and spits out the average for the month.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
struct weather
{
// string name;
int rain;
double high;
double low;
double avg;
};

weather month[12];
// string monthName[12] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int i;

for (i = 0; i < 12; i++);
{
// cout << "Please enter the name of the Month: ";
// cin >> month[i].name;
cout << "Please enter the Rain Fall in inches: ";
cin >> month[i].rain;
cout << "Please give the Highest Temperature: ";
cin >> month[i].high;
if (month[i].high < -100 || month[i].high > 140)
{
cout << "Hang on it couldn't have been that cold/hot. Please enter a valid temp: ";
cin >> month[i].high;
}
cout << "Please give the Lowest Temperature: ";
cin >> month[i].low;
if (month[i].high < -100 || month[i].high > 140)
{
cout << "Come on, it couldn't have been that cold/hot. Please enter a valid temp: ";
cin >> month[i].low;
}

month[i].avg = 0;
month[i].avg = (month[i].high + month[i].low) / 2;
}


for (i = 0; i < 12; i++)
{
//cout << monthName[i] << endl;
// cout << month[i].name << endl;
cout << "Month " << i << endl;
cout << "Rainfall: " << month[i].rain << "in" << endl;
cout << "High: " << month[i].high << "f" << endl;
cout << "Low: " << month[i].low << "f" << endl;
cout << "Average Temp: " << month[i].avg << "/n" << endl;
}
return 0;
}



The problem I'mhaving is that whenever I try to spit out the string of what month it is (or ask the user to input a month name) it crashes the whole exe

Ontop of that after I enter the first months worth of data the computer loops through the rest of it automatically and fills it all in with junk data... then crashes!

Please help!
This should work for ya. 3 main things i found were, the struct should be outside of main, you had a semicolon after the first for loop and you should be using cin.getline rather than cin's. Hope this helps ya

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
void clearBuf();
struct weather
{
char name[10];
int rain;
int high;
int low;
int avg;
};
int main()
{
weather month[12];
int i;
char ctemp[4];

for (i = 0; i < 12; i++)
{
cout << "Please enter the name of the Month: ";
cin.getline(month[i].name, 10);

cout << "Please enter the Rain Fall in inches: ";
cin.getline(ctemp, 4);
clearBuf();
month[i].rain = atof(ctemp);

do
{
cout << "Please give the Highest Temperature: ";
cin.getline(ctemp, 4);
clearBuf();
month[i].high = atoi(ctemp);
}while (month[i].high < -100 || month[i].high > 140);

do
{
cout << "Please give the Lowest Temperature: ";
cin.getline(ctemp, 4);
clearBuf();
month[i].low = atoi(ctemp);
}while (month[i].high < -100 || month[i].high > 140);

month[i].avg = 0;
month[i].avg = ((month[i].high + month[i].low) / 2);
}


for (i = 0; i < 12; i++)
{
cout << month[i].name << endl;
cout << "Month " << i << endl;
cout << "Rainfall: " << month[i].rain << "in" << endl;
cout << "High: " << month[i].high << "f" << endl;
cout << "Low: " << month[i].low << "f" << endl;
cout << "Average Temp: " << month[i].avg << "/n" << endl;
}
cout << "press any key to close";
_getch();
return 0;
}
void clearBuf()
{
if (!cin)
{
cin.clear();
cin.ignore(1000,'\n');
}
return;
}
AH! I figured it was something stupidly simple. Thanks!
Topic archived. No new replies allowed.