2014-09-24 34 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時,添加某種「您輸入了未接受的計劃字母」消息。
    相關問題