使用开关盒将3吨20厘米转换为厘米 [英] Conversion of 3mt 20cm to cm using switch case

查看:65
本文介绍了使用开关盒将3吨20厘米转换为厘米的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

我是一名物理专业的学生,​​我们在最后的一年中学习了c ++.我无法编写将3米20厘米转换为厘米的程序.
请帮忙.

谢谢
阿普瓦

[edit]这是您的最后一年,您还没有学会键盘上的所有键都在哪里吗?尤其是元音?TextSpeak转换为英语,大写,标点符号-OriginalGriff [/edit]

Hello.

I''m a physics student and we are having c++ in our final year. I''m not able to write the program for converting 3 metres 20 cm to cm.
Please help.

Thanks
Apurva

[edit]It''s your final year, and you haven''t learnt where all the keys on the keyboard are yet? Specifically the vowels?TextSpeak converted to English, capitalization, punctuation - OriginalGriff[/edit]

推荐答案

您的尝试已经很接近了,但是我发现了一些错误:

在案例标签1下,您要求输入以米为单位并将其存储在可变厘米中,这可能不是您想要的.
您声明变量meters,但使用metres(请注意拼写).

while循环中的条件不正确,整数不能同时大于0和小于1.您可能想要做的是检查是否大于0的menuOption以继续询问新的输入,直到用户输入0退出为止.

Your attempt is quite close already, but I spot a few mistakes:

Under case label 1, you request input in meters and store it in the variable centimeters, this is probably not what you want.
You declare a variable meters but use metres (notice the spelling).

The conditions in your while loop aren''t right, an integer can never be larger than 0 and smaller than 1 at the same time. What you probably want to do, is check for a menuOption larger than 0 to keep asking for new input until the user enters 0 to quit.

#include <iostream>

using namespace std;

int main (void)
{
   int menuOption;
   double centimeters, metres;

   // Display a welcome message.
   cout << "Welcome to the Metres To Centimetres Conversion\n\n";

   // Display a menu option to to determine what the user wants to convert.
   cout << "Enter the number (1) to convert inches to centimeters.\n";
   cout << "Enter the number (0) to exit.\n";
   cout << "Menu Option: ";
   cin  >> menuOption;
   cout << "\n\n";

   // Create formaula to display floating-point type with exactly two digits after decimal point.
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   // Determine the user input to convert and convert and display the results.
   while(menuOption > 0)
   {
		switch (menuOption)
		{
			case 1:
				cout << "Please enter number of metres you wish to convert to centimeters: ";
				cin >> metres;
				centimeters = metres*100;
				cout << "The conversion from metres to centimeters is: " << centimeters << " cm.\n";
				break;
			default:
				cout << "Unknown menu selection!\n";
		}
		cout << "Please enter another menu option?\n";
		cout << "Enter the number (1) to convert metres to centimeters.\n";
		cout << "Enter the number (0) to exit.\n";
		cout << "Menu Option: ";
		cin >> menuOption;
		cout << "\n\n";
   } 

   return (0); 
}


好吧,现在,我可以观察到:
Well, for now, I can observe that:
cout << "Please enter number of metres you wish to convert to centimeters: "; 
cin >> centimeters; // Looks wrong. This should be meters and not centimeters
centimeters = metres*100;


试试吧!

另外,如果您遇到任何错误或逻辑错误,请共享.


Try!

Also, do share if there are any errors or just logical mistake you are facing.


这篇关于使用开关盒将3吨20厘米转换为厘米的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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