I have an error with my switch statement in this baseball calculator. The error says :" transfer of control bypasses initialization of:"
The switch statement is used for the types of calculations you want to do in this program like:
On base percentage
Batting Average
etc...
// 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;
here's the error:
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;
}
You can't declare variables inside of switch cases unless you add a new scope. Just change each of your cases to look like this instead:
case 'X': {
// original contents
} break;
That will allow you to declare new variables inside the scope delimited by the {}s and avoid this error.
Related
I am trying to store multiple int into a vector by having the user input the grade they have received. However I do believe there is a more simplified version of trying to do this. Would it be better to have separate functions? Is there anyone who can guide me in the right direction?
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
cout << "Welcome to the Grade Calculator! \n"
<< "What is the student's full name? ";
string student_name;
getline(cin,student_name);
cout << "\nPlease input the points earned for each assignment.";
vector<int> student_grades(8);
cout << "\nLab 2: ";
cin >> student_grades[0];
cout << "Lab 3: ";
cin >> student_grades[1];
cout << "Lab 4: ";
cin >> student_grades[2];
cout << "Lab 5: ";
cin >> student_grades[3];
cout << "Lab 6: ";
cin >> student_grades[4];
cout << "Lab 7: ";
cin >> student_grades[5];
cout << "Lab 8: ";
cin >> student_grades[6];
cout << "Lab 9: ";
cin >> student_grades[7];
cout << "Lab 10: ";
cin >> student_grades[8];
//Finding sum of the grades
int sum_of_grades = accumulate(student_grades.begin(),student_grades.end(),0);
//Console Output
cout << '\n'<< student_name << " has earned " << sum_of_grades << " points in the course, so far." << endl;
cout << "\nThere are 2000 points possible in this course." << endl;
cout << "\nInput the anticpated score for the final project (200 points possible): ";
int anticipated_score;
cin >> anticipated_score;
int total_score = sum_of_grades+anticipated_score;
cout << "Total Projected Assignment Points: " << total_score << endl;
cout << "\nFinal Exam Score Needed for Final Course Grade (1000 Points Possible)"
<< "\nTo Earn an A, need at least: " << 1800 - total_score << " points, for a total of: 1800"
<< "\nTo Earn a B, need at least: " << 1600 - total_score << " points, for a total of: 1600"
<< "\nTo Earn a C, need at least: " << 1400 - total_score << " points, for a total of: 1400"
<< "\n To Earn a D, need at least: " << 1200 - total_score << " points, for a total of: 1200" << endl;
return 0;
}
You can use a for-loop:
for(int i = 0; i < 8; ++i){
cout << "\nLab " << i+2 << ": ";
cin >> student_grades[i];
}
This code is essentially equivalent to what you wrote in your program; it loops through all possible indices, prompting the user to input numbers into the vector.
I have the following code in which I'm trying to validate the radius. If the radius is zero or negative, I'm supposed to give the user endless opportunities to write the radius again until it's valid. When I tested the code, I wrote a negative value and it worked just fine, it gave me endless opportunities until I gave a valid value, but if I wrote a correct value since the beginning, it made me write the value again as if it was incorrect.
#include <iostream>
#include <stdlib.h>
#include "listaEnlazada.h"
using namespace std;
using namespace listaenlazada;
void menu()
{
cout << "\n\t\tLISTA ENLAZADA SIMPLE\n\n";
cout << "1. Insert at begginig " << endl;
cout << "2. Insert at the end " << endl;
cout << "3. Insert in a specific position " << endl;
cout << "4. Write list " << endl;
cout << "5. Search element " << endl;
cout << "6. Exit " << endl;
}
int main() {
CircPtr cabezaLista = NULL, position;
int op;
string name;
double radius;
double x;
double y;
int pos;
do {
menu();
cin >> op;
switch(op)
{
case 1:
cout << "Write the name of the circle: ";
cin >> name;
cout << "\n Write radius: ";
cin >> radius;
do{
cout << "Invalid input. ";
cin >> radius;
}while(radius <= 0);
cout << "\n Write center: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
insertarCabeza(cabezaLista, name, radius, x, y);
break;
case 2:
cout << "Write the name of the circle: ";
cin >> name;
cout << "\n Write radius: ";
cin >> radius;
do{
cout << "Invalid input. ";
cin >> radius;
}while(radius <= 0);
cout << "\n Write center: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
insertarFinal(cabezaLista, name, radius, x, y);
break;
case 3:
cout << "Write the name of the circle: ";
cin >> name;
cout << "\n Write radius: ";
cin >> radius;
do{
cout << "Invalid input. ";
cin >> radius;
}while (radius <= 0);
cout << "\n Write center: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
cout << "Position : ";
cin >> pos;
posicion = buscarPosicion(cabezaLista, pos-1);
if (posicion != NULL)
insertarPosicion(posicion, name, radius, x, y);
break;
case 4:
cout << "\n\n Showing list : ";
escribirLista(cabezaLista);
break;
case 5:
cout << "\n Center of circle to search: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
buscarElemento(cabezaLista, x, y);
break;
case 6:
cout << "\n End of the program. ";
break;
}
cout << endl;
}while(op != 6);
return 0;
}
Just use while loop instead of do while loop. In your code because of cin you are taking one input value and for any value which user enters, next do while loop execute ones without checking condition so your prompting with invalid input on the correct radius input.
Instead of this
cout << "\n Write radius: ";
cin >> radius;
do{
cout << "Invalid input. ";
cin >> radius;
}while(radius <= 0);
Do like this
cout << "\n Write radius: ";
cin >> radius;
while(radius <= 0){
cout << "Invalid input. ";
cin >> radius;
}
Learn the basics of the loops.
I am new to c++ and my textbook is not very helpful. I have a few errors in my code. Where I am being told the identifier for customerAccount is undefined, and I have an incompatible declaration with my int search before and after my main. I will post some code below as I have been trying to figure this out for a while and I am at a loss.
#include<iostream>
#include<string>
using namespace std;
struct {
string Name;
string Address;
string City_State_Zip;
double phoneNumber;
double actBalance;
string Payment;
};
//This is where the errors start, saying customer account is undefined
void Display(customerAccount ca);
//declaration is incompatible with int search
int Search(customerAccount, string);
int main() {
customerAccount customers[10];
string SName;
int choice, i = 0, size = 0;
do {
cout << "****Menu****" << endl;
cout << "1. Enter Customer Information" << endl;
cout << "2. Change Customer Information" << endl;
cout << "3. Search For Customer" << endl;
cout << "4. Display Customer Data" << endl;
cout << "5. Exit" << endl;
cout << "Please enter a choice 1,2,3,4 or 5";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter customer name: ";
cin >> customers[i].Name;
cout << "Enter customer address: ";
cin >> customers[i].Address;
cout << "Enter city state and zip: ";
cin >> customers[i].City_State_Zip;
cout << "Enter phone number: ";
cin >> customers[i].phoneNumber;
cout << "Enter account balance: ";
cin >> customers[i].actBalance;
if (customers[i].actBalance < 0) {
cout << "Account balance cannot be negative. Please re-enter: "
<< endl;
cin >> customers[i].actBalance;
}
cout << "Enter last payment: ";
cin >> customers[i].Payment;
i++;
break;
case 2: int ele;
cout << "Enter customer information to modify: " << endl;
cout << "Enter customer name: ";
cin >> customers[ele - 1].Name;
cout << "Enter customer address: ";
cin >> customers[ele - 1].Address;
cout << "Enter city state and zip";
cin >> customers[ele - 1].City_State_Zip;
cout << "Enter phone number: ";
cin >> customers[ele - 1].phoneNumber;
cout << "Enter account balance: ";
cin >> customers[ele - 1].actBalance;
if (customers[ele - 1].actBalance < 0) {
cout << "Account balance cannot be negative. Please re-enter: "
<< endl;
cin >> customers[i].actBalance;
}
cout << "Enter date of payment: ";
cin >> customers[ele - 1].Payment;
break;
case 3: cout << "Enter customer name to search: ";
cin >> SName;
for (size = 0; size < i; size++) {
int check;
check = Search (customers[size], SName);
if (check == 1)
Display(customers[size]);
}
break;
case 4:
for (size = 0; size < i; size++)
Display(customers[size]);
break;
case 5: exit(0);
break;
}
} while (choice != 5);
system("pause");
return 0;
}
void Display(customerAccount ca) {
cout << " Customer name:";
cout << ca.Name << endl;
cout << " Address:";
cout << ca.Address << endl;
cout << " city state and zip:";
cout << ca.City_State_Zip << endl;
cout << " Phone number:";
cout << ca.phoneNumber << endl;
cout << " Account balance:";
cout << ca.actBalance << endl;
cout << " Date of payment:";
cout << ca.Payment << endl;
}
//declaration is incompatible with int search
int Search(customerAccount ca, string str) {
if (str.compare(ca.Name) == 0)
return 1;
else
return 0;
}
case 2: int ele;
On this line you have an uninitialized variable which is causing your bug.
I am just trying to create a simple "menu". Basically user can input their selection and when they enter 'E', it should exit the menu. I can't catch why its giving me an infinity loop-- i know its most likely my while loop(?). its all just hard-coded as im just trying to get the gist of it.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
char choice;
int numOfCups;
cout << "Hot Beverage Menu: \n";
cout << "A: Coffee $1.00 \n";
cout << "B: Tea $0.75 \n";
cout << "C: Hot Chocolate: $1.25 \n";
cout << "D: Cappuccino: $2.50 \n";
cout << "E: Exit Menu \n";
cout << "Please make a drink selection:";
cin >> choice;
do {
switch(choice) {
case 'A': cout << "You chose Coffee \n";
cout << "How many cups would you like?";
cin >> numOfCups;
cout << "Your total will be: " << '$' << fixed << setprecision(2) << (1.00 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'B': cout << "You chose Tea \n";
cout << "How many cups would you like? \n";
cin >> numOfCups;
cout << "Your total will be: \n" << '$' << fixed << setprecision(2) << (0.75 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'C': cout << "You chose Hot Chocolate \n";
cout << "How many cups would you like? \n";
cin >> numOfCups;
cout << "Your total will be: \n" << '$' << fixed << setprecision(2) << (1.25 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'D': cout << "You chose Cappuccino \n";
cout << "How many cups would you like? \n";
cin >> numOfCups;
cout << "Your total will be: \n" << '$' << fixed << setprecision(2) << (2.50 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'E': cout << "Exit Menu";
break;
default: cout << "Invalid input. Please make another selection.";
break;
}
} while (choice == 'E');
return 0;
}
Loop continues as long as the condition is true, and finishes when the condition is false. Instead of while (choice == 'E') you should have while (choice != 'E').
Also, you should add cin >> choice; to the default condition, or you will have an infinite loop in that case.
Try do ... while (choice != 'E');.
For a project I have to create a simple, menu-driven conversion software package that the user enters a number and converts among other things, meters to feet, feet to meters, etc. I have to use a function for each conversion. I have started with the meters to feet function. But when I compile it I get the error message "a function-definition is not allowed here before '{' token." How do I fix this? I am also a beginner.
#include <iostream>
using namespace std;
double meters_infeet(double meters){
double totalmetersinfeet = (meters * 3.280839895);
return totalmetersinfeet;
}
double feet_inmeters(double feet){
double totalfeetinmeters = (feet / 3.280839895);
return totalfeetinmeters;
}
double area_infeet(double length_inmeters, double width_inmeters){
double totalareainfeet = (length_inmeters * 3.280839895)
return totalareainfeet;
}
double area_inmeters(double length_infeet, double width_infeet){
double totalareainmeters = (length_infeet / 3.280839895)
return totalareainmeters;
}
int main()
{
int choice;
double meters;
double feet;
double length_inmeters;
double width_inmeters;
double length_infeet;
double width_infeet;
do{
cout << "English-Metric Junior" << endl;
cout << "1)Convert from meters to feet" << endl;
cout << "2) Convert from feet to meters" << endl;
cout << "3) Compute the area of a rectangle in square feet given width
cout << "4) Compute the area of a rectangle in meters given the length
cout << "5) Quit the Program" << endl;
cout << "Please enter a number (1-5)" << endl;
cin >> choice;
}while(choice > 5);
switch (choice)
{
case 1:
cout << "Enter Meters" << endl;
cin >> meters;
cout << meters_infeet(meters)<< "Feet " << endl;
break;
case 2:
cout << "Enter Feet" << endl;
cin >> feet;
cout << feet_inmeters(feet)<< "Meters " << endl;
break;
case 3:
cout << "Enter length in meters" << endl;
cin >> length_inmeters;
cout << "Enter width in meters" << endl;
cin >> width_inmeters;
cout << area_infeet( length_inmeters, width_inmeters) << endl;
break;
case 4:
cout << "Enter length in feet" << endl;
cin >> length_infeet;
cout << "Enter width in feet" << endl;
cin >> width_infeet;
cout << area_inmeters(length_infeet, width_infeet)<< endl;
break;
In fact you defined function main within function meters_infeet
double meters_infeet(double meters)
{
double totalmetersinfeet = meters * 3.280839895;
return totalmetersinfeet;
int main()
You missed the closing brace of function.meters_infeet
It seems that this closing brace
cin >> choice;
}
is the closing brace that has to be after the return statement of meters_infeet and before main.
It gets lost in the program.:)
And in this code snippet
switch (choice)
{
case 1:
cout << "Enter Meters" << endl;
cin >> meters;
double meters_infeet(double meters);
break;
}
statement
double meters_infeet(double meters);
is a function declaration.
I think you mean something like
double feets = meters_infeet( meters );
EDIT: It is a bad idea to change the code in the question when the original question before updating was already answered. Because in this case readers will not see a relation between the question and the answers.
First of all these functions do not make sense
double area_infeet(double length_inmeters, double width_inmeters){
double totalareainfeet = (length_inmeters * 3.280839895)
return totalareainfeet;
}
double area_inmeters(double length_infeet, double width_infeet){
double totalareainmeters = (length_infeet / 3.280839895)
return totalareainmeters;
}
Their second parameters are not used.
The switch statement should be inside the do-while statement
do {
cout << "English-Metric Junior" << endl;
cout << "1)Convert from meters to feet" << endl;
cout << "2) Convert from feet to meters" << endl;
cout << "3) Compute the area of a rectangle in square feet given width
cout << "4) Compute the area of a rectangle in meters given the length
cout << "5) Quit the Program" << endl;
cout << "Please enter a number (1-5)" << endl;
cin >> choice;
switch (choice)
{
//...
}
} while( choice != 5);