2015-11-05 97 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++编译代码时运行代码的原因。 –