not displaying anything

the code compiles but does not show data from the file.
I will appreciate any help.

*******************************
here is contents of file that it needs to display

MBS,35,31,NW,12,3009,R
BAX,33,29,N,17,3006,S
MOP,43,41,NE,7,3004,DR
LAN,35,29,SE,8,3000,PC
AMN,41,38,SW,10,2999,CLD
FNT,39,36,S,4,2994,CLR
*******************************


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
  
#include <iostream>
#include<string>
#include<fstream>

using namespace std;

//data class
class weather{
	public:
		string name;
		int temperature;
        int dewPoint;
        string winds;
        float pressure;
        string weather;	
		
};

//fuction prototypes
void weatherFileInput(weather[], int);
int display(weather[], int);



int main(){
	
	const int size = 25;      
    weather weatherInput[size];
    weatherFileInput(weatherInput, size);
    display(weatherInput, size);
    cout << endl;
    system("pause");
  	return 0;
}
void weatherFileInput(weather weatherInput[], int size)
{

	fstream inputFile;
    inputFile.open("weatherObs.txt");

for (int i = 0; i < size; i++)
    {
	cout << weatherInput[size].name << "name" << endl;
	cout << weatherInput[size].temperature <<"temperature"<<  endl;
	cout << weatherInput[size].dewPoint <<"dewPoint"<< endl;
	cout << weatherInput[size].winds << "winds"<< endl;
	cout << weatherInput[size].pressure << "pressure"<<endl;
	cout << weatherInput[size].weather << "weather"<<endl;
}
}
int display(weather weatherInput[], int size)
{
	cout << weatherInput[size].name  << " name"<< endl;
	cout << weatherInput[size].temperature <<"temperature"<< endl;
	cout << weatherInput[size].dewPoint << "dewPoint"<<endl;
	cout << weatherInput[size].winds << "winds"<< endl;
	cout << weatherInput[size].pressure << "pressure"<< endl;
	cout << weatherInput[size].weather << "weather"<< endl;
	

   
	
}
Last edited on
Line 40: You don't check that th open was successful.

Line 44-49: You're doing cout not cin.

Line 54-59: You will display an out of bounds entry.
There are various issues. The for loops are using size for an array index which is invalid (an array has index 0 to size - 1) and don't change within the loop. The data only has 6 lines but a full 25 line are tried to be read. Also the data file has 7 elements per line but only 6 are defined in the struct?? Consider:

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
#include <iostream>
#include <string>
#include <fstream>

struct weather {
	std::string name;
	int temperature {};
	int dewPoint {};
	std::string winds;
	float pressure {};
	int unkn {};
	std::string weather;
};

size_t weatherFileInput(weather[], size_t);
void display(const weather[], size_t);

int main() {
	constexpr size_t maxSize {25};
	weather weatherInput[maxSize];

	if (const auto cnt {weatherFileInput(weatherInput, maxSize)}; cnt)
		display(weatherInput, cnt);
}

size_t weatherFileInput(weather weatherInput[], size_t maxSize) {
	std::fstream inputFile("weatherObs.txt");
	size_t cnt {};

	if (inputFile)
		for (char com {}; cnt < maxSize && (std::getline(inputFile, weatherInput[cnt].name, ',')); ++cnt) {
			inputFile >> weatherInput[cnt].temperature >> com;
			inputFile >> weatherInput[cnt].dewPoint >> com;
			std::getline(inputFile, weatherInput[cnt].winds, ',');
			inputFile >> weatherInput[cnt].pressure >> com;
			inputFile >> weatherInput[cnt].unkn >> com;
			inputFile >> weatherInput[cnt].weather >> std::ws;
		} else
			std::cout << "Cannot open file\n";

	return cnt;
}

void display(const weather weatherInput[], size_t cnt) {
	for (size_t i {}; i < cnt; ++i) {
		std::cout << weatherInput[i].name << " name\n";
		std::cout << weatherInput[i].temperature << " temperature\n";
		std::cout << weatherInput[i].dewPoint << " dewPoint\n";
		std::cout << weatherInput[i].winds << " winds\n";
		std::cout << weatherInput[i].pressure << " pressure\n";
		std::cout << weatherInput[i].weather << " weather\n\n";
	}
}



MBS name
35 temperature
31 dewPoint
NW winds
12 pressure
R weather

BAX name
33 temperature
29 dewPoint
N winds
17 pressure
S weather

MOP name
43 temperature
41 dewPoint
NE winds
7 pressure
DR weather

LAN name
35 temperature
29 dewPoint
SE winds
8 pressure
PC weather

AMN name
41 temperature
38 dewPoint
SW winds
10 pressure
CLD weather

FNT name
39 temperature
36 dewPoint
S winds
4 pressure
CLR weather

Last edited on
Topic archived. No new replies allowed.