Totaling scores in C++ - c++

Okay so I when I run this code I get that my total is equal to 0 and that
messes up my average and grade.I am not sure what I am doing wrong as the
total += scores function is where it should be, yet it is still not adding up
the scores.
int validateNumber(int, int, int);
in main() function
int num, score, total = 0;
and
validateNumber(num, score, total);
and the definition
int validateNumber(int num, int score, int total) {
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
return total;
}

If you want to implement the validate() function like you did here,
validateNumber(num,score,total);
you can make it void and pass the variable total as reference. e.g,
void validateNumber(int num, int score, int &total) {
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
}
and the rest would be same...
Otherwise I wouldn't have use 3 arguments in this case. e.g,
int validateNumber(int num) {
int total=0,score;
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
return total;
}
and the call:
int num, total;
...
total=validateNumber(num);
Hope it helped...

Are you assuming the function call validateNumber(num,score,total); in line 5 would calculate the total? You should call the function from main function, and assign the return value to a variable, (e.g. total).

Related

How to only print out highest/smallest array values in c++

Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}

for loop with invalid error and looping questions

I am fairly new to C++, I am trying to write a code that allows input number of reviewers then allows number of reviewers to enter movie rating and display asterisks based on the number input. I am having difficulty incorporating an if statement that display "Movie ratings must be from 1 to 5." when the user input any number that's outside of 1 to 5. Another thing when it does work, it still continues the for loop of cout << "\nReviwer " << r << " rating: " ; instead of stopping and restarting. Any assistance is appreciated.
complied code
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
else {
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
Output example should be like this
You should have
#include <iostream>
#include <string>
using namespace std;
int main(){
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
while (rating < 1 || rating > 5){
cout << "Movie ratings must be from 1 to 5." << endl;
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
}
if (rating >= 1 && rating <=5){
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
}
};
It's not optimised but do the trick
if your mean is the reviewer must input the number between 1 and 5,
you can use a do while loop like this:
do
{
//enter rating;
}while (rating < 1||rating >5);
//print out rating;
I suppose you aim at something like this:
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
while(true) {
cout << "\nReviewer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5) {
cout << "Movie ratings must be from 1 to 5." << endl;
} else {
break;
}
}
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end for
Here is some code I quickly put together. I hope the comments are useful and if you have any questions just ask!
The main part is the recursive function get_ratings which will loop forever until it returns 1
// All this function does is returns the correct amount of stars
// E.G. make_stars(4) returns " * * * *"
string make_stars(int star_Count) {
string stars;
for (int i = 0 ; i < star_Count ; i++) {
stars += " *";
}
return stars;
}
// We get the ratings and returns 1 or 0 depending of it succeeded or failed
int get_ratings(int reviewer_count) {
// We initialise the ratings integer
int rating;
// We loop through all reviewers
for (int i = 0 ; i < reviewer_count ; i++) {
// We do i + 1 so it is more readable (usually in English we start at 1 not 0 like computers)
cout << "What is reviewer " << (i + 1) << "'s rating?" << endl;
//We get the user input
cin >> rating;
// We check to see if rating is withing the range. We could also do is NOT in the range and flip line 27 and 29
if (1 <= rating && rating <= 5) {
// If it is within range we will print the correct amount of stars
cout << make_stars(rating) << endl;
} else {
// We return 0 so we can determine the function "failed"
return 0;
}
}
// We return 1 so we can determine the function "succeeded"
return 1;
}
// This is a recursive function (it can run itself)
int get_ratings_rec(int reviewers) {
cout << "All ratings must be given between 1 and 5 (inclusive)" << endl;
// get_ratings_status is equal to 1/0 depending on if get_ratings() succeeded or failed
int get_ratings_status = get_ratings(reviewers);
if (get_ratings_status == 1) {
// If it was a success we print "Success!"
cout << "Success!" << endl;
} else {
// If it was a failure we tell the user and run get_ratings_loop() again until it succeeds
cout << "Failed, please try again\n" << endl;
get_ratings_loop(reviewers);
}
}
// Our main entry point to the program
int main() {
// We initialise the reviewers integer
int reviewers;
cout << "How many reviewers?\n>>> " << endl;
cin >> reviewers;
// We run get_ratings_loop() with the integer given
get_ratings_loop(reviewers);
}
You'd want to keep asking the number of stars if the user inputs a value outside the range, something like:
for (int r = 1; r <= reviewers; r++)
{
cout << "\nReviwer " << r << " rating: ";
do
{
cin >> rating;
if (rating < 1 || rating > 5) // print error message
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5); // repeat the loop if out of range
for (int j = 1; j <= rating; j++)
{
cout << "* ";
} //end for
cout << endl;
}
Note that you should be doing input validation also, e.g., if the input is an alphabetic character, your code will trigger an infinite loop, here an example of a possible solution:
#include <limits>
//...
do
{
if (!(cin >> rating))
{
std::cout << "Bad input, try again";
cin.clear(); //clear error flags
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear buffer
rating = 0;
}
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5);

If two numbers are found to be equal how would one take the first number inputted

I have gotten all of my program to work exactly how it should except for the very end.
At the end when the 'winnerAvg' and 'winnerName' are outputted to the screen if multiple names have the same average all of them will be printed to the screen.
What I was wondering was if there was a way to get only the first name inputted with that average to be outputted to the screen. Instead of all that apply.
#include <iostream>
#include <string> //for name entry of contestants
#include <iomanip> //rounding and point perceision
#include <fstream> //write names and averages to a file for storage
using namespace std;
double calcAvgScore(int, int, int, int, int); //calculates the average after the highest and lowest scores are found
double findHighest(int, int, int, int, int); //finds the highest of the 5 scores
double findLowest(int, int, int, int, int); //finds the lowest of the 5 scores
int main()
{
string cName; //name of the contestant
int jScore1, jScore2, jScore3, jScore4, jScore5; //scores of each judge
double AvgScore; //average of the scores
ofstream outFile;
outFile.open("NamesAndAverage.txt"); //creation and opening of text file where the names and averages will be stored
cout << "All judges' scores are from 1 to 10" << endl;
cout << " " << endl;
for (int count = 1;; count++) //loop to keep track of number of Contestants and exits when Done is entered
{
cout << " " << endl;
if (count = 1)
cout << "Please enter the name of Contestant #" << count << ". If there are no Contestants enter ""Done""" << endl;
else
cout << "Please enter the name of Contestant #" << count << ". If there are no more Contestants please enter ""Done""" << endl;
cin >> cName;
if (cName == "Done")
break;
cout << " " << endl;
cout << "Please enter the score from Judge 1 ";
cin >> jScore1;
while (jScore1 < 1 || jScore1 > 10)
{
cout << "scores must be from 1 to 10" << endl;
cout << "Please re-enter the score from Judge1 ";
cin >> jScore1;
}
cout << "Please enter the score from Judge 2 ";
cin >> jScore2;
while (jScore2 < 1 || jScore2 > 10)
{
cout << "scores must be from 1 to 10" << endl;
cout << "Please re-enter the score from Judge2 ";
cin >> jScore2;
}
cout << "Please enter the score from Judge 3 ";
cin >> jScore3;
while (jScore3 < 1 || jScore3 > 10)
{
cout << "scores must be from 1 to 10" << endl;
cout << "Please re-enter the score from Judge3 ";
cin >> jScore3;
}
cout << "Please enter the score from Judge 4 ";
cin >> jScore4;
while (jScore4 < 1 || jScore4 > 10)
{
cout << "scores must be from 1 to 10" << endl;
cout << "Please re-enter the score from Judge4 ";
cin >> jScore4;
}
cout << "Please enter the score from Judge 5 ";
cin >> jScore5;
while (jScore5 < 1 || jScore5 > 10)
{
cout << "scores must be from 1 to 10" << endl;
cout << "Please re-enter the score from Judge5 ";
cin >> jScore5;
}
double AvgScore = calcAvgScore(jScore1, jScore2, jScore3, jScore4, jScore5); //call to function
outFile << cName << " " << AvgScore; //writing variables to the file
}
outFile.close();
cout << " " << endl;
cout << " "<<endl;
double winnerScore = 0; //variable for the highest average
string winnerName = "Null"; //variable for name with highest average
ifstream inFile;
inFile.open("NamesAndAverage.txt");
while (inFile >> cName >> AvgScore) //reading variables from file
{
if (AvgScore > winnerScore)
winnerScore = AvgScore;
if (AvgScore == winnerScore)
winnerName=cName;
}
cout << "The winner is " << winnerName << " with a score of " << setprecision(2) << fixed << winnerScore << endl;
system("pause");
return 0;
}
double calcAvgScore(int jScore1, int jScore2, int jScore3, int jScore4, int jScore5)
{
double highest = findHighest(jScore1, jScore2, jScore3, jScore4, jScore5);
double lowest = findLowest(jScore1, jScore2, jScore3, jScore4, jScore5);
double total = (jScore1 + jScore2 + jScore3 + jScore4 + jScore5 - highest - lowest);
double AvgScore = (total / 3);
cout << "inside Average function Avg= " << AvgScore << endl;
return AvgScore;
}
double findHighest(int jScore1, int jScore2, int jScore3, int jScore4, int jScore5)
{
double highest = 0;
if (jScore1 > highest)
highest = jScore1;
if (jScore2 > highest)
highest = jScore2;
if (jScore3 > highest)
highest = jScore3;
if (jScore4 > highest)
highest = jScore4;
if (jScore5 > highest)
highest = jScore5;
cout << "inside highest function highest = " << highest << endl;
return highest;
}
double findLowest(int jScore1, int jScore2, int jScore3, int jScore4, int jScore5)
{
double lowest = 10;
if (jScore1 < lowest)
lowest = jScore1;
if (jScore2 < lowest)
lowest = jScore2;
if (jScore3 < lowest)
lowest = jScore3;
if (jScore4 < lowest)
lowest = jScore4;
if (jScore5 < lowest)
lowest = jScore5;
cout << "inside lowest function lowest= " << lowest << endl;
return lowest;
}
Change this:
if (AvgScore > winnerScore)
winnerScore = AvgScore;
if (AvgScore == winnerScore)
winnerName=cName;
to:
if (AvgScore > winnerScore)
{
winnerScore = AvgScore;
winnerName = cName;
}
should do the trick - in other words, don't update name unless you are also updating the score. If the score is equal, the FIRST found will be the "winner".

Wrong variable in for...loop

Not a great title I know but I'm not sure how to word it. Anyway, in brief I am trying to calculate the number of scores, the total of scores and the average and grade of the scores. I am using for... loops to complete this task. So...
my function prototypes:
int validateNumber(int);
void summary(int,int,float,char);
char getGrade(float);
float getAvg(int,int);
probably only the validateNumber(int) is relevant here but just in case.
The main()
int num, total, scores;
cout << over4 << "How many scores do you want to average? " << endl;
cout << over4 << "Enter a value from 1 to 4: ";
cin >> num;
And the calls(?):
total = validateNumber(num);
summary(num,total,average,grade);
And then the definitions:
int validateNumber(int num)
{
int total = 0, score;
while (num < 1 || num > 4)
{
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++)
{
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << over3 << score << " is not between 0 and 100! Renter the score: "
<< i << ": ";
cin >> score;
}
total += score;
}
return total;
}
and:
void summary(int num,int total,float average,char grade)
{
cout << over4 << "Number of scores : " << num << endl;
cout << over4 << "Scores total : " << total << endl;
cout << over4 << "Average : " << average << "%" << endl;
cout << over4 << "Grade : " << grade << endl;
cout << down11;
}
When the user enters a num value between 1 and 4, there is no problem, the program works as it should. However when the user enters a value for num not in that range, the function works as it should BUT the summary will tell me that the number of scores was that first erroneous value and as a result mess up my average/grade.
in your function you are passing by value not by reference, so the change which is done in your function int validateNumber(int); is only in that function's scope, outside num's value is same as it was first. you should send value by reference. in this way :
int validateNumber(int &);
What happens is that you pass num to the validateNumber function by value. The local num in the validateNumber function copies the global num's value and get's processed accordingly. However, the global num variable stays as it were. In order to change the num itself you will have to pass it by reference. Change the parameters on your function definition to: int validateNumber(int &num) { ... }

C++ For Loop Problem?

Ok so i'm working on this project and its a travel expense program. Basically it just has some functions that gets info from the user. I'm having a problem with my for loop. The running total is messing up. The numbers end up like 2 or 4 numbers off of what there supposed to be. Here's the code(I know it's not neat or anything i will clean that up later)
#include <iostream>
#include <fstream>
using namespace std;
int getDays(int);
double getDepartureTime();
double getArrivalTime(double);
double airFees(double);
double carRentalFees(double);
double getMilesDriven(double);
double getParkingTotal(double,double);
double getParkingSpent(double,double);
double getTaxiFees(double,double);
double employeeHotelExpense(double,double);
double getHotelExpense(double,double);
double getMealExpenses(double,double);
void timeEquivalent();
double breakFastFee = 0;
int main()
{
int days=0, amount=0, departure_conference=0, departure_home=0,time = 0;
double airFee=0,taxiFeesAllowed,parkingAllowed = 0,employeeHotelExpense = 0,employeeTaxiFees = 0, milesDriven=0,
parkingFees=0, taxiFees=0, yes=0,arrivalTime = 0;
double carRentalFee = 0, hotel_expenses=0,departureTime = 0, meals=0,employeeMealExpenses = 0, parkingSpent = 0,allowableHotelExpense = 0
,allowedMealTotal = 0,mealsSpent = 0;
char employee[40];
//timeEquivalent();
//cout << "What Time Did You Arrive " <<endl;
//cin >>time;
days = getDays(days);
timeEquivalent();
departureTime = getDepartureTime();
arrivalTime = getArrivalTime(arrivalTime);
airFee = airFees(airFee);
carRentalFee = carRentalFees(carRentalFee);
milesDriven = getMilesDriven(milesDriven);
parkingAllowed = 6 * days;
parkingSpent = getParkingSpent(parkingSpent,days);
taxiFeesAllowed = days * 10;
employeeTaxiFees = getTaxiFees(taxiFees,days);
allowableHotelExpense = 90 * days;
employeeHotelExpense = getHotelExpense(employeeHotelExpense, days);
employeeMealExpenses = getMealExpenses(departureTime,arrivalTime);
cout << employeeMealExpenses <<endl;
return 0;
}
int getDays(int days)
{
cout << " How many Days did you stay on the trip " <<endl;
cin >> days;
while(days < 0)
{
cout <<"Please enter a value greater than 0 :D " <<endl;
cin >> days;
}
return days;
}
double getDepartureTime()
{
double departureTime;
cout << "Please Refer To The Menu Above and enter the time of departure in military\n";
cout << "Time. For example if you departed at 7:30 enter 0730\n\n";
cin >> departureTime;
return departureTime;
}
double getArrivalTime(double arrivalTime)
{
cout << "Please refer to the menu above and enter the time you arrived back home in\n";
cout <<" military format\n";
cin >> arrivalTime;
return arrivalTime;
}
double airFees(double airfee)
{
cout << " How Much Were Your Air Fees " <<endl;
cin >> airfee;
while(airfee < 0)
{
cout <<" Please enter a value greater than 0 :D " <<endl;
cin >> airfee;
}
return airfee;
}
double carRentalFees(double carRentalFee)
{
cout << " How Much were Your Car Rental Fees " <<endl;
cin >> carRentalFee;
while(carRentalFee < 0)
{
cout <<"Please enter a value of 0 or greater :D " <<endl;
cin >> carRentalFee;
}
return carRentalFee;
}
double getMilesDriven(double milesDriven)
{
const double mileRate = 0.27;
cout << " How many miles did you drive, please enter 0 if a private vehicle was not used " <<endl;
cin >> milesDriven;
while(milesDriven < 0)
{
cout << " Please Enter 0 or Greater:)"<<endl;
cin >> milesDriven;
}
return mileRate * milesDriven;
}
double getParkingSpent(double parkingSpent, double days)
{
cout << " How Much Did You Spend on Parking " <<endl;
cin >> parkingSpent;
while(parkingSpent < 0)
{
cout << "Please Enter an Amount of 0 or Greater "<<endl;
cin >> parkingSpent;
}
return parkingSpent*days;
}
double getTaxiFees(double taxiFees,double days)
{
cout << " Please Enter The Amount of Taxi Fees Please " <<endl;
cin >> taxiFees;
while(taxiFees < 0)
{
cout << "Please Enter an Amount of 0 or Greater "<<endl;
cin >> taxiFees;
}
return taxiFees * days;
}
double getHotelExpense(double employeeHotelExpense,double days)
{
cout << " How Much Were Your Hotel Expenses " <<endl;
cin >> employeeHotelExpense;
while(employeeHotelExpense < 0)
{
cout << "Please Enter a Amount of 0 or Greater "<<endl;
cin >> employeeHotelExpense;
}
return employeeHotelExpense * days;
}
double getMealExpenses(double departureTime,double arrivalTime)
{
static double breakFastFee = 0 ;
static double lunchFee = 0 ;
static double dinnerFee = 0 ;
int numberOfDays = 2 ;
double total = 0;
for(int days =1;days <=numberOfDays;days++)
{
if ( days < numberOfDays && departureTime > 000 && departureTime < 700)
{
cout << "Please Enter Your breakfast cost"<<endl;
cin >> breakFastFee;
cout << " Please Enter Your Lunch Cost " <<endl;
cin >>lunchFee;
cout << "Please Enter Your Dinner Cost " <<endl;
cin >> dinnerFee;
}
if (days < numberOfDays && departureTime > 700 && departureTime <=1200)
{
cout << "Please Enter Your Lunch Cost"<<endl;
cin >> lunchFee;
cout << "Please Enter Your Dinner cost "<<endl;
cin >> dinnerFee;
}
if(days < numberOfDays && departureTime > 1201 && departureTime <= 1800)
{
cout << "Enter The Cost of Dinner " <<endl;
cin >> dinnerFee;
}
if(days == numberOfDays && arrivalTime > 800 && arrivalTime<=1300)
{
cout <<"Enter The Cost of Breakfast " <<endl;
cin >> breakFastFee;
}
if(days == numberOfDays && arrivalTime > 1301 && arrivalTime <= 1900)
{
cout << "Enter The Cost of Breakfast "<<endl;
cin >> breakFastFee;
cout << " Enter The Cost of Lunch " <<endl;
cin >> lunchFee;
}
if(days == numberOfDays && arrivalTime > 1901)
{
cout << "Enter The Cost of Breakfast " <<endl;
cin >> breakFastFee;
cout << " Enter The Cost of Lunch " <<endl;
cin >> lunchFee;
cout << "Enter The Cost of Dinner " <<endl;
cin >> dinnerFee;
}
total+=breakFastFee + lunchFee + dinnerFee;
}
return total;
}
void timeEquivalent()
{
cout <<"Regular Time " << "\t\t" <<"Military Time \n";
cout <<"************" << "\t\t" <<"***************"<<endl;
cout <<"Midnight " << "\t\t" <<"0000 \n";
cout <<"1:00a.m. " << "\t\t" <<"0100 \n";
cout <<"2:00a.m. " << "\t\t" <<"0200 \n";
cout <<"3:00a.m. " << "\t\t" <<"0300 \n";
cout <<"4:00a.m. " << "\t\t" <<"0400 \n";
cout <<"5:00a.m. " << "\t\t" <<"0500 \n";
cout <<"6:00a.m. " << "\t\t" <<"0600 \n";
cout <<"7:00a.m. " << "\t\t" <<"0700 \n";
cout <<"8:00a.m. " << "\t\t" <<"0800 \n";
cout <<"9:00a.m. " << "\t\t" <<"0900 \n";
cout <<"10:00a.m." << "\t\t" <<"1000 \n";
cout <<"11:00a.m." << "\t\t" <<"1100 \n";
cout <<"12:00p.m." << "\t\t" <<"1200 \n";
cout <<"1:00p.m. " << "\t\t" <<"1300 \n";
cout <<"2:00p.m. " << "\t\t" <<"1400 \n";
cout <<"3:00p.m. " << "\t\t" <<"1500 \n";
cout <<"4:00p.m. " << "\t\t" <<"1600 \n";
cout <<"5:00p.m. " << "\t\t" <<"1700 \n";
cout <<"6:00p.m. " << "\t\t" <<"1800 \n";
cout <<"7:00p.m. " << "\t\t" <<"1900 \n";
cout <<"8:00p.m. " << "\t\t" <<"2000 \n";
cout <<"9:00p.m. " << "\t\t" <<"2100 \n";
cout <<"10:00p.m." << "\t\t" <<"2200 \n";
cout <<"11:00p.m." << "\t\t" <<"2300 \n";
cout <<"Midnight " << "\t\t" <<"0000 \n";
}
can somebody tell me whats going wrong. If you test it out use 0600 for departure time and 0900 for arrivaltime.
There are multiple problems in this code. One, in a function like getDays that returns the number of days, you don't need to pass the number of days into the method as a parameter.
Also, since you want a value for days that is greater than 0, you should be checking whether days <= 0 in the while loop. Your current condition, days < 0, will be false if days is set to 0.
getDays would be better written as:
int getDays()
{
int days = 0;
cout << " How many Days did you stay on the trip " <<endl;
cin >> days;
while(days <= 0)
{
cout <<"Please enter a value greater than 0 :D " <<endl;
cin >> days;
}
return days;
}
Also, in getMealExpenses, there seems to be no reason to declare breakFastFee, lunchFee, and dinnerFee as static. This may be the cause of the problem you asked about, since they never get re-initialized to 0 after the first call to getMealExpenses.
Finally, neatly-formatted code is more likely to get helpful responses, because it's easier to read :-)
I think its becouse you are not resetting the breakFastFee, lunchFee, dinnerFee variable to 0 before each execution. So when total+=breakFastFee + lunchFee + dinnerFee; hits it will add values from the previues iteration.