Hi everyone, new to the DaniWeb community, i have always used it for homework help in the past, but i'm about to rip my hair out if i stare at this code for another hour.

basically the project is to write a code that reads data from a file, gets the name, hitemp, lotemp, and avgrainfall from the input, to be used in later functions. Seemed very simple about a week ago, but the File I/O combined with the Getline() and array of structs is prooving to be a challenge.

It just seems like there isn't a problem, but it won't work! I believe its my getline functions.

The code compiles correctly in VS8.0, but as soon as command prompt opens, windows closes the window and i get this huge error message (running Windows 7 pro).

I'm sure its something stupid, but if anyone can see anything wrong with this please let me know, otherwise I'm going to assume its my computer (again... ;) )

Thanks!

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;
struct WeatherInfo
{
	string city;
	double TotRain;
	double TempHi;
	double TempLo;
	double TempAvg;
};
ifstream infile;
	
void fillCities(WeatherInfo Rain [], int &);

int main()
{
	int NumCities;
	WeatherInfo Rain[30];
	
	infile.open("prog1.txt");
	fillCities(Rain, NumCities);
                infile.close();
	return 0;
 }
 
void fillCities(WeatherInfo Rain [], int &NumCities)
{	



	string CityName;
	int i = 0;

	getline(infile, CityName);
	while ( !infile.eof() )
	{
		Rain[i].city = CityName;
		infile >> Rain[i].TotRain;
		infile >> Rain[i].TempHi;
		infile >> Rain[i].TempLo;
		infile.ignore();
		getline(infile, CityName);
		i++;
	}
	NumCities = i;
}
Ancient Dragon commented: Nice post and thanks for using code tags on your very first attempt :) +26

Recommended Answers

All 5 Replies

First move lines 15, 24 and 26 down into fillcities() function. There is no reason for ifstream to be global.

You can combine lines 38 and 39 like this: while( getline( infile, CityName ) ) . There is no need to use eof() like that because it doesn't work the way you think it does. eof() is only detected after an attempt to read something, not before.

Post a few lines of that data file.

Thanks for the quick reply!

As for the end of file, i have to use it because i'm not supposed to know how much data is being presented :(

heres some of the file Input

Dallas
3.5
85
5
Boston
4.5
56
8
Bowling Green
3.1
89
6
Houston
7.5
54
11
Orlando
3.5
78
12
Baltimore
4.5
89
14

>>As for the end of file, i have to use it because i'm not supposed to know how much data is being presented

My comment still stands. Re-read it again to understand what I said about it and how to correct it.

void fillCities(WeatherInfo Rain [], int &NumCities)
{	
    ifstream infile;
	infile.open("prog1.txt");
	string CityName;
	int i = 0;

	while ( getline(infile, CityName) )
	{
		Rain[i].city = CityName;
		infile >> Rain[i].TotRain;
		infile >> Rain[i].TempHi;
		infile >> Rain[i].TempLo;
        infile.ignore();
		i++;
	}
    infile.close();
	NumCities = i;
}

Thanks Dragon for the advice on the fix.

i made the biggest newbie move ever, it wasn't my code, however, i did name my input file (inadvertently) "prog1.txt.txt" :(

I am in CS class right now and i want to go back to my dorm room and see if it will work now. i will let everyone know if there is still a problem.

again thanks Dragon, thanks for your patence and understanding :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.