如何获得正确的更改量? [英] How do I get the correct amount of change?

查看:71
本文介绍了如何获得正确的更改量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的代码中识别问题,这是一个购票计划,我想假设正在改变,但计算错误的金额,我无法弄清楚原因。以下是代码:

  //  作者:John Moye  
// 源文件:amuseProj.cpp
< span class =code-comment> // 描述:游乐园项目
// 使用的IDE:XCode

#include < iostream >
#include < iomanip >
使用 命名空间标准;

// 常数

const double CHILDPRICE = 12 00 ;
const double ADULTPRICE = 20 50 ;

int main()
{

/ / 整数

int childTix;
double childTotal;
double changeTotal;
int adultTix;
double totalBill;
double adultTotal;
double cashReceived;
int ticketTotal;
int confirmN;




// title

cout<< \ n Chesapeake Amusement Park<< endl<< ENDL;




cout<< \ n输入子票(输入-1表示停止)....;
confirmN = 99 ;
cin>> childTix;

while (childTix> 0

{

childTix = childTix - (childTix / 4);






cout<< \ n输入成人票......;

cin>> adultTix;



adultTotal =(adultTix * ADULTPRICE) - 3 ;


confirmN ++;








ticketTotal = adultTix + childTix;


childTotal = childTix * CHILDPRICE;

adultTotal = adultTix * ADULTPRICE;

totalBill = adultTotal + childTotal;


















cout.setf(ios :: fixed);
cout.setf(ios :: showpoint);
cout.precision( 2 );

cout<< \ n \切萨皮克游乐园;
cout<< \ n ---------------------- ---;
cout<< \ n \ n门票价格总计\ n;



cout<< 儿童<< setw( 3 )<< childTix
<< setw( 14 )<< CHILDPRICE
<< setw( 11 )<< childTotal;

cout<< \ n Adults<< setw( 3 )<< adultTix
<< setw( 14 )<< ADULTPRICE
<< setw( 11 )<< adultTotal;



cout<< \ nn \\ n总票数:<< setw( 3 )<< ticketTotal;


if ((ticketTotal> 20 )|| (childTix> = 14 ))
cout<< \ n \ n总票据......<< setw( 11 )<< totalBill + 15;




else
cout<< \ n \ n总票据......<< setw( 11 )<< totalBill;



cout<< \ n \ n输入您要支付的金额:;

cin>> cashReceived;



cout<< \ n\ n收到现金<< setw( 11 )<< cashReceived;



do
{
cout<< \ n \ n收到的现金不够,请给出总额;
cin>> cashReceived;

}
while (cashReceived< totalBill);
cout<< \ n \ n谢谢;




changeTotal = cashReceived - totalBill;

cout<< \ n \ n更改<< setw( 11 )<< changeTotal;
cout<< \ n \ n确认号码:<< setw( 11 )<< confirmN;



cout<< \ n \ n输入子票(输入-1表示停止).... << setw( 11 );

cin>> childTix;

}

cout<< \ n \ n很快再回来<< setw( 11 );





返回 0 ;

}

[edit]已添加代码块 - OriginalGriff [/ edit]



我有什么试过:



我试过制作新的双打并将代码移到不同的地方

解决方案

< blockquote>首先整理你的代码,这样你就可以看到发生了什么!摆脱所有空白行,正确缩进。



然后,是时候看看发生了什么,因为制作新的双打并将代码移动到不同的位置并不是一个可行的开发策略。 >


编译并不意味着你的代码是正确的!:笑:

把开发过程想象成写一封电子邮件:成功编译意味着你写了用正确的语言发送电子邮件 - 英语,而不是德语 - 不是电子邮件中包含您要发送的邮件。



所以现在你进入第二阶段开发(实际上它是第四或第五,但你会在之后的阶段进入):测试和调试。



首先看看它做了什么,以及它与你想要的有何不同。这很重要,因为它可以为你提供有关它为什么这样做的信息。例如,如果一个程序是为了让用户输入一个数字并将其翻倍并打印出答案,然后如果输入/输出是这样的:

输入预期输出实际输出
1 2 1
2 4 4
3 6 9
4 8 16

然后很明显,问题在于将它加倍的位 - 它不会将自身加到自身上,或者将它乘以2,它将它自身相乘并返回输入的平方。

因此,您可以查看代码,很明显它就在这里:

  int   Double  int   value 
{
return value * ;
}



一旦你知道可能出现的问题,就开始使用teh调试器找出原因。在您的行上设置一个断点:

  while (childTix  >   0 



并运行您的应用。在执行代码之前,请考虑代码中的每一行应该做什么,并将其与使用Step over按钮依次执行每一行时实际执行的操作进行比较。它符合您的期望吗?如果是这样,请转到下一行。

如果没有,为什么不呢?它有何不同?



这是一项非常值得开发的技能,因为它可以帮助你在现实世界和发展中。和所有技能一样,它只能通过使用来改善!



是的,我可能会告诉你问题是什么 - 但这并不难做到,并且你将同时学到一些非常值得的东西!


引用:

我需要识别在我的代码中出现问题,这是一个购票计划,我想要改变,但错误的金额正在计算中,我无法弄清楚原因。



你只是忘了告诉我们你的代码应该做什么改变是错误的。我们无法知道你的期望。

引用:

我试过制作新的双打并移动代码到不同的地方



随机变化不是一个可行的解决方案。唯一的方法是了解代码是如何错误的。调试器是一个工具,可以让你看到代码在做什么,并在代码执行时检查变量。



有一个工具可以让你看到你的代码正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


I need to identify the problem in my code, it is a ticket purchasing program and im suppose to be getting change but the wrong amount is being calculated and I cant figure out why. Here is the code:

// Author:			John Moye
// Source file:		amuseProj.cpp
// Description: Amusement Park Project
// IDE used: XCode

#include <iostream>
#include <iomanip>
using namespace std;

 // Constants

const double CHILDPRICE = 12.00;
const double ADULTPRICE = 20.50;

int main ()
{
    
    // integers
   
    int childTix;
    double childTotal;
    double changeTotal;
    int adultTix;
    double totalBill;
    double adultTotal;
    double cashReceived;
    int ticketTotal;
    int confirmN;
    
    
    
    
    //title
    
    cout << "\n           Chesapeake Amusement Park" << endl << endl;
    
    
    
    
    cout << "\n  Enter children tickets (enter -1 to stop)....";
    confirmN= 99;
    cin >>childTix;
    
    while (childTix > 0)
        
    {
    
    childTix = childTix - (childTix/4);
    
    
    
    
    
    
    cout << "\n  Enter adult tickets...";
    
    cin >>adultTix;
        
  
  
      adultTotal = (adultTix * ADULTPRICE) - 3;
  
        
        confirmN ++;
        
        

        
  
       
    
    
    ticketTotal = adultTix + childTix;
        
 
    childTotal = childTix * CHILDPRICE;
    
    adultTotal = adultTix * ADULTPRICE;
    
    totalBill = adultTotal + childTotal;
    
        
    
    
        
        
        

        
    
        
  
    
    
    
    
    
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    cout << "\n\n                 Chesapeake Amusement Park";
    cout << "\n                 -------------------------";
    cout << "\n\n               Tickets      Price      Total\n";
    
    
    
    cout << "     Children   " << setw(3)  << childTix
                               << setw(14) << CHILDPRICE
                               << setw(11) << childTotal;
    
    cout << "\n     Adults     " << setw(3)  << adultTix
                               << setw(14) << ADULTPRICE
                               << setw(11) << adultTotal;
        
        
        
    cout <<"\n\n    Total Tickets:" << setw(3) << ticketTotal;
    
        
    if ( (ticketTotal > 20) || (childTix >= 14))
        cout << "\n\n Total Bill...               "<< setw(11)  << totalBill+15;
        
       
        
        
    else
      cout << "\n\n     Total Bill...               " << setw(11)  <<totalBill;
    
   
    
    cout << "\n\n Enter the amount you're going to pay:";
    
    cin >>cashReceived;
    
      
    
    cout << "\n\n     Cash Received              "<< setw(11)  <<cashReceived;
     
        
       
        do
        {
            cout << "\n\n Cash received is not enough, please give the total amount";
            cin >> cashReceived;
            
        }
        while (cashReceived < totalBill);
        cout << " \n\n Thank you";
        
        
        
       
    changeTotal = cashReceived - totalBill;
    
    cout << "\n\n     Change                      " << setw(11)  << changeTotal;
    cout << "\n\n     Confirmation Number:        " << setw(11)  << confirmN;
   
    
        
        cout << "\n\n     Enter children tickets (enter -1 to stop)...." << setw(11);
        
        cin >> childTix;
    
    }
    
    cout << "\n\n     Come back again soon        " << setw(11);
    
              
    
    
    
return 0;

}

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

Ive tried making new doubles and moving the code around to different spots

解决方案

Start by tidying up your code so you can see what is going on! Get rid of all those spare blank lines, indent it properly.

Then, it's time to see what is going on, because "making new doubles and moving the code around to different spots is not a viable development strategy.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:

Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:

int Double(int value)
   {
   return value * value;
   }


Once you have an idea what might be going wrong, start using teh debugger to find out why. Put a breakpoint on your line:

while (childTix > 0)


and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Yes, I could probably tell you what "the problem" is - but it's not difficult to do this yourself, and you will learn something really worthwhile at the same time!


Quote:

I need to identify the problem in my code, it is a ticket purchasing program and im suppose to be getting change but the wrong amount is being calculated and I cant figure out why.


You only forgot to tell us what your code is supposed to do an in what the change is wrong. We have no way to know what you expect.

Quote:

Ive tried making new doubles and moving the code around to different spots


Random changes is not a viable solution. The only way to go is to understand how the code is wrong. The debugger is a tool that allow you to see what your code is doing and to inspect variables as code execute.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何获得正确的更改量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆