visual C++.程序在输入后继续退出
visual C++. Program keeps exiting after input
我的代码有问题。所以我的作业是这样问的:
一家移动电话服务公司为其客户提供三种不同的订阅套餐:
套餐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):已加载"C:\Users\Prince\Documents\Visual Studio 2013\Projects\ConsoleApplication7\Debug\ConsoleApplication7.exe"。已加载符号。
"ConsoleApplication7.exe"(Win32):已加载"C:\Windows\SysWOW64\ntdll.dll"。找不到或打开PDB文件。
"ConsoleApplication7.exe"(Win32):已加载"C:\Windows\SysWOW64\kernel32.dll"。找不到或打开PDB文件。
"ConsoleApplication7.exe"(Win32):已加载"C:\Windows\SysWOW64\KernelBase.dll"。找不到或打开PDB文件。
"ConsoleApplication7.exe"(Win32):已加载"C:\Program Files\Bitdefender\Bitdefender 2015\active virus control\Avc3_00259_008\avcuf32.dll"。找不到或打开PDB文件。
"ConsoleApplication7.exe"(Win32):已加载"C:\Windows\SysWOW64\msvcp120d.dll"。找不到或打开PDB文件。
"ConsoleApplication7.exe"(Win32):已加载"C:\Windows\SysWOW64\msvcr120d.dll"。找不到或打开PDB文件。
程序"[3476]ConsoleApplication7.exe"已退出,代码为0(0x0)。
cout << "Select A, B, or C" << endl;
cin >> letter // (*);
问题出现在(*)
标记的行上:当字母的类型为double时,您正试图输入字符串数据。将字母改为字符串或字符。并且不要将其设置为0.0或其他任何值。试试这样的东西:
String letter;
或
char letter;
我还注意到您有char choice = '';
,所以您可以使用cin >> choice
而不是cin >> letter;
。
好的,所以代码中有几个问题——一些程序杀手,以及一些逻辑和读取方面的改进。我们将从程序杀手开始
- 你要求对方输入一封信,但却试图以双重格式(信件)存储。您最好将其存储为字符串,然后稍后将其转换为字符(为了程序兼容性),或者只是请求一个字符作为开头
char选项="A";//尽管为了以后在上的可读性,你最好把它称为"包"
然后,当你要求他们输入时,行是:
cout << "Select A, B, or C" << endl;
cin >> choices;
使用字符的优点是,您也可以在下面的switch语句中使用它(就像您自己一样)。
- 下一个问题是,您在交换机中错误地使用了默认情况。默认情况是当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时,添加某种"您输入了一封未接受的计划信函"消息
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 循环后如何继续阅读
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- 如何让LLDB在成功时退出,在失败时等待
- C++控制台应用程序阻止退出
- 程序在执行程序的其余部分之前退出
- 构造函数在退出函数时无法初始化一个参数
- 继续运行程序而无需任何干预,直到要求退出为止
- 如何退出函数,但继续程序的其余部分
- 使用Enter继续前进并ESC退出C
- 如何在 C++ 中继续该函数而不退出 after else 语句
- 在C++中添加while循环以继续或退出控制台
- 在父母获得杀死信号和退出后,用system()产生的儿童进程继续运行
- QT/C++,OS X:退出应用程序时可选择隐藏(),以便应用程序继续运行,并在单击dock中的图标后再次显示
- C++如何继续主,直到用户选择退出选择
- visual C++.程序在输入后继续退出
- 程序在退出(0)后似乎继续运行
- c++ 11线程是否提供了一种在主线程退出后分离线程继续的方法?