Beginner programmer here. The code below is able to run without outputing any errors; however, it does not output anything and it never stops running. And, I cannot figure out why this is the case after spending countless hours.
A struct contains dynamic arrays to store integers and strings after reading in a file. This file (weatherdata.txt) contains a list of city names, high and low temperatures. Then, these are stored into the dynamic arrays after reading those lines, and double the size of dynamic arrays if necessary (double the size).
To see if this works, I wanted to output the list of cities, but that didn't work. Did I wrote my code incorrectly somewhere?
#include <iostream>
#include <fstream>
using namespace std;
//Declaring struct
struct dynArr{
string *cityName;
int *hiTemp;
int *loTemp;
int size;
};
//Function read in the desired file
void openFile ( dynArr & arr1 ){
arr1.size = 10;
arr1.cityName = new string[arr1.size];
arr1.hiTemp = new int[arr1.size];
arr1.loTemp = new int[arr1.size];
string city;
int hi, lo;
ifstream is;
is.open ("weatherdata.txt ", ios::in);
int i = 0;
is >> city;
while ( ! is.eof() ){
is >> hi >> lo;
//Double the size of dynamic arrays
if ( i >= arr1.size){
string *tempStr1;
tempStr1 = new string[arr1.size*2];
int *tempInt1;
tempInt1 = new int[arr1.size*2];
int *tempInt2;
tempInt2 = new int[arr1.size*2];
for (int a = 0; a < arr1.size; a++){
tempStr1[a] = arr1.cityName[a];
tempInt1[a] = arr1.hiTemp[a];
tempInt2[a] = arr1.loTemp[a];
}
delete[] arr1.cityName;
delete[] arr1.hiTemp;
delete[] arr1.loTemp;
arr1.cityName = tempStr1;
arr1.hiTemp = tempInt1;
arr1.loTemp = tempInt2;
arr1.size = arr1.size*2;
}
//Store the read lines from file into the dynamic arrays
arr1.cityName[i] = city;
arr1.hiTemp[i] = hi;
arr1.loTemp[i] = lo;
i++;
is >> city;
}
for (int a = 0 ; a < i ; a++)
cout << a << ". " << arr1.cityName[a] << endl;
}
int main(int argc, char *argv[]) {
dynArr arr1;
openFile(arr1);
}