so i have to make three functions.One asks for how many employees are in a company.The other asks how many days they missed.The third one calculates the average by dividing the amount of employees by the number of days missing.In main,all i have to do is have cout prompts and call the functions.Im not to sure if i'm doing it right,but it crashes when it has to calculate the average.
#include <iostream>
#include <iomanip>
using namespace std;
int employee(int employeeNum);
int missingDays(int daysMissing);
double getAvg(int employeeNum,int daysMissing,double average);
int employee(int employeeNum)
{
cout<<"Enter the number of employee in the company.";
cin>>employeeNum;
return employeeNum;
}
int missingDays(int daysMissing)
{
cout<<"Enter the amount of days employees missed during the past year.";
cin>>daysMissing;
return daysMissing;
}
double getAvg(int employeeNum,int daysMissing,double average)
{
average=employeeNum/daysMissing;
return average;
}
int main()
{
int employeeNum,people,missing,daysMissing;
double avg,average;
people=employee(employeeNum);
cout<<"The number of employees in the company is "<<people<<"."<<endl;
missing=missingDays(daysMissing);
cout<<"The number of days employees missed during the past year is "<<missing<<".";
avg=getAvg(employeeNum,daysMissing,average);
cout<<average;
}
Let me know what i got to do and thanks for the help.
This looks like homework, so you should only expect nudges. So, some nudges: why do your functions employee() and missingDays() take arguments? And why does getAvg() take 3? In which variables do your input values end up? Where are they used next (if at all)?
Your problem is this line
missing=missingDays(daysMissing);
for some reason you have two variables for "daysMissing" (I'm not sure why you pass it in to your missingDays function in the first place)
After that line, missing will contain the value input, not daysMissing (since it's not passed by reference)
Your getAvg function, presumably, is crashing with a division by zero error.
So depending on compiler the value of daysMissing is either undefined or initialized with 0.
In your function getAvg you divide employeeNum/daysMissing;
But you are not allowed to divide by zero, thats's it :)
#include <iostream>
#include <iomanip>
using namespace std;
int employee(int employeeNum);
int missingDays(int daysMissing);
double getAvg(int employeeNum,int daysMissing,double average);
int employee(int employeeNum)
{
cout<<"Enter the number of employee in the company: ";
cin>>employeeNum;
return employeeNum;
}
int missingDays(int daysMissing)
{
cout<<"Enter the amount of days employees missed during the past year: ";
cin>>daysMissing;
return daysMissing;
}
double getAvg(int employeeNum,int daysMissing)
{
if(daysMissing == 0)
{
return 0;
}
return (double)employeeNum/daysMissing;;
}
int main()
{
int employeeNum,people,missing,daysMissing;
double avg,average;
people=employee(employeeNum);
cout<<"The number of employees in the company is: "<<people<<"\n";
missing=missingDays(daysMissing);
cout<<"The number of days employees missed during the past year is: "<<missing<<"\n";
avg=getAvg(people,missing);
cout<<avg;
}
Related
Problem is, on execution, the value of roundCost I'm getting is
something like -1220673834. I post the entire program because I'm not
sure where I'm going wrong.
Note: I was asked to take all variables as double type and later,
roundCost should be of type int. So I used type conversion there.
#include<iostream>
using namespace std;
class Restaurant{
private:
double tip, tax,totalCost,mealCost, tipPercent, taxPercent;
int roundCost;
public:
int tipCalc(double)
{
tip=mealCost*(tipPercent/100);
return tip;
}
int taxCalc(double)
{
tax=mealCost*(taxPercent/100);
return tax;
}
int totalCost1()
{
totalCost=mealCost+tip+tax;
return totalCost;
}
int roundCost1(double)
{
roundCost=(int)totalCost;
return roundCost;
}
}; // class ends
int main()
{
double mealCost, tipPercent, taxPercent, totalCost;
int roundCost;
Restaurant ob1;
cout<<"\n Enter mealCost \n";
cin>>mealCost;
cout<<"\n Enter mealtipPercent \n";
cin>>tipPercent;
cout<<"\n Enter mealtaxPercent \n";
cin>>taxPercent;
ob1.tipCalc(tipPercent);
ob1.taxCalc(taxPercent);
ob1.totalCost1();
ob1.roundCost1(totalCost);
cout<<"\n Round of cost is "<<roundCost<<endl;
return 0;
}
One thing you seem to be missing is that variables in your class have a different scope then variables in your main. You set the mealcost in your main from cin but you never passed this variable to the class. I changed this to be done using a constructor that sets the meal cost on creation. In every class you make you should always add a constructor. Also, you should be naming the variables your passing to functions and then using the same name in the function. For example in the tax percent function i pass double t, t is the percent, we then use t in the calculation. Your round cost variable was also private so you needed to output it via a function.
Also int functions will return a value, if you are using this type of function you should be assigning the return variable to something, but since you are just setting things in your class you can use void functions for most. The only time you use a value in the main is in the roundcost so this one is good to have it return a value. As it is int (which i assumed you wanted) it will get no decimal points and it will simply cut off any decimals in the total cost (ie 75.75 would become 75).
#include<iostream>
using namespace std;
class Restaurant{
private:
double tip, tax,totalCost,mealCost;
int roundCost;
public:
Restaurant (double m)
{
mealCost = m;
}
void tipCalc(double t)
{
tip=mealCost*(t/100.0);
}
void taxCalc(double t)
{
tax=mealCost*(t/100.0);
}
void totalCost1()
{
totalCost=mealCost+tip+tax;
}
int roundCost1()
{
roundCost=(int)totalCost;
return roundCost;
}
}; // class ends
int main()
{
double mealCost, tipPercent, taxPercent, totalCost;
int roundCost;
cout<<"\n Enter mealCost \n";
cin>>mealCost;
Restaurant ob1(mealCost);
cout<<"\n Enter mealtipPercent \n";
cin>>tipPercent;
cout<<"\n Enter mealtaxPercent \n";
cin>>taxPercent;
ob1.tipCalc(tipPercent);
ob1.taxCalc(taxPercent);
ob1.totalCost1();
cout<<"\n Round of cost is "<<ob1.roundCost1()<<endl;
return 0;
}
Try to do a bit more research next time by using a debugger, outputting cout statements regularly and searching for the errors you find but this will give you a working code this time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For HW (intro to C++) I had to modify a program that I wrote previously, I will be including that code here. Seeing the output of it will make things much more clearer:
//Program to compute and display the average and appropriate letter grade of 3 test scores
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
char grade;
double Test_1,Test_2,Test_3,Avg,ClassAvg1,ClassAvg2,ClassAvg3;
int sumTest_1,sumTest_2,sumTest_3;
int n;
sumTest_1=sumTest_2=sumTest_3=0;
const int totalSum=5.0;
for(n=1;n<=5;n++) {
do {
cout<<"What are the three test scores for student #"<<n;
cin>>Test_1>>Test_2>>Test_3;
if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)
cout<<"You entered an invalid score - please try again"<<endl;
}
while(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100);
Avg=((Test_1+Test_2+Test_3)/3.0);
if(Avg<65) {
grade= 'F';
}
else if(Avg<70)
{
grade= 'D';
}
else if(Avg<80)
{
grade= 'C';
}
else if(Avg<90)
{
grade= 'B';
}
else
{
grade='A';
}
cout<<setprecision (0)<<fixed;
cout<<"Your test average is "<<Avg<<" and your grade is " <<grade<<endl;
sumTest_1=sumTest_1+Test_1;
sumTest_2=sumTest_2+Test_2;
sumTest_3=sumTest_3+Test_3;
ClassAvg1=sumTest_1/5.0;
ClassAvg2=sumTest_2/5.0;
ClassAvg3=sumTest_3/5.0;
}
cout<<"The class average for test #1 is: "<<ClassAvg1<<endl;
cout<<"The class average for test #2 is: "<<ClassAvg2<<endl;
cout<<"The class average for test #3 is: "<<ClassAvg3<<endl;
}
For hw, I have to modify the program above by including two functions, ComputeAvg which will be called to compute and return the student's average and LetterGrade, to compute and return the letter grade. This is so that instead of using one main program, I now use these 2 functions. I have written this code so far, but I'm not sure if I'm doing it right and it will be great if someone can explain things out for me since I'm still not very comfortable writing functions:
//Program to compute and display the average and appropriate letter grade of 3 test scores
#include<iostream>
using namespace std;
void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
double Avg;
double ComputeAvg(int Test_1,int Test_2,int Test_3);
{
Avg=ComputeAvg(Test_1,Test_2,Test_3);
return(Test_1+Test_2+Test_3)/3.0;
}
}
int main()
{
int Test_1,Test_2,Test_3,n;
double Avg;
for(n=1;n<=5;n++)
{
do
{
cout<<"What are the three test scores for student #"<<n;
cin>>Test_1>>Test_2>>Test_3;
if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)
cout<<"You entered an invalid score - please try again"<<endl;
}
cout<<"Your test average is:"<<Avg<<endl;
ComputeAvg(Test_1,Test_2,Test_3);
}
}
#include<iostream>
using namespace std;
void LetterGrade(int Test_1, int Test_2, int Test_3)
{
double Avg;
double LetterGrade(int Test_1,int Test_2,int Test_3);
{
Grade=LetterGrade(Test_1,Test_2,Test_3);
void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
double Avg;
double ComputeAvg(int Test_1,int Test_2,int Test_3);
{
Avg=ComputeAvg(Test_1,Test_2,Test_3);
return(Test_1+Test_2+Test_3)/3.0;
}
}
The above code doesn't look correct, especially using a nested function or recursion.
Something tells me you want a simplified version:
double Compute_Average(int test_1, int test_2, int test_3)
{
return ( static_cast<double>(test_1)
+ static_cast<double>(test_2)
+ static_cast<double>(test_3))
/ 3.0;
}
Since the function is returning a value, in your main function you want to store the result in a variable:
double Avg = Compute_Average(Test_1, Test_2, Test_3);
cout << "Test average is: " << Avg << "\n";
I am new to C++ and i'm having trouble with my program using classes and inputfile to display my input in the output. How should I display the country, population and area? I'm getting error messages like:
Line 82 [Error] invalid use of 'Country::Country'
Line 89 [Error] invalid types 'long int[int]' for array subscript
Line 93 [Error] invalid types 'double[int]' for array subscript
Here is what I have so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Country
{
private:
string name;
long int population;
double area;
public:
Country();
Country(string, long, double);
void setName(string);
void setPopulation(long);
void setArea(double);
string getName();
long getPopulation();
double getArea();
};
Country::Country(){
name="?";
population=0;
area=0;
}
Country::Country(string name1, long population1, double area1){
name=name1;
population=population1;
area=area1;
}
void Country::setName(string name1){
name=name1;
}
void Country::setPopulation(long population1){
if(population1>=0.0)
population=population1;
else{
population1=0.0;
cout<< "Invalid number. Setting population to 0."<<endl;
}
}
void Country::setArea(double area1)
{
if(area1>=0.0)
area=area1;
else{
area1=0.0;
cout<< "Invalid number. Setting area to 0."<<endl;
}
}
string Country::getName(){
return name;
}
long Country::getPopulation(){
return population;
}
double Country::getArea(){
return area;
}
int main(){
Country home;
const int H=5;
string homename="";
long homepopulation=0;
double homearea=0;
ifstream infile("mycountrydata.txt");
home.setName(homename);
home.setPopulation(homepopulation);
home.setArea(homearea);
home.Country(homename, homepopulation, homearea);
for(int i=0; i<H; i++){
cout<<"Enter the country's name: ";
infile>>homename[i];
cout<<endl;
cout<<"Enter the country's population: ";
infile>>homepopulation[i];
cout<<endl;
cout<<"Enter the country's area: ";
cout<<endl;
infile>>homearea[i];
}
infile.close();
return 0;
}
A constructor is a special member function which cannot be directly invoked this way:
home.Country(homename, homepopulation, homearea);
long's don't have the [] operator defined and hence you can't do:
infile>>homepopulation[i];
since earlier you declare long homepopulation. The same explanation holds for the error in
infile>>homearea[i];
These are answers addressing the exact errors in your code, but it isn't a substitute for a good teaching resource. See this answer for some useful material.
country is a constructor and it can be invoked by giving the below statement in the beginning of the main() replacing country home;
country home(homename, homepopulation, homearea);
I guess you want to use homepopulation and homearea as arrays but you declared them as normal variables.
I doing a freind function program according to this book I have and I did a little of my own code to the program. I puzzle because I get this error message that the "room_num" is undeclared and intellisense identifier "room_num" is undefine. I need help in understanding why this is happen and how to fix it. Here is the code I have been working on for the passed three weeks.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
class HotelRoom
{
friend int Transfer( HotelRoom&, int);
private:
int room_num;
int transroom_num;
int room_cap;
int occup_stat;
double daily_rt;
public:
HotelRoom(int room, int roomcap, int occup, int transroom, double rate = 89.00);
~HotelRoom();
int Display_Number(); //Displays room number and add the method Display_Guest()
int Get_Capacity();
int Get_Status();
double Get_Rate();
int Change_Status(int);
double Change_Rate(double);
void Display_Guest();
};
HotelRoom::~HotelRoom()
{
cout << endl<<endl;
cout << "Guest in room "<<room_num << " has checked out." <<endl;
}
int HotelRoom::Display_Number()
{
return room_num;
}
int HotelRoom::Get_Capacity()
{
return room_cap;
}
int HotelRoom::Get_Status()
{
return occup_stat;
}
int HotelRoom::Change_Status(int occup)
{
occup_stat = occup;
if (occup > room_cap)
{
return -1;
}
else
return occup_stat;
}
double HotelRoom::Get_Rate()
{
return daily_rt;
}
double HotelRoom::Change_Rate(double rate)
{
daily_rt = rate;
return daily_rt;
}
int Transfer(HotelRoom& room_r1, int transroom)
{
//if guest transfers to different hotel room, room is vacant and transroom is now occupied
room_r1.room_num = room_r1.transroom_num;
return room_num;
}
int main()
{
cout<< setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
int room = 0;
int roomcap = 4;
int transroom;
int occup;
double rate = 89.00;
cout<<"\nEnter the room number: "<<endl;
cin>>room;
cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;
cout<<"\nThe guest has decided to transfer rooms"<<endl;
cout<<"\nEnter the room to transfer the guest to"<<endl;
cin>>transroom;
HotelRoom room1(room,roomcap, occup, transroom, rate ); //initialize the object
if (room1.Change_Status(occup) == -1)
{
cout<<"You have exceeded the room capacity"<<endl;
}
else
{
cout <<"\nThe room number is ";
room1.Display_Number();
cout<<"."<<endl;
cout<<"\nThe name of the primary guest is ";
room1.Display_Guest();
cout <<"."<<endl;
cout<<"\nThe number of guest in the room is "<<room1.Change_Status(occup)<<"." <<endl;
cout<<"\nThe daily rate for room "<<room<< " is "<<room1.Get_Rate()<<"."<<endl<<endl;
cout<<"\nYou have tranferred the guest from room"<<room1.Display_Number()<<"to" <<Transfer(room1,transroom)<<endl;
}
cout<<"\nRoom ";
room1.Display_Number();
cout<<" is vacant."<<endl;
system("PAUSE");
return 0;
}
The function Transfer is not a method of HotelRoom, still you are trying to access room_num in it as if it was. You need to specify which room_num of which HotelRoom instance you mean. Probably you meant return room_r1.room_num instead of return room_num.
Also in your Transfer function you never use the parameter transroom, instead you are using a transroom_num from room_r1. This is probably not what you want.
Finally you haven't implemented the constructor and DisplayRoom of HotelRoom. You should create a stubs, which do nothing or print warnings as long as you haven't implemented the methods properly, so you can at least compile and link the code.
Since you are a beginner I would just stick with member functions and class private variables until you get better at it.
As far as the error message, my guess is that inside the function you are using room_num does not have access to the private parts of the HotelRoom class. Notice I said guess, that's because you should copy and paste the text on the output window here so we can see what exactly is happening.
First, you have to identify that room_num is class member variable.
int Transfer(HotelRoom& room_r1, int transroom)
{
room_r1.room_num = room_r1.transroom_num;
//because room_num is not non class member variable, you have to write like below.
return room_r1.room_num;
//return room_num;
}
Secondly, you did not write definition HotelRoom::HotelRoom(int,int,int,int,double), HotelRoom::Display_Guest(void). So you have to write this constructor and function for avoiding error LNK2019.
I'm having an issue with passing parameters for this simple program. I am getting a "cannot convert 'float*' to 'float' argument '2' to 'void getData(std::string, float, float, float, float)'" error when trying to compile this program. Can anyone figure out what I'm doing wrong? I've been trying forever. Note: Please ignore deprecated stuff like system("PAUSE") and a few other things. This is simple the way my teacher has thought me to code and this is what he wants me using for this program. I am aware of getchar() and I use it for practice and final work. Plus this shouldn't affect the program as I have been using it without issues before on small programs for my C++ class.
Here's the code:
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
void getData(string,float,float,float,float);
void getCalc(int,float,float,float,float,float,float,float,float,float,float);
void getPrint(float,float,float);
int main()
{
int const acres=1000;
string crop;
float cpa[4];
float yield[4];
float per[4];
float increase[4];
float cost[4];
float grossmin[4];
float grossmax[4];
float netmin[4];
float netmax[4];
float netave[4];
getData(crop,cpa,yield,per,increase);
getCalc(acres,cpa,yield,per,increase,cost,grossmin,grossmax,netmin,netmax,netave);
getPrint(netmin,netmax,netave);
system("PAUSE");
return 0;
}
void getData(string fcrop,float fcpa[],float fyield[],float fper[],float fincrease[])
{
for (int i=0;i<4;i++)
{
cout<<"Enter the crop: ";
getline(cin,fcrop);
cout<<"Enter the cost per acre:$ ";
cin>>fcpa[i];
cout<<"Enter the yield: ";
cin>>fyield[i];
cout<<"Enter $/bishell: ";
cin>>fper[i];
cout<<"Enter the percentage increase: ";
cin>>fincrease[i];
cin.ignore(80,'\n');
}
}
void getCalc(int acres,float fcpa[],float fyield[],float fper[],float fincrease[],float fcost[],float fgrossmin[],float fgrossmax[],float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
int acres=1000;
fcost[i]=acres*fcpa[i];
fgrossmin[i]=acres*fyield[i]*fper[i];
fgrossmax[i]=fgrossmin[i]+(fgrossmin[i]*fincrease[i]/100);
fnetmin[i]=fgrossmin[i]-fcost[i];
fnetmax[i]=fgrossmax[i]-fcost[i];
fnetave[i]=(fnetmin[i]+fnetmax[i])/2;
}
}
void getPrint(float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
cout<<"The minumum profit is:$ "<<fnetmin[i]<<endl;
cout<<"The maximum profit is:$ "<<fnetmax[i]<<endl;
cout<<"The average profit is:$ "<<fnetave[i]<<endl;
}
}
In the prototype you have written at the start of the program, this is written.
void getData(string,float,float,float,float) ;
It should be this instead identical to the one in its definition.
void getData(string,float[],float[],float[],float[]);
The function prototype should be same in its declaration and implementation.