2016-09-27 248 views
-4

所以我正在寫一個處理輸入文件中的值的程序。我的變量包括total,taxtotal,subtotal等。&它們已經被聲明和初始化,但我收到兩條錯誤消息:「未初始化的局部變量」使用的小計「和變量」taxtotal「相同。 的源代碼:實際初始化的未初始化的局部變量?

#include "stdafx.h" 
#include<stdio.h> 
#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <string> 
using namespace std; 

int main() 
{ 

    ifstream shoppingBasketFile; 
    shoppingBasketFile.open("HW3_Data.txt"); 
    bool isTaxed = false; 
    char taxValue = 0; 
    char inPrice[64]; 
    char name[128]; 
    double price, taxtotal, subtotal, total = 0; 

    if (shoppingBasketFile.is_open()) 
    { 
     // display the header info: 
     cout << "o Thank you for shopping at StuffMart" << endl; 
     cout << setw(3) << left << "o " 
      << setw(20) << left << "Item" 
      << setw(12) << "Unit Price" 
      << setw(4) << "Tax" 
      << endl 
     << "o -------------------------------------" << endl; 
     // parse the input file until end of file; 
     while (!shoppingBasketFile.eof()) 
     { 

      // parse the item name: 
      shoppingBasketFile >> name; 
      cout << "Name = " << name << endl; 
      if (name == NULL) 
      { 

       // what should we really do here? 
       continue; 
      } 


      // parse the price: 
      shoppingBasketFile >> price; 
      if (price < 0 || price > 100000000000) { 
       continue; 
      } 
      cout << "Price = " << price << endl; 

      // parse the isTax flag: 
      shoppingBasketFile >> isTaxed; 
      shoppingBasketFile >> taxValue; 
      cout << "Is taxed? = " << taxValue << endl; 
      // if end of file break out of this loop: 
      if (!shoppingBasketFile.good()) break; 
      if (isTaxed == true) { 
       taxtotal = taxtotal + (.085 * price); 
       taxValue = 'Y'; 
      } 
      else { 
       taxValue = 'N'; 

      } 
      //display tax as Y instead of T/1 
      if (isTaxed == true) { 
       cout << "Tax: Y" << endl; 
      } 
      else { 
       cout << "Tax: N" << endl; 
      } 
      //compute the subtotals 
      subtotal = subtotal + price; 
      // display the item info:  
      cout << "name" << name << ", price: $" << price << ", is taxed: " << taxValue << endl; 


      // reset input values: 
      name, price, isTaxed = 0; 
      // end of while loop 
     } 
     //compute the final total: 
     total = subtotal + taxtotal; 
     //output the totals 
     cout << "o" << setw(37) << "---------------" << endl 
      << "o " << setw(26) << "Subtotal $" << fixed << setprecision(2) << right << subtotal << endl 
      << "o " << setw(26) << "Tax (8.5%) $" << fixed << setprecision(2) << right << taxtotal << endl 
      << "o " << setw(26) << "Total $" << fixed << setprecision(2) << right << total << endl; 
    } 


shoppingBasketFile.close(); 
return 0; 
} 

任何提示將不勝感激!

+3

_「它們已被聲明並初始化」_這些變量未被初始化。 –

+1

請停止嘗試通過試錯來學習C++,它會讓你無處可去。相反,從一本好書中系統地學習它。 –

+1

請停止假設我如何學習C++,假設會讓你無處可去。我其實是在上一堂課,指的是一本好書,並有兩位導師。不過謝謝你。 –

回答

4

看起來你宣佈在發言這裏subtotal

double price, taxtotal, subtotal, total = 0; 

但只有初始化total值爲0,導致其在賦值的右側使用觸發錯誤:

subtotal = subtotal + price; 

要初始化多個項目,只需顯式添加「=」。 例子:

double price = 0, taxtotal = 0, subtotal = 0, total = 0; 
+0

這有效!我其實曾經嘗試過這一點,但是我又遇到了另一個阻礙我獲得足夠結果的錯誤。對C++還是很新的,所以謝謝你幫我排除故障! :) –

6

在此聲明:

double price, taxtotal, subtotal, total = 0; 

類型名稱double適用於所有4個變量,但= 0初始化僅適用於total

正如其他人所說,最直接的解決方法是:

double price = 0, taxtotal= 0, subtotal = 0, total = 0; 

但最好的風格聲明對自己的行每個變量:

double price = 0.0; 
double taxtotal = 0.0; 
double subtotal = 0.0; 
double total = 0.0; 

注意,使用0是完全合法的( int值將被隱式轉換爲double0.0),但使用浮點常量更加明確。

(我選擇了垂直對齊的初始化。有些人可能不喜歡這樣做。)

我猜你沒有得到的指針呢。當你這樣做時,你會遇到另一個原因來聲明每個變量在自己的行上。此:

int* x, y, z; 

x定義爲一個int*,但yzint。採用每行一個聲明,如對上述初始化,避免了這個機會,錯誤和混亂:

int* x; 
int* y; 
int* z; 

一旦你的代碼編譯,你必須與該行的一個問題:

name, price, isTaxed = 0; 

這是一個有效的陳述,但它不會做你認爲它的作用。

,逗號運算符。它按順序評估它的左和右操作數,併產生右操作數的值,丟棄左操作數的值。該聲明評估並丟棄當前值name,然後評估並丟棄price的當前值,然後將值0指定爲isTaxed。 (感謝user4581301指出這一點。)

你可以這樣寫:

name = price = isTaxed = 0; 

(因爲轉讓產生已分配的值),或者更簡單地說,如:

// name = 0; 
price = 0.0 
isTaxed = false; 

我已將註釋分配給name,因爲它是一個數組,並且您不能將值分配給數組對象。我不會顯示正確的版本,因爲我不知道你在這裏做什麼。

建議:從小開始,保持簡單,並在每一步確認您的代碼在添加新代碼之前工作。我想你已經試過一次寫太多的代碼。你有近100行代碼甚至不能編譯。我一直在編程一個long的時間,我不會編寫那麼多的代碼,但不確保它編譯和運行。