我可以运行此代码,但我无法选择哪种货币等。请帮我解决这个问题! [英] I can run this code but I cannot select which currency etc. Please help me to fix this problem!

查看:82
本文介绍了我可以运行此代码,但我无法选择哪种货币等。请帮我解决这个问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main ()
{
    int A,B,C,D,E,F,bndollar,usdollar,eurodollar,myrdollar,bnd,usd,eur,myr,currency,n,N,y,Y;
    char choice;
    double number;
    while (choice);
    
    
    {
         
    cout << "CURRENCY CONVERTER\n" <<endl;
    cout << "Choose a foreign currency\n" <<endl;
	cout << "1. BND to USD\n"  <<endl;
	cout << "2. USD to BND\n"  <<endl;
	cout << "3. BND to EUR\n"  <<endl;
	cout << "4. EUR to BND\n"  <<endl;
	cout << "5. BND to MYR\n"  <<endl;	
	cout << "6. MYR to BND\n"  <<endl;
	{
    cout << "\nPlease enter the number of the currency you want to convert: ";
    cin >> currency;

    }
    if (choice == 1)
    {  
       cout << "Please enter the amount of BND you would like to convert to USD: ";
       cin >> bndollar;
       cout << "\nYou have entered " << bndollar << "bnd is equal to " << bndollar*0.714950 << "usd." <<endl; 
    }
    
    else if (choice == 2)
    {
         cout << "Please enter the amount of USD you would like to convert to BND: ";
   	     cin >> usdollar;
         cout << "\nYou have entered " << usdollar << "usd is equal to " << usdollar*1.39870 << "usd." <<endl;
    }   
       
    else if (choice == 3)
    {
         cout << "Please enter the amount of BND you would like to convert to EUR: ";
         cin >> bndollar;
         cout << "\nYou have entered " << bndollar << "bnd is equal to " << bndollar*0.661654 << "bnd." <<endl;	    
    }         
       
       
    else if (choice == 4)
    {
         cout << "Please enter the amount of EUR you would like to convert to BND: ";
     	 cin >> eurodollar;
         cout << "\nYou have entered " << eurodollar << "euro is equal to " << eurodollar*1.51136 << "euro." <<endl;	  
    }         
       
    else if (choice == 5)
    {
          cout << "Please enter the amount of BND you would like to convert to MYR: ";
   	      cin >> bndollar;
          cout << "\nYou have entered " << bndollar << "bnd is equal to " << bndollar*3.16401 << "bnd." <<endl;	   
    }   
          
    else if (choice == 6)
    {
            cout << "Please enter the amount of MYR you would like to convert to BND: ";
            cin >> myrdollar;
            cout << "\nYou have entered " << myrdollar << "myr is equal to " << bndollar*0.316055 << "myr." <<endl;	
    }
    
 
    cout << "\n\n\n\nDo you wish to convert again? (Y/N) ";
    cin >> choice;
}

    if (choice != n && choice !=N)
    {
    cout << "\n\n\n\n\n\n\n\nThank You and Goodbye\n";
}
    
    {
   if (choice == y && choice == Y)
     
            cout << "\n";
     }

    system ("pause");
	return 0;  

}





PLease帮助我这里有什么问题?



我尝试了什么:



当我尝试运行它时,它会变成这样:



CURRENCY CONVERTER



选择外币



1. BND到USD



2. USD到BND



3. BND到EUR



4. EUR到BND



5. BND到MYR



6. MYR到BND





请输入您要转换的货币数量:1









您想再次转换吗? (是/否)n

















谢谢你和再见

按任意键继续。 。 。



PLease help me what's the problem here?

What I have tried:

When i try to run it, it becomes like this:

CURRENCY CONVERTER

Choose a foreign currency

1. BND to USD

2. USD to BND

3. BND to EUR

4. EUR to BND

5. BND to MYR

6. MYR to BND


Please enter the number of the currency you want to convert: 1




Do you wish to convert again? (Y/N) n








Thank You and Goodbye
Press any key to continue . . .

推荐答案

首先,正确缩进代码:难以阅读并弄清楚发生了什么。当你做这样的事情时,这尤其是一个问题:

First off, indent your code properly: that is hard to read and work out what is going on. This is particularly a problem when you do things like this:
    if (choice != n && choice !=N)
    {
    cout << "\n\n\n\n\n\n\n\nThank You and Goodbye\n";
}
    
    {
   if (choice == y && choice == Y)
     
            cout << "\n";
     }





其次,这个测试很愚蠢:



Second, this test is silly:

if (choice != n && choice !=N)

因为 choice 是char,而且 n N 是整数,两者都没有得到值。这同样适用于您的是测试。可能你应该转储变量 n N y Y 并使用如下测试:

because choice is char, and both n and N are integers, neither of which gets a value. The same applies to your "Yes" test. Probably, you should dump the variables n , N, y, and Y and use tests like this:

if (choice != 'n' && choice != 'N')
    {
    cout << "\n\n\n\n\n\n\n\nThank You and Goodbye\n";
    }





第三,即使您修复了字符,是问题中的AND文本也不起作用:



Thirdly, the AND text in your "Yes" question won't work, even if you fix the characters:

if (choice == 'y' && choice == 'Y')

变量不能有两个不同的值同时。你想用||作为OR操作而不是&&作为AND。



第四,你认为你的while循环应该在哪里结束?修复缩进应该可以帮助你看到那里出了什么问题...



请看看那段代码:你正在复制一堆你能做的东西容易重构为更易读和可维护的形式。如果比较,还要考虑使用开关而不是加载inf。

a variable cannot have two different values at the same time. You want to use "||" as an OR operation instead of "&&" as an AND.

Fourthly, where do you think your while loop should end? Fixing the indentation should help you see what is wrong there...

And please, have a look at that code: you are duplicating a load of stuff that you could easily refactor into more readable - and maintainable - form. Also consider using a switch instead of loads inf if comparisons.


引用:

请问我这里有什么问题?

PLease help me what's the problem here?



问题很多,你有未初始化的变量

这里的分号阻止了循环来做任何事情因为循环是空的。


The problems are numerous, you have uninitialized variables
The semicolon here prevent the loop from doing anything because the loop is empty.

while (choice);



并且您没有因为没有设置 choice 的值而被困在这一行。

对于进一步的问题,使用调试器来查看你的代码正在做什么。



当你不明白你的代码在做什么或为什么它做什么确实如此,答案是调试器

使用调试器查看代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



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

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

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



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

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


and you are not stuck in this line just by luck because you didn't set the value of choice.
For further problems, use the debugger to see what you code is doing.

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, it is an incredible learning tool.

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.


怎么样:

What about:
#include <iostream>
#include <string>
#include <array>
using namespace std;

struct ConversionInfo
{
  string currency[2];
  double ratio;
};

int prompt_user( const array<ConversionInfo,3> & cia)
{
  int count=1;
  cout << "please enter your choice" << endl;
  for (auto ci : cia)
  {
    cout << count << ") " << ci.currency[0] << " to " << ci.currency[1] << endl;
    ++count;
    cout << count << ") " << ci.currency[1] << " to " << ci.currency[0] << endl;
    ++count;
  }

  int choice;
  cin >> choice;
  return choice;
}

int main()
{
  array<ConversionInfo, 3> cia {{{"bnd", "usd", 0.714950}, {"bnd","eur", 0.661654}, {"bnd", "myr", 3.16401}}};

  int choice;

  for(;;)
  {
    choice = prompt_user( cia );

    if ( choice < 1 || choice > 6) break;

    int index = (choice-1) >> 1; // index of the 'cia' array item
    int from_index = (choice-1) & 1; // index of the 'cia.currency' array item
    int to_index = from_index == 0 ? 1 : 0; // index of the 'cia.currency' array item
    double from_amount, to_amount;

    cout << "please enter the amount in " << cia[index].currency[from_index] << endl;
    cin >> from_amount;
    to_amount = from_index == 0 ? from_amount * cia[index].ratio : from_amount / cia[index].ratio;
    cout << "the amount in " << cia[index].currency[to_index] << " is " << to_amount << endl;
  }

  cout << "goodbye" << endl;
}


这篇关于我可以运行此代码,但我无法选择哪种货币等。请帮我解决这个问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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