2014-09-24 33 views
-2

我的代码有问题。所以,我的功课问这个:C++。程序在输入后保持退出

移动电话服务提供RHAS为客户三个不同的包月套餐:

套餐A:对于每月$ 39.99提供450分钟。额外的分钟是每分钟0.45美元。

套餐B:每月59.99美元,提供900分钟。额外的分钟是每分钟0.40美元。

包C:为每月69.99美元提供无限分钟提供。

编写一个计算客户每月账单的程序。它应该询问客户购买了哪个包裹以及使用了多少分钟。它应该显示应付的总金额。

输入验证:确保用户只选择套餐A,B或C.

这是我的代码吧:

/* 

1. Set variables (chars, int, etc) for hours and fees. 
2. Ask user to select between A, B, or C. 
3. Ask user to input time. 
Also set a maximum amount of time for each case and setting a maximum amount of time for the  month. 
5.Use case switch for options 
6.calculate the customers bill for the month apprioprately. 

*/ 


#include <iostream> 
#include <iomanip> 
#include <cmath> 

using namespace std; 

int main() 
{ 
const double packageA = 39.99;//set variables for all three. They don't change. 
const double packageB = 59.99; 
const double packageC = 69.99; 
char choices = ' '; // use "char" for the packages(choices) 
int time = 0; // set time as integer. Since some may be decimals, I use double. 
double letter = 0.0; //use "letter" for whatever letter they choose. 
cout << "Read choices below and select choice."<<endl; 
cout << "A.$39.99 per month gets 450 minutes. Additional minutes are $0.45 per minute." << endl; 
cout << "B.$59.99 per month gets 900 minutes. Additional minutes are $0.40 per minute." << endl; 
cout << "C.$69.99 per month gets you unlimited access" << endl; 


cout << "Select A, B, or C" << endl; 
cin >> letter; 

if (choices == 'A' || choices == 'B' || choices == 'C')//using switch case 
{ 

    cout << "Enter minutes:" << endl;//ask user to input time 
    cin >> time; 


    if (time>0 && time<43829)// 43829 is the max amount of minutes in a month. 0 is the least they person can have. If it fits the requirements, then it can continue. 

    { 

     switch (choices) 
     { 
     case 'A': 
      if (time<450) 
       letter = packageA;// if the time is less than required. Then no extra charge. 
      else 
       letter = ((time - 450)*0.45) + packageA;// if it exceeds maximum minutes and 45 cents is charged. Same for all cases below except its respective amount is charged. 
      break; 
     case 'B': 
      if (time<900) 
       letter = packageB; 
      else 
       letter = ((time - 900)*.40) + packageB; 
      break; 
     case 'C': 
      letter = packageC;// if not, then package C and no equation since time is unlimited. It is a one time fee for all time used. 
      break; 


     default: cout << "Total amount due is: $" << letter << endl; // give total amount charged based on information entered. 
     } 
    } 
    system("pause"); 
    return 0; 
} 

} 

我的问题是,当我运行它,它会在我选择一封信后关闭。如果我选择A,它会自动关闭。关闭后我收到以下消息:

'ConsoleApplication7.exe'(Win32):Loaded'C:\ Users \ Prince \ Documents \ Visual Studio 2013 \ Projects \ ConsoleApplication7 \ Debug \ ConsoleApplication7.exe'。符号加载。

'ConsoleApplication7.exe'(Win32):Loaded'C:\ Windows \ SysWOW64 \ ntdll.dll'。找不到或打开PDB文件。

'ConsoleApplication7.exe'(Win32):Loaded'C:\ Windows \ SysWOW64 \ kernel32.dll'。找不到或打开PDB文件。

'ConsoleApplication7.exe'(Win32):Loaded'C:\ Windows \ SysWOW64 \ KernelBase.dll'。找不到或打开PDB文件。

'ConsoleApplication7.exe'(Win32):Loaded'C:\ Program Files \ Bitdefender \ BitDefender 2015 \ active virus control \ Avc3_00259_008 \ avcuf32.dll'。找不到或打开PDB文件。

'ConsoleApplication7.exe'(Win32):Loaded'C:\ Windows \ SysWOW64 \ msvcp120d.dll'。找不到或打开PDB文件。

'ConsoleApplication7.exe'(Win32):Loaded'C:\ Windows \ SysWOW64 \ msvcr120d.dll'。找不到或打开PDB文件。

程序'[3476] ConsoleApplication7.exe'已退出,代码为0(0x0)。

+5

难道你不想'cin >>选择'? – quantdev 2014-09-24 05:17:07

+0

听起来好像是时候学习使用调试器了。 – Biffen 2014-09-24 05:17:28

+0

错误消息表明您的防病毒软件也可能会造成干扰,请尝试将其关闭 – 2014-09-24 05:37:33

回答

1
cout << "Select A, B, or C" << endl; 
cin >> letter // (*); 

的问题是由(*)标线:您要同时封信是double类型的输入字符串数据。将字母更改为字符串或字符。并且不要将它设置为0.0或其他任何东西。刚刚尝试是这样的:

String letter; 

char letter; 

我还注意到,您有char choice = '';所以你可以使用cin >> choice而不是cin >> letter;

+0

现在我输入分钟后关闭。但同样的错误。 – user2221218 2014-09-24 18:24:01

+0

@ user2221218好的,所以当你创建变量时间时,在你说的时间可能有小数的评论中,所以你声明它是双重的,但实际上你并不是。看看你的代码它说int time = 0;那不是双倍。你应该像这样宣布:double time;试试这个,让我知道会发生什么 – 2014-09-25 03:05:12

0

好的,所以在代码中有几个问题 - 一些程序杀手,以及一些逻辑和读取的改进。我们将从程序杀手开始

  1. 您要求该人输入一个字母,但试图以双格式(字母)存储它。你最好把它存储到一个字符串中,然后将它转换为一个字符(为了程序的兼容性),或者仅仅是要求一个字符开始。

char choices ='A'; //虽然你可以更好地调用它像“套餐”,为便于阅读,后来

然后,当你问他们的输入,也行是:

cout << "Select A, B, or C" << endl; 
cin >> choices; 

的优势使用一个字符就是你可以在下面的switch语句中使用它(和你一样)。

  1. 下一个问题是您在交换机中错误地使用了默认情况。默认情况是switch语句与其他选择不匹配时会发生的情况(例如,如果它们输入的内容不是'A','B'或'C')要使其正确工作,希望你的程序输出的switch语句后的总金额。
switch (choices) 
{ 
case 'A': 
    // do something 
    break; 
case 'B': 
    // do something 
    break; 
case 'C': 
    // do something 
    break; 
}; 
cout << "Total amount due is: $" << letter << endl; 
  • 验证检查。目前,你有几个验证检查,其中包括有效的金额的分钟,如果他们收取额外的时间,等等。您的时间验证应该是:

    如果(时间> = 0 & &时间< 43829)

  • 主要是因为0是已经使用的分钟的有效数字。另外,31天的月份有44640分钟。 30天= 43200分钟。 43829从哪里来? 此外,在您的switch语句,你检查

    if(time < 450) 
    

    这应改为:

    if (time <= 450) 
    

    450分钟未列入计划。 (这也应该改变900分钟的计划)。

    改进包括:

    • 如果他们选择选项C,从逻辑上讲,他们不应该进入他们分钟 - 他们有无限的,没有他们进入数量会影响他们的量(虽然我能理解包括它为简单和/或任务要求)。
    • 更改一些名称的可读性,如更改字母为“成本/总计”
    • 当他们未输入A,B或C时,添加某种“您输入了未接受的计划字母”消息。
    相关问题