2

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);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
xtcode
  • 23
  • 3
  • 1
    Why not use `std::vector` instead of dynamic arrays? – Barmar Jul 31 '15 at 22:38
  • I haven't gotten that far to learn that yet. Though, I can try. But I'm still extremely curious as to why the code isn't working. – xtcode Jul 31 '15 at 22:42
  • What is the contents of whetherdata? I'm guessing there is a city with a space in its name or something else the makes the stream fail before eof, so eof is never reached. – ex-bart Jul 31 '15 at 22:43
  • 3
    possible duplicate of [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Barmar Jul 31 '15 at 22:43
  • Have you tried printing them as you read them in? Do you actually read anything in? Try printing the size of your array at the end (`cout << i`) what is it? – Fantastic Mr Fox Jul 31 '15 at 22:44
  • @Ben, uhh.. wow. I just tried cout << i underneath the is >> city line and it isn't pretty. – xtcode Jul 31 '15 at 22:46
  • @xtcode might be time to show us that city file then ... – Fantastic Mr Fox Jul 31 '15 at 22:47
  • Next thing is, what is `string`? is that something you wrote? – Fantastic Mr Fox Jul 31 '15 at 22:49
  • @Ben The strings are the names of cities. – xtcode Jul 31 '15 at 22:54
  • That should actually work, given that weatherdata.txt, AFAICT. Although @Barmar's comment about [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) stands. Are you using the correct filename? And is the file in the working directory of your program? Try checking if opening the file succeeded. – ex-bart Jul 31 '15 at 23:04
  • The file opens fine, but are you guys saying that the code is written correctly? Also, I'm not quite too sure what I'm supposed to be getting out from http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – xtcode Jul 31 '15 at 23:15
  • I just noticed: there is a space after the filename and before the closing quote in `is.open ("weatherdata.txt ", ios::in);`. Therefore I don't quite believe that the file opens fine. Check with `if(!is) cerr << "Open failed!" << endl;` or similar after opening. – ex-bart Jul 31 '15 at 23:19
  • What in the hell? I swear I took care of that space already. The program outputs correctly now. Wow, 5+ hours spent because of this?! – xtcode Jul 31 '15 at 23:23
  • @ex-bart Can you post an answer so I can choose you as the best answer? – xtcode Jul 31 '15 at 23:29

1 Answers1

1

There is a space between filename and closing quote in

is.open ("weatherdata.txt ", ios::in);
ex-bart
  • 1,352
  • 8
  • 9