Hello I am new to the programming community. I'm writing a code to calculate gym fees over years and I'm experimenting with loops.
I have come across an issue where my double variables are being "truncated" (I believe its called from research) and i don't know why. Everything seems to work how i want it to so far (I'm not completely done finishing the quit option) except the variables are converting to integers when I use cout. I've done a lot of google research and reading but can't seem to pinpoint it. If someone could help me understand the problem, if I'm just making a silly mistake.
I am working in Microsoft Visual Studio Basic 2017.
Like I said I'm not completely done but i ran into this issue and I really want to understand why it is happening and how to fix it. Thank you so much.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//A) Declare necessary variables for user input and calculations
int userInput, counter;
const double GOLD = 0.01,
SILVER = 0.02,
BRONZE = 0.04,
INITIAL_FEE = 1200.00;
double yearlyFee, tempVal, feeVal;
//B) Display menu
cout << " Welcome to Ronda's Strikeforce Gym!!\n";
cout << "x--------------------------------------------------- x\n";
cout << " Membership Fee Calculator\n\n";
cout << " 1. Gold\n";
cout << " 2. Silver\n";
cout << " 3. Bronze\n";
cout << " 4. Quit\n\n";
cout << "Please enter your membership level(1 - 3, or 4 to quit) : ";
//C) Read in the input from the user
cin >> userInput;
//D) Validate user input(must input 1 through 4), loop if value is invalid
while (userInput < 1 || userInput > 4)
{
cout << "\nError. Please enter a valid menu choice (1-4):";
cin >> userInput;
}
//E) If values 1 - 3 were entered perform calculations else if 4 was entered exit program with thank you message.
if (userInput == 1) feeVal = GOLD;
else if (userInput == 2) feeVal = SILVER;
else if (userInput == 3) feeVal = BRONZE;
else if (userInput == 4) cout << "Thank you for using Rhonda's Fee Calculator!";
//E.1) Perform calculation for specified membership for 10 years and output. (This will require a loop.)
yearlyFee = INITIAL_FEE;
for (counter = 1; counter <= 10; counter++)
{
tempVal = INITIAL_FEE * feeVal;
yearlyFee += tempVal;
cout << fixed << showpoint << setprecision(2);
cout << "\nYear\t" << counter << "\t" << "$" << yearlyFee;
}
cout << endl << endl;
return 0;
}
|
Welcome to Ronda's Strikeforce Gym!!
x--------------------------------------------------- x
Membership Fee Calculator
1. Gold
2. Silver
3. Bronze
4. Quit
Please enter your membership level(1 - 3, or 4 to quit) : 1
Year 1 $1212.00
Year 2 $1224.00
Year 3 $1236.00
Year 4 $1248.00
Year 5 $1260.00
Year 6 $1272.00
Year 7 $1284.00
Year 8 $1296.00
Year 9 $1308.00
Year 10 $1320.00
Press any key to continue . . .