C ++ switch statements

  • 本文关键字:statements switch c++
  • 更新时间 :
  • 英文 :


我正在为学校做一个项目。我从来没学过c++,所以别对我太苛刻了。

我的教授给了我们一些代码,想让我们把数据类型从char改为string。这还不错,但是他想让我们把if else表述改成case表述。从我对case语句的理解来看,它需要像case 1:一样,而1在哪里,我认为我不允许放一个字母代替。

下面是代码和问题,供参考:

指令:

  1. 编辑代码,以便用户可以使用字符串属性输入m的大写或小写。

  2. 编辑if/else语句到case语句的代码

  3. 代码:

// Chapter 4 Exer 18
// BMI
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Variables input by user
char gender;
double bodyWeight;
double wristMeasurementAtFullestPoint;
double waistMeasurementAtNavel;
double hipMeasurementAtFullestPoint;
double forearmMeasurementAtFullestPoint;
double waistMeasurementAtFullestPoint;
// Variables used for calculations
double A1, A2, A3, A4, A5;
double B;
double bodyFat;
double bodyFatPercentage;
cout << "Enter your gender (F or M): ";
cin >> gender;
cout << endl;

// Female BMI
if (gender == 'F' || gender == 'f')
{
cout << "Enter body weight (in pounds): ";
cin >> bodyWeight;
cout << endl;

cout << "Enter wrist measurement at fullest point (in inches): ";
cin >> wristMeasurementAtFullestPoint;
cout << endl;
cout << "Enter waist measurement at navel (in inches): ";
cin >> waistMeasurementAtNavel;
cout << endl;
cout << "Enter hip measurement at fullest point (in inches): ";
cin >> hipMeasurementAtFullestPoint;
cout << endl;
cout << "Enter forearm measurement at fullest point (in inches): ";
cin >> forearmMeasurementAtFullestPoint;
cout << endl;
// BMI Calculations
A1 = bodyWeight * 0.732 + 8.987;
A2 = wristMeasurementAtFullestPoint / 3.140;
A3 = waistMeasurementAtNavel * 0.157;
A4 = hipMeasurementAtFullestPoint * 0.249;
A5 = forearmMeasurementAtFullestPoint * 0.434;
B = A1 + A2 - A3 - A4 + A5;
bodyFat = bodyWeight - B;
bodyFatPercentage = bodyFat * 100 / bodyWeight;
cout << "Body fat: " << bodyFat << endl;
cout << "Body fat percentage: " << bodyFatPercentage << endl;
}
// Male BMI
else if (gender == 'M' || gender == 'm')
{
cout << "Enter body weight (in pounds): ";
cin >> bodyWeight;
cout << endl;

cout << "Enter waist measurement at fullest point (in inches): ";
cin >> waistMeasurementAtFullestPoint;
cout << endl;
// BMI Calculations
A1 = bodyWeight * 1.082 + 94.42;
A2 = waistMeasurementAtFullestPoint * 4.15;
B = A1 - A2;
bodyFat = bodyWeight - B;
bodyFatPercentage = bodyFat * 100 / bodyWeight;
cout << "Body fat: " << bodyFat << endl;
cout << "Body fat percentage: " << bodyFatPercentage << endl;
}
else
{
cout << "Invalid gender code." << endl;
}
return 0;
}

转换大小写字母:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Variables input by user
char gender;
double bodyWeight;
double wristMeasurementAtFullestPoint;
double waistMeasurementAtNavel;
double hipMeasurementAtFullestPoint;
double forearmMeasurementAtFullestPoint;
double waistMeasurementAtFullestPoint;
// Variables used for calculations
double A1, A2, A3, A4, A5;
double B;
double bodyFat;
double bodyFatPercentage;


do
{
cout << "Enter your gender (F or M): ";
cin >> gender;
if (gender >= 'a' && gender <= 'z')
{
gender = gender - 32; // cast to UPPERCASE
}
cout << gender << endl;

// Female BMI
switch (gender)
{
case 'F':
cout << "Enter body weight (in pounds): ";
cin >> bodyWeight;
cout << endl;
cout << "Enter wrist measurement at fullest point (in inches): ";
cin >> wristMeasurementAtFullestPoint;
cout << endl;
cout << "Enter waist measurement at navel (in inches): ";
cin >> waistMeasurementAtNavel;
cout << endl;
cout << "Enter hip measurement at fullest point (in inches): ";
cin >> hipMeasurementAtFullestPoint;
cout << endl;
cout << "Enter forearm measurement at fullest point (in inches): ";
cin >> forearmMeasurementAtFullestPoint;
cout << endl;
// BMI Calculations
A1 = bodyWeight * 0.732 + 8.987;
A2 = wristMeasurementAtFullestPoint / 3.140;
A3 = waistMeasurementAtNavel * 0.157;
A4 = hipMeasurementAtFullestPoint * 0.249;
A5 = forearmMeasurementAtFullestPoint * 0.434;
B = A1 + A2 - A3 - A4 + A5;
bodyFat = bodyWeight - B;
bodyFatPercentage = bodyFat * 100 / bodyWeight;
cout << "Body fat: " << bodyFat << endl;
cout << "Body fat percentage: " << bodyFatPercentage << endl;
break;
// Male BMI
case 'M':
cout << "Enter body weight (in pounds): ";
cin >> bodyWeight;
cout << endl;
cout << "Enter waist measurement at fullest point (in inches): ";
cin >> waistMeasurementAtFullestPoint;
cout << endl;
// BMI Calculations
A1 = bodyWeight * 1.082 + 94.42;
A2 = waistMeasurementAtFullestPoint * 4.15;
B = A1 - A2;
bodyFat = bodyWeight - B;
bodyFatPercentage = bodyFat * 100 / bodyWeight;
cout << "Body fat: " << bodyFat << endl;
cout << "Body fat percentage: " << bodyFatPercentage << endl;
break;
default:
cout << "Invalid gender code." << endl;
}
} while (gender != 'M' && gender != 'F');
return 0;
}