2014-12-04 19 views
-2

所以這個程序應該是收集天氣氣溫超過7天,然後基本上只是平均溫度和最高溫度記錄打印出來回給用戶。請記住,下面這段代碼是一個更大的程序的一部分。無論如何,問題似乎是「highest_temp1」浮點變量。當我運行該程序時,它會生成某種錯誤代碼而不是最高溫度。這段代碼在一個單獨的源文件中進行了測試,並且沒有任何問題。浮點變量不工作內部使用for循環switch敘述

switch (choice) 
    { 
    case 3: 
     int n; 
     float temperatures [7]; 
     float lastweektemp [7] = {12.56,8.65,7.5,10,7.9,5,8}; 
     float highest_temp1, highest_temp2; 
     float accumulated_temp1, accumulated_temp2; 

     system("CLS"); 
     cout << "____________Weather Data____________" << endl << endl; 
     for (n = 0; n<7; n++) 
     { 
      cout << "What is the temperature for Day " << n+1 << " ?" << endl; 
      cin >> temperatures[n]; 
      if (highest_temp1 < temperatures [n]) 
      { 
       highest_temp1 = temperatures [n]; 
      } 
      if (highest_temp2 < lastweektemp [n]) 
      { 
       highest_temp2 = lastweektemp [n]; 
      } 

      accumulated_temp1 = accumulated_temp1 + temperatures[n]; 
      accumulated_temp2 = accumulated_temp2 + lastweektemp [n]; 
     } 

     cout << endl << " Day This Week Last Week" << endl; 

     for (n=0; n<7; n++) 
     { 
      cout << n+1 << temperatures[n] << lastweektemp[n] << endl; 
     } 

     system("CLS"); 
     cout << "    Weather Report" << endl; 
     cout << "    --------------" << endl << endl; 
     cout << "Current Week: " << endl; 
     cout << "-------------" << endl; 
     for (n=0; n<7; n++) 
     { 
      cout << "Day " << n+1 << ": " << temperatures[n] << endl; 
     } 
     cout << endl << "   Average: " << accumulated_temp1/7 << endl; 
     cout << "   Highest Temperature: " << highest_temp1 << endl; 

     cout << "Last Week: " << endl; 
     cout << "----------" << endl; 
     for (n=0; n<7; n++) 
     { 
      cout << "Day " << n+1 << ": " << lastweektemp[n] << endl; 
     } 
     cout << endl << "   Average: " << accumulated_temp2/7 << endl; 
     cout << "   Highest Temperature: " << highest_temp2 << endl; 

     system("PAUSE"); 


    } 

在本週的最高溫度是24,但它正在打印「最高溫度:3.45857e + 032」
這個確切的「錯誤代碼」的出現我每次運行程序時它不會改變。

我是一個新手,所以我爲什麼不能上傳照片。

任何幫助,將不勝感激。我在大學裏做了一個小任務。這是我的第一個問題,所以很容易!

+4

_前初始化變量「是新手所以我爲什麼不能上傳照片。」 _所以,你可以在這裏你的錯誤代碼的文本複製到你的問題。無論如何,我們不想把它當作照片。 – 2014-12-04 14:29:41

+1

在調試器中遍歷代碼,以確切地查看使用未初始化變量的位置。另外,啓用編譯器警告來告訴你同樣的事情。 – 2014-12-04 14:48:31

回答

0

你還沒有分配到德變量highest_temp1任何價值,你與另一個值進行比較吧。

基本上你需要先爲它分配一個值,你比較之前..

highest_temp1 = 10.00 

(或任何它應該包含)

+0

謝謝你,我把它設置爲零。 「highest_temp1 = 0;」它工作完美。只是一個簡單的問題。在沒有初始化'highest_temp1變量的情況下,它在自己的源文件中執行時如何運行此程序?認真C++是我必須理解的第一語言,如果我感到困惑。 – 2014-12-05 21:18:46

+0

因爲沒有任何初始化默認情況下,浮動變量包含一些值(+/- 3.4E +/- 38(〜7個位數))..所以在運行時它不顯示:-) – 3Demon 2014-12-06 15:25:52

0

你還沒有初始化highest_temp1(或highest_temp1在那之後,我停止尋找)。

0

同爲accumulated_temp,這被不會被初始化。可以通過

float accumulated_temp1(0); 
-1

完成對浮點數病症使用的if語句開關不能在浮點數情況下工作,交換機只對整數工作。

+0

任何錯誤你甚至看這個問題? – 2014-12-04 14:55:52

0

使用它們

float highest_temp1(-FLT_MAX); // -FLT_MAX insures results of first compare 
float highest_temp2(-FLT_MAX); // Could use -1.0/0.0 of -INFINITY instead 
float accumulated_temp1(0.0); 
float accumulated_temp2(0.0);