2015-11-05 71 views
-2

他先謝謝你的幫助。我只是在爲我的學校做一件事,而我偶然發現了一個問題。這是一個簡單的賬單計算器,可以區分高級用戶和普通用戶,並根據不同的費率計算您的帳單。我以爲我已經完成了它,但現在我得到這個錯誤。請任何幫助,只是想得到這一切說和做。C++初學者編碼錯誤

調試錯誤!
程序:... \ ConsoleApplication12.exe
模塊:... \ ConsoleApplication12.exe

運行時檢查失敗#3 - T的

enter image description here

#include <iostream> 
#include<iomanip> 

using namespace std; 
int main() { 
const float Regular = 10; 
const float Premium = 25; 
const float RRate = .2; 
const float DRate = .1; 
const float NRate = .05; 
int account; 
int Rminutes; 
int Dminutes; 
int Nminutes; 
int totalmin; 
float Dcharge; 
float Ncharge; 
float total; 
char service; 
cout << "Enter the account number\n"; 
cin >> account; 
cout << "Enter the service code\n"; 
cin >> service; 

switch(service) 
{case'r': 
case'R': 
    cout << "Please enter the total amount of minutes used\n"; 
     cin >> Rminutes; 
     if (Rminutes > 50) { 
      total = (Rminutes - 50)*RRate + Regular; 
     } 
     else { 
      total = Regular; 
     } 
     break; 
case'P': 
case'p': 
    cout << "Please enter how many minutes were used during the day\n"; 
    cin >> Dminutes; 
    cout << "Please enter how many minutes were used during the night\n"; 
    cin >> Nminutes; 
    if (Dminutes > 75) { 
     Dcharge = (Dminutes - 75)*DRate; 
    } 
    if (Nminutes > 100) { 
     Ncharge = (Nminutes - 100)*NRate; 
    } 
    total = Premium + Dcharge + Ncharge; 
    break; 
default: 
    cout << "Invalid service code\n"; 
    return 1; 
    break; 
} 
totalmin = Rminutes + Dminutes + Nminutes; 
cout << "Your account number is:" << account; 
cout << "Your type of service is:" << service; 
cout << "your total minutes used was" << totalmin; 
cout << "your bill is" << total; 
return 0; 
} 
+6

您需要將其縮小到[mcve](https://stackoverflow.com/help/mcve)。 – Jason

+0

我只是運行Dev C++上的代碼,它的工作... xD –

+0

該代碼適用於我,輸出是: 您的帳號是:1您的服務類型是:使用的總分鐘數是23您的帳單是10 – anthr

回答

0

嘗試爲所有變量定義初始化值。此代碼也適用於Visual Studio:

#include <iostream> 
#include<iomanip> 

using namespace std; 
int main() { 
    const float Regular = 10; 
    const float Premium = 25; 
    const float RRate = .2; 
    const float DRate = .1; 
    const float NRate = .05; 
    int account=0; 
    int Rminutes=0; 
    int Dminutes=0; 
    int Nminutes=0; 
    int totalmin=0; 
    float Dcharge=0; 
    float Ncharge=0; 
    float total=0; 
    char service=0; 
    cout << "Enter the account number\n"; 
    cin >> account; 
    cout << "Enter the service code\n"; 
    cin >> service; 

    switch (service) 
    { 
    case'r': 
    case'R': 
     cout << "Please enter the total amount of minutes used\n"; 
     cin >> Rminutes; 
     if (Rminutes > 50) { 
      total = (Rminutes - 50)*RRate + Regular; 
     } 
     else { 
      total = Regular; 
     } 
     break; 
    case'P': 
    case'p': 
     cout << "Please enter how many minutes were used during the day\n"; 
     cin >> Dminutes; 
     cout << "Please enter how many minutes were used during the night\n"; 
     cin >> Nminutes; 
     if (Dminutes > 75) { 
      Dcharge = (Dminutes - 75)*DRate; 
     } 
     if (Nminutes > 100) { 
      Ncharge = (Nminutes - 100)*NRate; 
     } 
     total = Premium + Dcharge + Ncharge; 
     break; 
    default: 
     cout << "Invalid service code\n"; 
     return 1; 
     break; 
    } 
    totalmin = Rminutes + Dminutes + Nminutes; 
    cout << "Your account number is: " << account << "\n"; 
    cout << "Your type of service is: " << service << "\n"; 
    cout << "your total minutes used was " << totalmin << "\n"; 
    cout << "your bill is " << total; 
    system("PAUSE"); 
    return 0; 
} 
+0

非常感謝。那是因爲我沒有定義我的價值觀? – deawsum

+0

在其他編譯器中,您的代碼應該沒有任何問題地運行。但是,如果變量未初始化,Visual Studio可能會引發錯誤,但我不知道爲什麼。在其他編譯器中,您只需將變量垃圾回收而不會導致任何錯誤。 –

+0

這就是爲什麼我使用Dev C++編譯代碼時運行代碼的原因。 –