我在这个棒球计算器中的 switch 语句有错误。错误说:“控制转移绕过初始化:”
switch 语句用于您要在此程序中执行的计算类型,例如:
在基础百分比击球率等...
// Baseball Stat.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
char menu_choice;
// Batting average formula.
float b_average(float hits, float bats) {return hits/bats;};
// On base percentage formula
float OBP(float hits, float walks, float Hit_by_Pitch, float bats, float sac_flys) {return (hits+walks+Hit_by_Pitch) / (bats+walks+Hit_by_Pitch+sac_flys);};
// Strikeout Ratio Formula
float Strikeout_ratio(float strikeouts, float bats) {return strikeouts/bats;};
// Fielding Average Formula
float fielding_average(float putouts, float assists, float errors) {return (putouts+assists) / (putouts + assists+errors);};
// Range Factor Formula
float range_factor(float putouts , float assists, float defins) {return (putouts+assists) * 9 / defins;};
// Pythagorean Expectation Formula
float pyt_expectation(double runs, double runs_allowed) {return (pow(runs, 1.83) / (pow(runs, 1.83) + pow(runs_allowed, 1.83))) * 100;};
int _tmain(int argc, _TCHAR* argv[])
{
cout << "BASEBALL STATISTICS CALCULATOR\n";
cout << "\nWhat do you want to calculate?\n""\nA.BATTING AVERAGE\n""\nB.ON BASE PERCENTAGE (OBP)\n""\nC.STRIKEOUT RATIO\n""\nD.FIELDING AVERAGE\n""\nE.RANGE FACTOR\n""\nF.BASEBALL GAME SIMULATOR\n";
cin >> menu_choice;
这是错误:
switch(menu_choice)
{
case 'A':
// Variables for First Player's Batting Average.
int atBatsBA;
int numHitsBA;
// Variables for Second Player's Batting Average.
int second_atBatsBA;
int second_numHitsBA;
cout << "\nBATTING AVG\n\n";
cout << "Player #1 At bats:\n";
cin >> atBatsBA;
cout << "Player #1 Hits:\n";
cin >> numHitsBA;
cout << "Player #2 At Bats:\n";
cin >> second_atBatsBA;
cout << "Player #2 Hits:\n";
cin >> second_numHitsBA;
cout << "\nPlayer #1's Batting Average is:\n" << b_average(numHitsBA, atBatsBA) << endl;
cout << "\nPlayer #2's Batting Average is:\n" << b_average(second_numHitsBA, second_atBatsBA) << endl;
break;
case 'B':
// Variables for First Player's OBP
int atBatsOBP;
int hitsOBP;
int walksOBP;
int Hit_By_PitchOBP;
int sa_flysOBP;
// Variables for Second Player's OBP
int second_atBatsOBP;
int second_hitsOBP;
int second_walksOBP;
int second_Hit_By_PitchOBP;
int second_sa_flysOBP;
cout << "\nOBP PERCENTAGE\n";
cout << "\nPlayer #1 At bats:\n";
cin >> atBatsOBP;
cout << "Player #1 Hits:\n";
cin >> hitsOBP;
cout << "Player #1 Walks:\n";
cin >> walksOBP;
cout << "Player #1 Hits by Pitch:\n";
cin >> Hit_By_PitchOBP;
cout << "Player #1 Sac Flys:\n";
cin >> sa_flysOBP;
cout << "Player #2 At bats:\n";
cin >> second_atBatsOBP;
cout << "Player #2 Hits:\n";
cin >> second_hitsOBP;
cout << "Player #2 Walks:\n";
cin >> second_walksOBP;
cout << "Player #2 Hits by Pitch:\n";
cin >> second_Hit_By_PitchOBP;
cout << "Player #2 Sac Flys:\n";
cin >> second_sa_flysOBP;
cout << "\nPlayer #1's OBP:\n" << OBP(hitsOBP,walksOBP,Hit_By_PitchOBP,atBatsOBP,sa_flysOBP) << endl;
cout << "\nPlayer #2's OBP:\n" << OBP(second_hitsOBP,second_walksOBP,second_Hit_By_PitchOBP,second_atBatsOBP,second_sa_flysOBP) << endl;
break;
case 'C':
// Variables for First Player's Strikeout Ratio
int atBatsSOR;
int strikeoutsSOR;
// Variables for Second Player's Strikeout Ratio
int second_atBatsSOR;
int second_strikeoutsSOR;
cout << "\nSTRIKEOUT RATION\n";
cout << "\nPlayer #1 At bats:\n";
cin >> atBatsSOR;
cout << "\nPlayer #1 Strikeouts:\n";
cin >> strikeoutsSOR;
cout << "\nPlayer #2 At bats:\n";
cin >> second_atBatsSOR;
cout << "\nPlayer #2 Strikeouts:\n";
cin >> second_strikeoutsSOR;
cout << "\nPlayer #1's Strikeout Ratio:\n" << Strikeout_ratio(strikeoutsSOR,atBatsSOR) << endl;
cout << "\nPlayer #2's Strikeout Ratio:\n" << Strikeout_ratio(second_strikeoutsSOR,second_atBatsSOR) << endl;
break;
case'D':
// Variables for First Player's Fielding Average.
int putoutsFA;
int assistsFA;
int errorsFA;
// Variables for Second Player's Fielding Average.
int second_putoutsFA;
int second_assistsFA;
int second_errorsFA;
cout << "\nFIELDING AVERAGE\n";
cout << "\nPlayer #1's putouts:\n";
cin >> putoutsFA;
cout << "Player #1's assists:\n";
cin >> assistsFA;
cout << "Player #1's errors:\n";
cin >> errorsFA;
cout << "Player #2's putouts:\n";
cin >> second_putoutsFA;
cout << "Player #2's assists:\n";
cin >> second_assistsFA;
cout << "Player #2's errors:\n";
cin >> second_errorsFA;
cout << "\nPlayer #1's Fielding Average:\n" << fielding_average(putoutsFA, assistsFA, errorsFA) << endl;
cout << "\nPlayer #2's Fielding Average:\n" << fielding_average(second_putoutsFA, second_assistsFA, second_errorsFA) << endl;
break;
case 'E':
// Variables for First Player's Range Factor.
int putoutsRF;
int assistsRF;
int defensive_inningsRF;
// Variables for Second Player's Range Factor.
int second_putoutsRF;
int second_assistsRF;
int second_defensive_inningsRF;
cout << "\nRANGE FACTOR\n";
cout << "\nPlayer #1's putouts:\n";
cin >> putoutsRF;
cout << "Player #1's assists:\n";
cin >> assistsRF;
cout << "Player #1's Defensive Innings Played:\n";
cin >> defensive_inningsRF;
cout << "Player #2's putouts:\n";
cin >> second_putoutsRF;
cout << "Player #2's assists:\n";
cin >> second_assistsRF;
cout << "Player #2's Defensive Innings Played:\n";
cin >> second_defensive_inningsRF;
cout << "\nPlayer #1's Range Factor:\n" << range_factor(putoutsRF, assistsRF, defensive_inningsRF) << endl;
cout << "\nPlayer #2's Range Factor:\n" << range_factor(second_putoutsRF, second_assistsRF, second_defensive_inningsRF) << endl;
break;
case'F':
// Variables for first team's batting average.
string ftname;
int atbatsTBA;
int hitsTBA;
// Variables for second team's batting average.
string stname;
int second_atbatsTBA;
int second_hitsTBA;
cout << "\nBASEBALL GAME SIMULATOR\n";
cout << "\nPlease enter the first team's name:\n";
cin >> ftname;
cout << "\nPlease enter the second team's name:\n";
cin >> stname;
cout << "\nPlease enter the first teams' total at bats:\n";
cin >> atbatsTBA;
cout << "\nPlease enter the first teams's total hits:\n";
cin >> hitsTBA;
cout << "\nPlease enter the second team's total at bats:\n";
cin >> second_atbatsTBA;
cout << "\nPlease enter the second team's total hits:\n";
cin >> second_hitsTBA;
cout << "\n" << ftname << " 's Batting Average is:\n" << b_average(atbatsTBA, hitsTBA) << "\n";
cout << "\n" << stname << " 's Batting Average is:\n" << b_average(second_atbatsTBA, second_hitsTBA) << "\n";
int TBA;
int TBA2;
TBA = b_average(atbatsTBA, hitsTBA);
TBA2 = b_average(second_atbatsTBA, second_hitsTBA);
if(TBA>TBA2){
cout << ftname <<" Wins!!\n";
}
else if(TBA<TBA2){
cout << stname << " Wins!!\n";
}
else{
cout << "It's a draw!!\n";
}
break;
default :
cout << "Please enter a valid option\n";
}
return 0;
}