我的代码有问题。所以我的作业问了这个:
移动电话服务为其客户提供三种不同的订阅套餐:
套餐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,它会自动关闭。关闭后我收到此消息:
&#39; ConsoleApplication7.exe&#39; (Win32):已加载&#39; C:\ Users \ Prince \ Documents \ Visual Studio 2013 \ Projects \ ConsoleApplication7 \ Debug \ ConsoleApplication7.exe&#39;。符号已加载。
&#39; ConsoleApplication7.exe&#39; (Win32):已加载&#39; C:\ Windows \ SysWOW64 \ ntdll.dll&#39;。无法找到或打开PDB文件。
&#39; ConsoleApplication7.exe&#39; (Win32):已加载&#39; C:\ Windows \ SysWOW64 \ kernel32.dll&#39;。无法找到或打开PDB文件。
&#39; ConsoleApplication7.exe&#39; (Win32):已加载&#39; C:\ Windows \ SysWOW64 \ KernelBase.dll&#39;。无法找到或打开PDB文件。
&#39; ConsoleApplication7.exe&#39; (Win32):已加载&#39; C:\ Program Files \ Bitdefender \ Bitdefender 2015 \ active virus control \ Avc3_00259_008 \ avcuf32.dll&#39;。无法找到或打开PDB文件。
&#39; ConsoleApplication7.exe&#39; (Win32):已加载&#39; C:\ Windows \ SysWOW64 \ msvcp120d.dll&#39;。无法找到或打开PDB文件。
&#39; ConsoleApplication7.exe&#39; (Win32):已加载&#39; C:\ Windows \ SysWOW64 \ msvcr120d.dll&#39;。无法找到或打开PDB文件。
程序&#39; [3476] ConsoleApplication7.exe&#39;已退出代码0(0x0)。
答案 0 :(得分:1)
cout << "Select A, B, or C" << endl;
cin >> letter // (*);
问题是在(*)
标记的行上:您正在尝试输入字符串数据,而字母是double类型。将字母更改为字符串或字符。并且不要将其设置为0.0或其他任何内容。试试这样的事情:
String letter;
或
char letter;
我还注意到您有char choice = '';
,因此您可以使用cin >> choice
代替cin >> letter;
。
答案 1 :(得分:0)
好的,所以代码中存在一些问题 - 一些程序杀手,以及一些逻辑和阅读改进。我们将从程序杀手开始
char choices ='A'; //虽然你最好把它称之为“包”,但为了以后的可读性
然后,当您要求输入时,请输入以下行:
cout << "Select A, B, or C" << endl;
cin >> choices;
使用角色的好处是你也可以在下面的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;
验证检查。目前,您需要进行多次验证检查,包括有效的分钟数,是否需要支付额外的时间等。您的时间验证应该是:
if(time&gt; = 0&amp;&amp; time&lt; 43829)
主要是因为0是使用的有效分钟数。此外,31天的月份有44640分钟。 30天= 43200分钟。 43829来自哪里? 另外,在switch语句中,检查
if(time < 450)
这应该改为:
if (time <= 450)
计划中包含450分钟。 (这也应该针对900分钟的计划进行更改。)
改进包括: