I am new to structs and I am a bit confused. what I am trying to do is pass values from a txt file into a structure, and then passing the struct into an array.
The txt looks something like this 02 3 2004 Paul 4 1 22
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
struct Date
{
int day;
int month;
int year;
};
struct Weather
{
Date date;
char name[12];
double sunShine;
double rainFall;
double midTemp;
};
int main()
{
int list[30];
int choice;
char name[12];
ifstream in("weather.txt");
Weather c[30];
Date d[30];
for (int i = 0; i < 30; i++)
{
in >> c[i].name >> c[i].sunShine >> c[i].rainFall
>> c[i].midTemp;
}
for (int i = 0; i < 30; i++)
{
in >> d[i].day >> d[i].month >> d[i].year;
}
if (in.is_open())
{
for (int i = 0; i < 30; i++)
{
in >> list[i];
}
}
do
{
cout << "1) List the dates when a given meteorologist was on duty.\n";
cout << "2) Find all the wet days.\n";
cout << "3) Find all the sunny days.\n";
cout << "4) Find the average rainfall between two dates.\n";
cout << "5) Find the hottest day based on the midday temperature.\n";
cout << "6) Find the day with the shortest sunshine.\n";
cout << "7) Exit from the application.\n";
cout << "Please select an option:\n";
cin >> choice;
switch (choice)
{
case 1:
for (int i = 0; i < 30; i++)
{
cout << list[i] << endl;
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
break;
}
} while (choice != 7);
return 0;
}
I am getting this 1) List the dates when a given meteorologist was on duty.
2) Find all the wet days.
3) Find all the sunny days.
4) Find the average rainfall between two dates.
5) Find the hottest day based on the midday temperature.
6) Find the day with the shortest sunshine.
7) Exit from the application.
Please select an option:
1
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
1) List the dates when a given meteorologist was on duty.
2) Find all the wet days.
3) Find all the sunny days.
4) Find the average rainfall between two dates.
5) Find the hottest day based on the midday temperature.
6) Find the day with the shortest sunshine.
7) Exit from the application.
Please select an option:
My expected output is 30 outputs of different data
I am getting this 1) List the dates when a given meteorologist was on duty.
2) Find all the wet days.
3) Find all the sunny days.
4) Find the average rainfall between two dates.
5) Find the hottest day based on the midday temperature.
6) Find the day with the shortest sunshine.
7) Exit from the application.
Please select an option:
1
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
02 3 2004 Paul 4 1 22
1) List the dates when a given meteorologist was on duty.
2) Find all the wet days.
3) Find all the sunny days.
4) Find the average rainfall between two dates.
5) Find the hottest day based on the midday temperature.
6) Find the day with the shortest sunshine.
7) Exit from the application.
Please select an option:
//Intaliazes the array as the data is stored in the weather.txt file
if (in.is_open())
{
for (int i = 0; i < 30; i++)
{
in >> list[i]; // The stored values
}
}
That is what I think it does, I cant tell as the output is outputting just the 30 numbers.
The list[] should store the data thats in the file. See the program is a menu. Read in data and when you press from 1-7 it will calculate something but my main goal at current is getting the data from the file into the the structs and from the struct into the array.
The txt looks something like this 02 3 2004 Paul 4 1 22
I'm guessing that the input file has many lines which have that same format?
Then your program would need an array of Weather objects.
Which indeed you do have at line 29
Weather c[30];
I see no reason for the other arrays,
int list[30];
and
Date d[30];
everything should be stored in that single array c. (A more meaningful name than a single letter for this most important part of the program might help with readability).