So I have some code here that takes an array for employee id and then also fills arrays for hours worked, pay rate, etc and then displays them all. There is no trouble with this part and it all runs smoothly. However I am trying to write this data to a file, but instead of writing the data for all the employees to the file it only writes the data for one employee. I cant seem to figure out why!
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ofstream outputFile;
const int numOfEmployees = 7;
int long empId[numOfEmployees] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489 };
int hours[numOfEmployees];
double payRate[numOfEmployees];
double wages[numOfEmployees];
outputFile.open("PayrollDataBackup.txt");
cout << "Enter the hours worked by 7 employees and their hourly pay rates."<<endl;
cout << " " << endl;
for (int count = 0; count < numOfEmployees; count++)
{
cout << "Hours worked by employee #" << empId[count] << ":";
cin >> hours[count];
while (hours < 0)
{
cout << "Please enter a positive number: ";
cin >> hours[count];
}
cout << "Hourly pay rate for employee #" << empId[count] << ":";
cin >> payRate[count];
while (payRate[count] < 15.00)
{
cout << "Please enter a pay rate higher than $6.00: ";
cin >> payRate[count];
}
}
cout << " " << endl;
cout << "Here are the hours worked, pay rate and gross pay for each employee:" << endl;
cout << " " << endl;
for (int count = 0; count < numOfEmployees; count++)
{
wages[count] = hours[count] * payRate[count];
cout << " " << endl;
cout << fixed << showpoint << setprecision(2);
cout <<"ID:"<< empId[count] <<" Hours: "<< hours[count] << " Pay rate: $" << payRate[count] <<" Wage: $" << wages[count] << endl;
}
for (int count = 0; count < numOfEmployees; count++)
{
outputFile << empId[count] << " " << hours[count] << " " << payRate[count] << " " << endl;
outputFile.close();
}
system("pause");
return 0;
}
You are closing the file after writing the first record.
Change:
for (int count = 0; count < numOfEmployees; count++)
{
outputFile << empId[count] << " " << hours[count] << " " << payRate[count] << " " << endl;
outputFile.close();
}
To:
for (int count = 0; count < numOfEmployees; count++)
{
outputFile << empId[count] << " " << hours[count] << " " << payRate[count] << " " << endl;
}
outputFile.close();
Related
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;
}
I am trying to create an organized looking graph with the amount of money for each food under each month. I was able to line up the first row, but after that, something started going off. This is what I ended up with:
This is the code I tried playing around with the setw() but I can't seem to change it for the number after it puts out the first array element.
void sales_report() {
for (int i = 0; i < 7; i++) {
cout << setw(17) << months[i] << " ";
}
cout << endl << foods[0] << " ";
for (int i = 0; i < 6; i++) {
sumapple += apples[i];
cout << setprecision(2) << fixed << setw(8) << money << apples[i] << " ";
}
cout << setprecision(2) << fixed << setw(8) << money << sumapple << endl;
cout << foods[1] << " ";
for (int i = 0; i < 6; i++) {
sumoranges += oranges[i];
cout << setprecision(2) << fixed << setw(7) << money << oranges[i] << " ";
}
cout << setprecision(2) << fixed << setw(7) << money << sumoranges << endl;
cout << foods[2] << " ";
for (int i = 0; i < 6; i++) {
sumpears += pears[i];
cout << setprecision(2) << fixed << setw(10) << money << pears[i] << " ";
}
cout << setprecision(2) << fixed << setw(10) << money << sumpears << endl;
cout << foods[3] << " ";
for (int i = 0; i < 6; i++) {
sumtomatoes += tomatoes[i];
cout << setprecision(2) << fixed << setw(7) << money << tomatoes[i] << " ";
}
cout << setprecision(2) << fixed << setw(7) << money << sumtomatoes << endl;
cout << foods[4] << " ";
for (int i = 0; i < 6; i++) {
sumcherries += cherries[i];
cout << setprecision(2) << fixed << setw(6) << money << cherries[i] << " ";
}
cout << setprecision(2) << fixed << setw(6) << money << sumcherries << endl << totalmonth;
for (int i = 0; i < 6; i++) {
total = apples[i] + oranges[i] + pears[i] + tomatoes[i] + cherries[i];
cout << setprecision(2) << fixed << setw(9) << money << total << " ";
}
}
You are using different std::setw() values in each of the for loops. Hence the misalignment. Use the same values.
So in my class I had to make a Numberwang simulation game. Everything works fine except for the fact that after 2 rounds the names don't correlate correctly. It supposed to say "Round 3, Player1 to play first." which it does however player2 comes up as the one to play first.
# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;
bool numberwang(int n)
{
if(n < 100 ){
return 1;
} else {
return 0;
}
}
int main()
{
string Firstplayer, Otherplayer;
int rounds;
int counter = 1;
int number;
int win = 18;
int lose= 1;
cout << "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about!" << endl;
cout << "What is player 1's name? ";
cin >> Firstplayer;
cout << "What is player 2's name? ";
cin >> Otherplayer;
cout << "How many rounds? ";
cin >> rounds;
cout << "Well, if you're ready, lets play Numberwang!" << endl;
while(counter <= rounds){
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
while(true){
cout << Firstplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
}
}
return 0;
}
After your if-statement (line 61) you say 'Firstplayer' and then you output the 'Otherplayer'. The names do not match.
Blockquote
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
I've been playing around with some code in my down-time from my degree and I've nested a do{}while() loop inside another one but the problem I'm having is that the code keeps going until the last van is full, even after the number of parcels has been fulfilled...
The code's below. If someone could take a look at it and tell me what I've done wrong that'd be awesome. Bare in mind I've only really been coding in C++ for about a month so I've still got hella lot to learn..
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while ((fCurrentPayload + fParcelWeight)) < fVanCapacity)
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCout;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}
Change this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity);
to this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity
&& iParcelLoaded < iParcelCount);
That way you will load as many items the user inputs. You code contains many syntax errors.
I corrected them for you, but please be more careful next time you post.
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestVanWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity && iParcelLoaded < iParcelCount);
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCount;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestVanWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}
I'v wrote a program for class which uses a for loop to have the user enter a values and it gives you a table with a loop counter, show number entered, and product. I'm trying to get the sum of All 10 numbers in each column to display at the end of each. I'm rather confused how to sum each column and display it underneath. Any help would be GREAT! I'm using Visual Studio Express 2012
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int input;
cout << "Enter Value: ";
cin >> input;
cout << "Loop Counter" << setw(20) << "Number Entered" << setw(14) << "Product" << endl;
for(int counter = 1; counter <= 10; counter++)
{
int product = input * counter;
if (product < 10 && counter != 10)
cout << setw(6) << counter << setw(17) << input << setw(17) << product << endl;
else if (product > 10 && counter != 10)
cout << setw(6) << counter << setw(17) << input << setw(18) << product << endl;
else
cout << setw(7) << counter << setw(16) << input << setw(18) << product << endl;
}
cout<<setfill('_')<<setw(45)<<"_"<<endl;
}
You'll need to sum up the column values in separate variables. Change your code like this:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int input = 0;
cout << "Enter Value: ";
cin >> input;
cout << "Loop Counter" << setw(20) << "Number Entered" << setw(14) << "Product" << endl;
int counterSum = 0;
int inputSum = 0;
int productSum = 0;
for(int counter = 1; counter <= 10; counter++) {
int product = input * counter;
if (product < 10 && counter != 10)
cout << setw(6) << counter << setw(17) << input << setw(17) << product << endl;
else if (product > 10 && counter != 10)
cout << setw(6) << counter << setw(17) << input << setw(18) << product << endl;
else
cout << setw(7) << counter << setw(16) << input << setw(18) << product << endl;
counterSum += counter;
inputSum += input;
productSum += product;
}
cout<<setfill('_')<<setw(45)<<"_"<<endl;
cout << setfill(' ') << setw(7) << counterSum << setw(16) << inputSum << setw(18) << productSum << endl;
}