HELP with Structs and arrays

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

Any help would be grateful

This is what I have so far.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace 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;
}

Last edited on
line 13 char name wont work, either use c-style strings char name[21] or proper strings string name

same goes for line 25
That was meant to be like char name[12] . Its still only outputting 30 numbers.
Last edited on
What is your output and expected output?

also

64
65
66
67
68
case 1:
			 for (int i = 0; i < 30; i++)
			 {
				 cout << list[i] << endl;
			 }

what are you trying to achieve here?
Case 1: was just testing. What i want to do is put the values from a txt file into a structure and then into an array.
and what output you are expecting from code? and what output you are getting?
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:
Last edited on
41
42
43
44
45
46
47
if (in.is_open())
	{
		for (int i = 0; i < 30; i++)
		{
			in >> list[i];
		}
	}


what does this snippet of your code does?
Last edited on
1
2
3
4
5
6
7
8
//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.
Last edited on
where did you initialized the list[]?
1
2
3
4
5
6
7
8
9
int main()
{

	int list[30]; // I initialized it here
	int choice;
	char name[12];
	ifstream in("weather.txt");
	Weather c[30];
	Date d[30];
1
2
3
4
5
6
7
8
9
int main()
{

	int list[30]; //This is called declaring
	int choice;
	char name[12];
	ifstream in("weather.txt");
	Weather c[30];
	Date d[30];


Since you didn't initialized the list to any values it just outputs garbage value
Oh I always thought that was initializing, so how do I fix this.
Depends on what you want to do with list[], what does list[] stores?
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.
what exactly in data list[] should store? there are so many things
What I was thinking the datalist[] should store the data thats in the txt. I'm I wrong saying this.
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).
Topic archived. No new replies allowed.