我正在编写一个转换器程序,可以将不同的值从英制系统转换为国际系统以及货币。 我想循环程序,这样它就不会只进行一次转换。这是我的代码:
#include "std_lib_facilities.h"
#include "stdio.h"
int a = 0;
int b = 0;
int c = 0;
void money()
{
cout << "This will convert CAD to either USD or EUR\n";
cout << "Please input USD or EUR followed by an enter keystroke for conversion\n";
string a;
while(cin >> a){
if( a == "USD"){
cout << "If you would like to convert USD into CAD, enter 1.\n";
cout << "If you would like to convert CAD into USD, enter 2.\n";
int x;
cin >> x;
if( x == 1){
cout << "Please enter the amount you wish to convert.\n";
double j;
cin >> j;
double k = j*1.29;
cout << j << "USD is " << k << "CAD.\n";
}
if ( x == 2){
cout << "Please enter the amount you wish to convert.\n";
double o;
cin >> o;
double p = o*0.77;
cout << o << "CAD is " << p << "USD.\n";
}
}
if( a == "EUR"){
cout << "If you would like to convert EUR into CAD, enter 1.\n";
cout << "If you would like to convert CAD into EUR, enter 2.\n";
int y;
cin >> y;
if(y == 1){
cout << "Please enter the amount you wish to convert.\n";
double g;
cin >> g;
double h = g*1.46;
cout << g << "EUR is " << h << "CAD.\n";
}
if(y == 2){
cout << "Please enter the amount you wish to convert.\n";
double z;
cin >> z;
double x = z*0.69;
cout << z << "CAD is " << x << "EUR.\n";
}
}
}
}
void weight()
{
double amount;
cout << "This will convert pounds to kilograms.\n";
cout << "Please input the amount you wish to convert.\n";
cin >> amount;
cout << "Would you like to convert " << amount << "kg to lb or the reverse?\n";
cout << "To convert kg to lb, please press 1. To convert lb to kg please press 2.\n";
int q;
while(cin >> q){
if( q == 1){
double kg = amount*2.2;
cout << amount << "kg is " << kg << "lb.\n";
}
if( q == 2){
double lb = amount*0.5;
cout << amount << "lb is " << lb << "kg.\n";
}
}
}
void temperature() // t to f and f to t
{
}
void setup()
{
cout << "Please enter either c,w or t for the corresponding conversions.\n";
cout << "(Where c is for currency, w is for weight, and t is for temperature.\n";
string a;
while ( cin >> a){
if( a == "c"){
money();
}
if( a == "w"){
weight();
}
if( a == "t"){
temperature();
}
}
}
int main() // loop it to make more than one conversion
{
cout << "Welcome to the ultimate converter app.\n";
setup();
cout << "Would you like to perform another conversion? (Y/N)\n";
string y;
while(cin >> y){
if( y == "Y"){
setup();
}
if( y == "N"){
exit (EXIT_FAILURE);
}
}
return 0;
}
程序确实按预期工作,但最后没有循环。在我输入最后一个命令后它就卡住了,就像 return 0 卡住一样。
如果有任何帮助,我将不胜感激。
最佳答案
我刚刚尝试了您的代码,正如您所说,它第一次运行得很好。但问题是它陷入了循环。据我所知,这里有两个主要问题。
当您实际上只需要在 main 中使用一个时,您在每个函数中都使用了 while 循环。在这些场景中尽量减少“while”循环的数量。我看到的第二件事是您使用“cin >> variable”作为参数。尽量远离这种情况,而是使用一个简单的 bool 变量。
在这里,我以一种肮脏的方式修复了你的代码,但它有效。希望这有帮助 :D
int a = 0;
int b = 0;
int c = 0;
void money()
{
cout << "This will convert CAD to either USD or EUR\n";
cout << "Please input USD or EUR followed by an enter keystroke for conversion\n";
string a;
while (cin >> a) {
if (a == "USD") {
cout << "If you would like to convert USD into CAD, enter 1.\n";
cout << "If you would like to convert CAD into USD, enter 2.\n";
int x;
cin >> x;
if (x == 1) {
cout << "Please enter the amount you wish to convert.\n";
double j;
cin >> j;
double k = j * 1.29;
cout << j << "USD is " << k << "CAD.\n";
break;
}
if (x == 2) {
cout << "Please enter the amount you wish to convert.\n";
double o;
cin >> o;
double p = o * 0.77;
cout << o << "CAD is " << p << "USD.\n";
break;
}
}
if (a == "EUR") {
cout << "If you would like to convert EUR into CAD, enter 1.\n";
cout << "If you would like to convert CAD into EUR, enter 2.\n";
int y;
cin >> y;
if (y == 1) {
cout << "Please enter the amount you wish to convert.\n";
double g;
cin >> g;
double h = g * 1.46;
cout << g << "EUR is " << h << "CAD.\n";
break;
}
if (y == 2) {
cout << "Please enter the amount you wish to convert.\n";
double z;
cin >> z;
double x = z * 0.69;
cout << z << "CAD is " << x << "EUR.\n";
break;
}
}
}
}
void weight()
{
double amount;
cout << "This will convert pounds to kilograms.\n";
cout << "Please input the amount you wish to convert.\n";
cin >> amount;
cout << "Would you like to convert " << amount << "kg to lb or the reverse?\n";
cout << "To convert kg to lb, please press 1. To convert lb to kg please press 2.\n";
int q;
while (cin >> q) {
if (q == 1) {
double kg = amount * 2.2;
cout << amount << "kg is " << kg << "lb.\n";
break;
}
if (q == 2) {
double lb = amount * 0.5;
cout << amount << "lb is " << lb << "kg.\n";
break;
}
}
}
void temperature() // t to f and f to t
{
}
void setup()
{
cout << "Please enter either c,w or t for the corresponding conversions.\n";
cout << "(Where c is for currency, w is for weight, and t is for temperature.\n";
string a;
while (cin >> a) {
if (a == "c") {
money();
}
if (a == "w") {
weight();
}
if (a == "t") {
temperature();
}
break;
}
}
int main() // loop it to make more than one conversion
{
cout << "Welcome to the ultimate converter app.\n";
setup();
string y;
while (true) {
cout << "Would you like to perform another conversion? (Y/N)\n";
cin >> y;
if (y == "Y") {
setup();
}
else if (y == "N") {
exit(EXIT_FAILURE);
}
}
return 0;
}
关于c++ - 循环一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47742581/