visual C++.程序在输入后继续退出

visual C++. Program keeps exiting after input

本文关键字:继续 退出 输入 C++ 程序 visual      更新时间:2023-10-16

我的代码有问题。所以我的作业是这样问的:

一家移动电话服务公司为其客户提供三种不同的订阅套餐:

套餐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;

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

  1. 你要求对方输入一封信,但却试图以双重格式(信件)存储。您最好将其存储为字符串,然后稍后将其转换为字符(为了程序兼容性),或者只是请求一个字符作为开头

char选项="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;
  1. 验证检查。目前,您有几个验证检查,包括有效的分钟数,是否应收取额外时间等费用。您的时间验证应为:

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

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

if(time < 450)

应将其更改为:

if (time <= 450)

由于计划中包含450分钟。(对于900分钟的计划,也应该对此进行更改)。

改进包括:

  • 如果他们选择选项C,从逻辑上讲,他们不应该输入他们的分钟数——他们有无限的时间,他们输入的任何数字都不会影响他们的数量(尽管为了简单和/或任务要求,我可以理解其中的内容)
  • 更改某些名称以提高可读性,例如将字母更改为"成本/总额"
  • 当他们没有输入a、B或C时,添加某种"您输入了一封未接受的计划信函"消息