[英]Program continues to run without any output
初學者程序員在這里。 下面的代碼能夠運行而不會輸出任何錯誤; 但是,它不會輸出任何內容,並且永遠不會停止運行。 而且,在花了無數個小時之后,我無法弄清楚為什么會這樣。
結構體包含動態數組,用於在讀入文件后存儲整數和字符串。 該文件 (weatherdata.txt) 包含城市名稱、高溫和低溫的列表。 然后,在讀取這些行后將它們存儲到動態數組中,並在必要時將動態數組的大小加倍(大小加倍)。
為了看看這是否有效,我想輸出城市列表,但這不起作用。 我是否在某處錯誤地編寫了我的代碼?
#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);
}
文件名和右引號之間有一個空格
is.open ("weatherdata.txt ", ios::in);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.