Break statement is not in loop or switch statement? - c++

I am having trouble with my code, I have read other forums this site about this problem but they don't really relate to my situation. I am new to C++, I barely have about 3 weeks into the course. I do not know why my break statement isn't working, to me it is in the loop. What am I missing?
float a[60][7];
int row;
cout << "How many students would you like to grade?" << endl;
cin >> studentNum;
cout << "Enter grades for the first student" << endl;
row = 0;
n = studentNum - 2;
while (row < studentNum)
{
a[row][0]=k;
a[row][0] = row;
cout << "Enter grade for test 1: ";
cin >> a[row][1];
cout << "Enter grade for test 2: ";
cin >> a[row][2];
cout << "Enter grade for test 3: ";
cin >> a[row][3];
cout << "Enter grade for test 4: ";
cin >> a[row][4];
a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4]) / 4;
a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2;
row++;
}
if (row == n)
{
cout << "Enter Grades for the last Student" << endl;
cout << "Enter grade for test 1: ";
cin >> a[row][1];
cout << "Enter grade for test 2: ";
cin >> a[row][2];
cout << "Enter grade for test 3: ";
cin >> a[row][3];
cout << "Enter grade for test 4: ";
cin >> a[row][4];
break;
}
else
{
cout << "Enter grades for the next student" << endl;
row = row + 1;
}
cout << "Stdnt" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << endl;
printarray(a, studentNum);
cin.get();
return 0;

Something like this?
float a[60][7];
int row = 0;
std::cout << "How many students would you like to grade?" << std::endl;
std::cin >> studentNum;
n = studentNum - 2;
std::cout << "Enter grades for the first student" << std::endl;
while (row <= n)
{
a[row][0] = k;
a[row][0] = row;
std::cout << "Enter grade for test 1: ";
std::cin >> a[row][1];
std::cout << "Enter grade for test 2: ";
std::cin >> a[row][2];
std::cout << "Enter grade for test 3: ";
std::cin >> a[row][3];
std::cout << "Enter grade for test 4: ";
std::cin >> a[row][4];
a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4]) / 4;
a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2;
if (row==n)
{
std::cout << "Enter Grades for the last Student" << std::endl;
std::cout << "Enter grade for test 1: ";
std::cin >> a[row][1];
std::cout << "Enter grade for test 2: ";
std::cin >> a[row][2];
std::cout << "Enter grade for test 3: ";
std::cin >> a[row][3];
std::cout << "Enter grade for test 4: ";
std::cin >> a[row][4];
}
else
{
std::cout << "Enter grades for the next student" << std::endl;
}
row++;
}
std::cout << "Student" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << std::endl;
printarray(a, studentNum);
std::cin.get();
return 0;
P.S. edit with fixed while

Related

Salary with vector array to get sum of employee gross pay

I am to create an application that takes at least 5 employees i.d, names, pay rate, and hours. And then I am to add the pay rate and the hours together to show the gross pay for each employee at the end of the initial inquiries. I am stuck on how to add it in the vector please help!
** Here is the assignment that our instructor gave us **
http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment4
I've added a vector and added all the essential information for the employee
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
using namespace std;
struct Employee
{
int id;
string firstName;
string lastName;
float payRate;
int hours;
};
int main()
{
/*========= other way of adding employee information ==========*/
/*const int NUM_EMPLOYEE = 5;
Employee employee[NUM_EMPLOYEE];
for (int i = 0; i < 5; i++)
{
cout << "ID of employee " << (i + 1) << ": ";
cin >> employee[i].id;
cout << "First Name of employee " << (i + 1) << ": ";
cin >> employee[i].firstName;
cout << "Last Name of employee " << (i + 1) << ": ";
cin >> employee[i].lastName;
cout << "Pay rate for employee " << (i + 1) << ": ";
cin >> employee[i].payRate;
cout << "Hours worked " << (i + 1) << ": ";
cin >> employee[i].hours;
}*/
/*========= End of other way of adding employee information ==========*/
vector<Employee> employees;
char Another = 'y';
while (Another == 'y' || Another == 'Y')
{
Employee e;
cout << "Enter employee ID: \n";
cin >> e.id;
cout << "Enter employee first name: \n";
cin >> e.firstName;
cout << "Enter employee last name: \n";
cin >> e.lastName;
cout << "Enter employee pay rate: \n";
cin >> e.payRate;
cout << "Enter employee hours worked: \n";
cin >> e.hours;
employees.push_back(e);
cout << "Another? (y/n): \n";
cin >> Another;
}
float sum = 0;
vector<Employee>::iterator it = employees.begin();
for (; it != employees.end(); it++)
{
cout << "ID of employee: \n" << it->id << ": \n"
<< "Employees name: \n" << it->firstName << " " << it->lastName << ": \n"
<< "Employee pay rate: \n" << it->payRate << ": \n"
<< "Employee hours worked: \n" << it->hours << "\n";
}
float avg = sum / employees.size();
Employee e;
/*cout << " ID of employees: \n" << e.id;
cout << " Name of employees: \n" << e.firstName << " " <<
e.lastName;*/
cout << "Gross pay of employees: \n" << avg;
_getch();
return 0;
}
Show Id, names, and gross pay of all employees to user
vector<Employee> employees;
char Another = 'y';
while (Another == 'y' || Another == 'Y')
{
Employee e;
cout << "Enter employee ID: \n";
cin >> e.id;
cout << "Enter employee first name: \n";
cin >> e.firstName;
cout << "Enter employee last name: \n";
cin >> e.lastName;
cout << "Enter employee pay rate: \n";
cin >> e.payRate;
cout << "Enter employee hours worked: \n";
cin >> e.hours;
employees.push_back(e);
cout << "Another? (y/n): \n";
cin >> Another;
}
float sum = 0;
vector<Employee>::iterator it = employees.begin();
cout << "ID" << "\t" << "First Name" << "\t" << "Last Name" << "\t" << "Pay rate" << "\t" << "Hours" << "\t" << "Gross Pay" << "\n" ;
for (; it != employees.end(); it++)
{
float grossPay = it->payRate * it->hours;
cout << it->id << "\t" << it->firstName << "\t\t" << it->lastName << "\t\t" << it->payRate << "\t\t"
<< it->hours << "$" << "\t" << grossPay << "\n";
sum += grossPay;
}
cout << "The gross pay for all employee is: " << "$" << sum;
_getch();
return 0;
}
//This is what i got so far. But if I want to make the application ask the user to input how many employee they want to enter into the database how do i do that? would it be like this?
int EMPLOYEE_NUM[][] // ??

Number that is inputted is not the same as displayed by the code, using array in a struct, why?

I'm making a code where users can type in numbers that will be stored in an array which is inside a struct. However, sometimes, the value that I input is not the same as the one that is stored/display. This problem is not very consistent. For example, when I input 10, it could either be shown as: 6384644, 6382852, or actually 10. I am very confused for this, I've tried changing the array data type using int, long, and double, but to no avail.
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int n,x,totalAge = 0,maxAge = 0,avgAge = 0,maxGoals = 0,bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> n;
cout << "Input the number of games: ";
cin >> x;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
long goals[];
};
players goals[x];
players playerList[n];
for (int i = 0; i < n; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin.ignore();
getline (cin, playerList[i].name);
cout << "Input player's current team: ";
getline (cin, playerList[i].currTeam);
cout << "Input player's shirt number: ";
cin >> playerList[i].shirtn;
cout << "Input player's age: ";
cin >> playerList[i].age;
cout << "Input player's height (m): ";
cin >> playerList[i].height;
cout << "Input player's weight (kg): ";
cin >> playerList[i].weight;
cout << endl;
for (int a = 0; a < x; a++) {
playerList[i].goals[a] = 0;
playerList[i].totalGoals = 0;
}
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> playerList[i].goals[a];
playerList[i].totalGoals += playerList[i].goals[a];
}
if (playerList[i].totalGoals > maxGoals) {
maxGoals = playerList[i].totalGoals;
bestPlayer = i;
}
if (playerList[i].age > maxAge) {
maxAge = playerList[i].age;
oldest = i;
}
totalAge += playerList[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << playerList[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << playerList[i].currTeam << endl;
cout << "Shirt Number: " << playerList[i].shirtn << endl;
cout << "Age: " << playerList[i].age << endl;
cout << "Height: " << playerList[i].height << " m" << endl;
cout << "Weight: " << playerList[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << playerList[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / n;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << playerList[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << playerList[bestPlayer].name << ", shirt number: " << playerList[bestPlayer].shirtn << ". With total goals of: " << playerList[bestPlayer].totalGoals << endl;
}
Your code's corrected version would be this:
#include<string>
#include<iostream>
#include<vector> //for vector
using namespace std;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
vector<long> goals; //used vector instead of array
};
int main () {
int N, X;
int totalAge = 0, maxAge = 0, avgAge = 0, maxGoals = 0, bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> N;
cout << "Input the number of games: ";
cin >> X;
players player[X];
for (int i = 0; i < N; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin>>player[i].name;
cout << "Input player's current team: ";
cin>>player[i].currTeam;
cout << "Input player's shirt number: ";
cin >> player[i].shirtn;
cout << "Input player's age: ";
cin >> player[i].age;
cout << "Input player's height (m): ";
cin >> player[i].height;
cout << "Input player's weight (kg): ";
cin >> player[i].weight;
cout << endl;
player[i].totalGoals = 0;
for (int a = 0; a < X; a++) {
long G;
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> G;
player[i].goals.push_back(G);
player[i].totalGoals += G;
}
if (player[i].totalGoals > maxGoals) {
maxGoals = player[i].totalGoals;
bestPlayer = i;
}
if (player[i].age > maxAge) {
maxAge = player[i].age;
oldest = i;
}
totalAge += player[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < N; i++) {
cout << player[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << player[i].currTeam << endl;
cout << "Shirt Number: " << player[i].shirtn << endl;
cout << "Age: " << player[i].age << endl;
cout << "Height: " << player[i].height << " m" << endl;
cout << "Weight: " << player[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < X; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << player[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / N;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << player[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << player[bestPlayer].name << ", shirt number: " << player[bestPlayer].shirtn << ". With total goals of: " << player[bestPlayer].totalGoals << endl;
return 0;
}

C++ switch statement for arithmetic operations

When I run my program, it doesn't allow me to pick an operation. It just goes straight to "Invalid option" and asks again. I want to be able to choose '+', '-', '*', '/', '^', and '!' as my options. What did I do wrong?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char operation;
int num1, num2, result, remain;
//Display the menu to the user and get their first choice
cout << "What operation would you like to perform:" << endl
<< " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial"
<< "\n q quit" << endl << endl << "Operation? ";
cin >> operation;
//Switch - the user does not want to quit
switch (operation)
{
case 1: result=num1+num2;
cout << "Enter the first number to add: " << endl;
cin >> num1;
cout << "Enter the second number to add: " << endl;
cin >> num2;
cout << endl << num1 << " + " << num2 << " = " << result;
break;
case 2: result=num1-num2;
cout << "Enter the first number to subtract: " << endl;
cin >> num1;
cout << "Enter the second number to subtract: " << endl;
cin >> num2;
cout << endl << num1 << " - " << num2 << " = " << result;
break;
case 3: result=num1*num2;
cout << "Enter the first number to multiply: " << endl;
cin >> num1;
cout << "Enter the second number to multiply: " << endl;
cin >> num2;
cout << endl << num1 << " * " << num2 << " = " << result;
break;
case 4: result=num1/num2;
cout << "Enter the dividend: " << endl;
cin >> num1;
cout << "Enter the divisor: " << endl;
cin >> num2;
cout << endl << num1 << " / " << num2 << " = " << result;
cout << endl << num1 << " % " << num2 << " = " << result;
break;
case 5: result=num1^num2;
cout << "Enter the base number " << endl;
cin >> num1;
cout << "Enter the power: " << endl;
cin >> num2;
cout << endl << num1 << " ^ " << num2 << " = " << result;
break;
case 6: result=num1!=num2;
cout << "Enter a number: " << endl;
cin >> num1;
cout << endl << num1 << " ! " << " = " << result;
break;
default:
cout << "That is an invalid operation!" << endl;
break;
} // switch statement closed
cout << endl << "What operation would you like to perform:" << endl << " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial" << "\n q quit" << endl << endl << "Operation? ";
cin >> operation;
return 0;
} //main statement closed
You are using a char against a switch of ints that do not match character codes that you wish to select. If you would like to decide among characters, use character literals instead of integer numbers:
case '+':
// Read inputs first
cout << "Enter the first number to add: " << endl;
cin >> num1;
cout << "Enter the second number to add: " << endl;
cin >> num2;
// Compute the result next
result=num1+num2;
cout << endl << num1 << " + " << num2 << " = " << result;
break;
Note that the assignment
result=num1+num2;
should come after the code that reads the inputs, i.e. num1 and num2.
at the end when I ask if it wants to do another operation, it just ends even if I pick an operation.
This is because an end-of-line character that you entered after num2 is sitting in the buffer. You need to ignore it. Add this line:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
before
cin >> operation;

Program builds but displays nothing

I am working on an assignment. The problem I am having is every time I try to run my program to see what it displays, nothing shows up on the command prompt. However, if I press any key and then enter, the program starts looping uncontrollably. The program doesn't even display the initial cout message, just a blinking "_". Thanks
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu();
void SizePrices();
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
double FlavorChoice=0;
int SizeChoice;
int PizzaCountP=(cin >> PizzaCountP, PizzaCountP);
int PizzaCountM = (cin >> PizzaCountM, PizzaCountM);
int PizzaCountL = (cin >> PizzaCountL, PizzaCountL);
int PizzaCountXL = (cin >> PizzaCountXL, PizzaCountXL);
double orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
cout << "Welcome to Joes pizza place!" << endl;
do{
PizzaMenu();
cout << "\nPlease chose a pizza from the menu(1-6): ";
cin >> FlavorChoice;
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? "; cin >> PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?"; cin >> PizzaCountM;
break;
case 3:
cout << "How many large pizzas?"; cin >> PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?"; cin >> PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)"; cin >> SizeChoice;
break;
}
}
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
printf("Your total is: %a", orderTotal);
}
} while (FlavorChoice != 6);
cout << "Thank you for visiting Joes place pizza! "<<endl;
}
void PizzaMenu()
{
cout << "\nSpecialty Pizza Menu" << endl;
cout << "\n1)Pizza 1" << endl << "\n2)Pizza 2" << endl << "\n3)Pizza 3" <<endl << "\n4)Pizza 4" << endl << "\n5)Pizza 5" << endl << "\n6)Pizza 6" << endl;
}
void SizePrices()
{
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
There were a few logical errors in the program. Right now, it should work fine...
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu()
{
cout << "Specialty Pizza Menu:" << endl;
cout << "1) Pizza 1" << endl << "2) Pizza 2" << endl << "3) Pizza 3" << endl << "4) Pizza 4" << endl << "5) Pizza 5" << endl << "6) Exit" << endl;
}
void SizePrices()
{
cout << "Size Prices:" << endl;
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
int FlavorChoice = 0;
int SizeChoice = 0;
int PizzaCountP = 0;
int PizzaCountM = 0;
int PizzaCountL = 0;
int PizzaCountXL = 0;
double orderTotal = 0.0;
cout << "Welcome to Joes pizza place!" << endl;
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
while(FlavorChoice != 6) {
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? ";
cin >> PizzaCountP;
orderTotal += personal * PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?";
cin >> PizzaCountM;
orderTotal += medium * PizzaCountM;
break;
case 3:
cout << "How many large pizzas?";
cin >> PizzaCountL;
orderTotal += large * PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?";
cin >> PizzaCountXL;
orderTotal += xlarge * PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)";
cin >> SizeChoice;
break;
}
}
// orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
// printf("Your total is: %a", orderTotal);
cout << "Your total is: $" << orderTotal << endl;
}
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
}
cout << "Thank you for visiting Joes place pizza! " << endl;
// system("pause");
return 0;
}

Array not initializing properly within a while loop

My issue is that I have set up an array to store totals that were calculated from values read from a file. These stored totals are then added together to find the over all average.
This issue is stemming from a 'cin' at the beginning of the program where the user inputs a number and that number is supposed to drive the program by setting how many times the program loops and how many modules are inside the array. The array does not seem to work properly no matter how much I try.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string StudentGrades;
int studentID;
double quiz1;
double quiz2;
double quiz3;
double quiz4;
int total = 0;
double choice;
ofstream outFile;
double numStud=1;
cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
outFile.open("StudentGrades.txt");
cout << "How many students would you like to enter?" << endl;
cin >> numStud;
for (int x = 0; x < numStud; x++)
{
cout << "Enter student ID: ";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
//cout << quiz1;
cout << "Enter quiz grade 2: ";
cin >> quiz2;
//cout << quiz2;
cout << "Enter quiz grade 3: ";
cin >> quiz3;
//cout << quiz3;
cout << "Enter quiz grade 4: ";
cin >> quiz4;
//cout << quiz4;
cout << endl;
//outFile.open("StudentGrades.txt");
if (outFile.is_open())
{
cout << "inside if/else outFile" << endl;
//outFile << "File successfully open";
outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
}
else
{
cout << "Error opening file";
}
outFile.close();
/*cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
cin >> choice;
if (choice == 1)
continue;
if (choice == 0)
{
outFile.close();
break;
}*/
}
ifstream inFile;
inFile.open("StudentGrades.txt");
int sTotal;
int total[numStud];
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
//cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
total = (quiz1 + quiz2 + quiz3 + quiz4);
sTotal = total[numStud];
double avg = total / 4;
}
system("pause");
return 0;
}
int total[numStud]; is a variable length array and is not standard in C++. If you need an array and you don't know what the size will be then you should use a std::vector. A vector can be used almost exactly as an array can. For example you could would become:
int total;
std::vector<int> studentTotal;
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
//cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
studentTotal.push_back(quiz1 + quiz2 + quiz3 + quiz4); // insert into the vector at the end
total += studentTotal.back(); // get last inserted element
}