calculate x years for savings with deposition and revenue C++ - c++

I'm sitting with a challenging homework in C++ and would be really thankful for some help here!
My program need to calculate how many years it will take for an optional yearly deposition with revenue to reach a specific savings limit.
I just can't see what's wrong and have tried debugging with no help.
It doesn't really help neither that i'm totally new to C++ and MVS 2015.
I don't know if it's the math or the programming itself that is wrong.
Static typing is foreign to me since I usually use python.
Also VS don't give much information and the program just stops after asking for revenue input.
Any suggestions?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int deposit;
int max_savings;
double revenue;
double change_factor;
double year = 0;
double geometric_sum;
cout << "Choose a yearly deposition:\n";
cin >> deposit;
cout << "Set your max saving-goal:\n";
cin >> max_savings;
cout << "set a revenue in percent:\n";
cin >> revenue;
change_factor = 1 + (revenue / 100);
geometric_sum = ((double)deposit * (pow(change_factor, year) - 1)) / (change_factor - 1);
while (geometric_sum < max_savings)
year++;
cout << "Your saving-goal will be in " << year << " years!" << endl;
cout << "Your account balance will then be " << geometric_sum << " dollars!" << endl;
return 0;
}

pow(change_factor, year) - 1
year is set to 0. Any value at the power of 0 is 1. 1 - 1 = 0. Basically you are multiplying with 0.

Related

How do I calculate and display the correct percentage?

I am a C++ noob.
I am trying to work on this text-based game for school, and I am having trouble with displaying the correct percentage. I think this has to do with how I calculate it in the program, or I am screwing something up with the function.
I would be most grateful for some assistance. Cheers.
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
double menu (float crew_count);
double calculatePct(float crew_count, float number_of_deaths)
{
double percent = ((crew_count - number_of_deaths) / crew_count) * 100;
}
double welcome ()
{
int crew_count;
string backstory = "\nYou are in charge of a top-secret military mission, when your\nspace ship makes an emergency landing, on the largest moon of planet Gork.\nThe space ship is damaged. Oxygen levels begin to drop.\nHow many military personnel are on your ship?\nNumber of personnel: ";
cout << backstory;
cin >> crew_count;
if (crew_count >= 1)
menu(crew_count);
else if (crew_count < 1)
cout << "\nThere must be 1 or more members of the crew! Please enter a valid number!\n";
}
double menu (float crew_count)
{
double percent;
double main_option;
cout << "\nChoose one:\n1. Attempt repairs on the ship.\n2. Request an emergency rescue from mission command.\n3. Break protocol and reveal the top-secret space ship's location,\nto the Russians on a nearby moon, asking for their assistance.\nYour choice: ";
cin >> main_option;
if (main_option == 1)
{
cout << "\nToxic material on the moon has corroded the launch gear, and the \nlaunch exploded!\n\nThere were no survivors.\n";
}
else if (main_option == 2)
{
cout << "\nThe oxygen was depleted before the rescue team arrived.\nThere were 4 people killed.\n";
if (crew_count <=4)
cout << "0% of crew members were rescued!\n";
else
float percent = calculatePct(crew_count, 4);
cout << percent << "% of the crew was rescued.\n";
}
else if (main_option == 3)
{
cout << "\nThe Russians agree to send a rescue ship, but secretly attempt to hack into the ships systems remotely, which triggers an automatic shut down of all\ncommunications systems and locks all mission critical storage units, including\none of the storage unit that holds emergency oxygen tanks.\n\nOne quarter of all personnel are lost.\n";
}
else if (main_option != 1, 2, 3)
{
cout << "\nYou have been eaten by a Grue!\n";
}
}
int main()
{
cout << "Welcome to Gork 1.0\nCreated by Cortez Phenix\nTo make selections, enter the number of each option!\n\n";
int choice;
cout << "What would you like to do?\n1. Play Game\n2. Exit\nYour Choice: ";
cin >> choice;
if (choice == 1)
welcome();
else if (choice == 2)
cout << "\nGoodbye!\n";
else
cout << "\nPlease choose 1 or 2.\n";
return 0;
}
Excuse me. I'm sure this post is hectic.
IMAGE: At the bottom, you can see the queer number
If we do some slight reformatting on parts of your code, it looks like this:
if (crew_count <=4)
cout << "0% of crew members were rescued!\n";
else
float percent = calculatePct(crew_count, 4);
cout << percent << "% of the crew was rescued.\n";
The value you print is not the value calculated by calculatePct, it's the indeterminate value of the percent variable defined earlier in the function.
In short: You forgot your curly-braces:
if (crew_count <=4)
cout << "0% of crew members were rescued!\n";
else
{ // Note curly brace here
float percent = calculatePct(crew_count, 4);
cout << percent << "% of the crew was rescued.\n";
} // And also here
I recommend you enable more verbose warnings, as the compiler should be able to detect that you use the uninitialized variable, as well as the new variable being initialized but not used.
you mixed intergers and floats and void
int crew_count then you fed double menu(float crew_count).
your menu function doesnt return anything its supossed to be void so does your welcome function
Also you calculatePct does not return the calculated percentage. Do that by adding return percent;
See if it helps

How can I fix my source code in my C++ program called Flix for Fun Profit Report?

I have coded a program in C++ for an assignment in my C++ Intro class but I have multiple errors in the program but I can't seem to figure out how to get all of the bugs out.
The program is supposed to ask the user for the name of the movie, the number of adult and child tickets sold, and calculate the gross box office profit, net box office profit and the amount paid to the distributor.
I can't seem to figure out how to initialize the variables adultTicketPrice and
childTicketPrice and I thought I declared them and am trying to figure out if they need to get initialized if I already declared them?
And how is the childTicket price out of scope?
And why am I getting the other errors and how can I fix them?
// Michael VanZant
// Flix for Fun Profit Report
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Create all double variables
double adultTicketsSold, childTicketsSold, grossBoxProfit, netBoxProfit,
amtPaidDist, adultTicketPrice, childTicketPrice
adultTicketPrice = 12;
childTicketPrice = 7;
cout << fixed << setprecision(2);
// Create the string variable
string movieName;
// Get the name of the movie and store it in the movieName string
cout << "What is the name of the movie?";
getline(cin, movieName);
cout << "\n";
// Cin.ignore to ignore the string variable type
cin.ignore();
// Get the amount of adult and child tickets sold
cout << "How many adult tickets do you want to buy?";
cin >> adultTicketsSold;
cout << "\n";
cout >> "How many child tickets did you want to buy?";
cin >> childTicketsSold;
cout << "\n";
// Calculate the amount of gross box office profit and display it
grossBoxProfit = (childTicketsSold * childTicketPrice) + (adultTicketsSold * adultTicketPrice);
cout << "Gross Box Office Profit: $" << grossBoxProfit;
cout << "\n";
// Calculate the net box profit amount and display it
netBoxProfit = grossBoxProfit * .20;
cout << "Net Box Profit Amount: $" << netBoxProfit;
cout << "\n";
// Calculate the amount paid to distributor and display it
amtPaidDist = grossBoxProfit - netBoxProfit;
cout << "Amount Paid to Distributor is: $" << amtPaidDist;
return 0;
}
When the compiler says "expected initialiser", it has nothing to do with these lines:
adultTicketPrice = 12;
childTicketPrice = 7;
which are actually assignments, not initialisations (though some old C terminology would call the first assignment an initialisation).
No, it's because it thinks you're still on this line, providing declarations and (optionally) initialisers:
double adultTicketsSold, childTicketsSold, grossBoxProfit, netBoxProfit,
amtPaidDist, adultTicketPrice, childTicketPrice
That's because you didn't put a ; at the end of it.
Also:
cout >> "How many child tickets did you want to buy?";
You meant <<.
Fixing those two little typos, the code compiles.

C++ Text Array with User Input, then printing the input

I’m hoping someone could help me out. Below is my current code. Please be gentle, this is legitimately my first program in C++, and it’s been about a year since I last touched C. And yes, this is for homework ~ I’ve used this page enough to know it’s asked quite a bit ;)
What I’m having an issue with, and struggling to find something moderately helpful regarding it, is how do I create an array to store user input text?
As you can see from the flow of the code: I ask how many items does the user want to purchase… this then dictates the looping, asking the user for the item name being purchased, the cost per item, and the total quantity. I am fine with the math part ~ I have the total items purchased and the running subtotal printing out pretty accurately. However, what I would like to do is print, in order, the names of the items that were purchased, too.
Current Code Output:
How many items do you want to enter? 3
What is the item name? Honey
What is the unit price for Honey? 5.99
How many purchased? 3
What is the item name? Milk
What is the unit price for Milk? 2.79
How many purchased? 2
What is the item name? chocolate
What is the unit price for chocolate? 1.97
How many purchased? 5
Bill Date:
Items Purchased: 10
Subtotal: 33.4
In between “Bill Date” and “Items Purchased” I would like to list, line by line, the (3) items purchased: Honey, Milk, and chocolate. It’s the storing of the item name and incrementing it that I am very much stuck on. If someone could point me in the right direction, I would greatly appreciate it. And please, the lengthier you explain the how's and why's, the better for me. Text/char arrays and I are mere acquaintances, whereas numerical int arrays and I are drinking buddies.
Thank you!! :D
Desired Code Output:
Bill Date:
Honey
Milk
chocolate
Items Purchased: 10
Subtotal: 33.4
My Code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <time.h>
using namespace std;
int main()
{
int itemCount = 0, i, itemQty;
int numOfItems = 0;
char itemName[25];
double itemCost;
double itemSub;
double subtotal = 0;
cout << "How many items do you want to enter? ";
cin >> itemCount;
for(i = 0; i < itemCount; i++)
{
cout << "What is the item name? ";
cin >> itemName;
cout << "What is the unit price for " << itemName << "? ";
cin >> itemCost;
cout << "How many purchased? ";
cin >> itemQty;
numOfItems = numOfItems + itemQty;
itemSub = itemQty * itemCost;
subtotal = subtotal + itemSub;
}
cout << "\n\tItems Purchased: " << numOfItems;
cout << "\n\tSubtotal: " << subtotal << "\n";
}
What is the largest number of items to be purhcased?...I suppose it is 100..Instead of making an array of chars you can make an array of string so change the line char itemName[25]; to string itemName[100]; then inside the for loop change the input and output of itemname to cin >> itemName[i];
cout << "What is the unit price for " << itemName[i] << "? ";
then you can output the names of items at last this way:
for(int i=0;i<itemCount;i++)
{
cout<<endl<<itemName[i];
}
cout << "\n\tItems Purchased: " << numOfItems;
cout << "\n\tSubtotal: " << subtotal << "\n";

Wrong answer displayed for simple math problems c++

I am just starting to learn C++ in college and our first assignment is to make a program that will do basic math. i feel like my code is not mistaken, but when i display the variable "sum", i get an answer that is way off. the value for the answer changes even if i input the same number multiple times. for example, i entered 2 for each variable and i got 1864273973 the first time and 1772335157 the second time. what could be causing this? i am using a macbook pro and code blocks, if anyone is wondering. i have also included my code.
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main()
{
//variabe declarations
int number, number2;
int sum, difference, product, dividend;
//calculations
sum = number + number2;
difference = number - number2;
product = number * number2;
dividend = number/number2;
//user inputs
cout << "\n1 of 2: Enter a number: ";
cin >> number;
cout << "\n2 of 2: Enter second number :";
cin >> number2;
cout << "\nNumber 1 entered: " << number << "\nNumber 2 entered: " << number2;
//output
cout << "\n" << number << "+" << number2 << "=" << sum << "\n";
}
C++ and almost every language nowadays use a structured system. It reads from top to bottom, so if you say "a = b+c" and then cin >> a, the calculation from b+c will be lost after the new input.
You are trying to calculate using variables that are declared but not initialized. In c++, this will cause the new variable to just receive "trash", a number you probably don't want. To correct this, I think you want to actually receive number and number2 BEFORE doing the math.

Error saying, "statement cannot resolve address of overloaded function"

I'm a beginner programmer, so this is going to look messy, but I keep getting the problem that is mentioned in the title. No matter where I try to put endl; it keeps giving me the same error. Also when I run the code my total for the second store comes out right but the first store total does not. Any idea on how to fix this? I'm using codeblocks on a windows 7 computer.
#include <iostream> //Allows cout/cin
#include <ctime> //Allows time
#include <iomanip> //Allows setprecision
using namespace std;
int main()
{
//Include header
//Input variables
double widgetStores;
double numberSoldFirst1;
double numberSoldFirst2;
double numberSoldSecond1;
double numberSoldSecond2;
double widgetsLeftS1W2;
double widgetsLeftS2W1;
double widgetsLeftS2W2;
//Start Clock
clock_t begin, end;
double time_spent;
begin = clock();
//Prompt for total number in stores
cout << "Total number of widgets at each store starting with :";
cin >> widgetStores;
double widgetStore1=widgetStores;
double widgetStore2=widgetStores;
double widgetsLeftS1W1;
//Prompt for amount sold during first and second week
cout << "How many widgets were sold at Store 1 the first week? ";
cin >> numberSoldFirst1;
cout << "How many widgets were sold at Store 1 the 2nd week? ";
cin >> numberSoldSecond1;
cout << "How many widgets were sold at Store 2 the first week? ";
cin >> numberSoldFirst2;
cout << "How many widgets were sold at Store 2 the 2nd week? ";
cin >> numberSoldSecond2;
//Calculate Number of widgets
widgetsLeftS1W1-=(widgetStore1-numberSoldFirst1);
widgetsLeftS1W2-=(numberSoldFirst1-numberSoldSecond1);
widgetsLeftS2W1-=(widgetStore2-numberSoldFirst2);
widgetsLeftS2W2-=(numberSoldFirst2-numberSoldSecond2);
//Display Values
cout << "Store 1 has " << widgetsLeftS1W2 << " widgets left after the 2nd week.";
cout << "Store 2 has " <<widgetsLeftS2W2 << " widgets left after the 2nd week.";
//Show time elapsed
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
cout << setprecision(2) << fixed << "****Elapsed time:" <<time_spent/60 << "minutes****";
return 0;
Did you try something like
cout << "Total number of widgets at each store starting with :";
cin >> widgetStores;
cout << endl; //Added this
cin doesn't have a << operator, so you need to send it to cout.
Edit: I found the error you're having. I guess that you are trying to put in lines as literally
endl;
and that doesn't go anywhere...
The only compile error this program has, is that you're using widgetsLeftS1W1, widgetsLeftS1W2, widgetsLeftS2W1 and widgetsLeftS2W2 before initializing them.
You probably need = instead of -=.
When you say
widgetsLeftS1W1 -= (widgetStore1-numberSoldFirst1);
what you actually mean is
widgetsLeftS1W1 = widgetsLeftS1W1 - (widgetStore1-numberSoldFirst1);
The computer doesn't know the value of widgetsLeftS1W1, so it gives you the error.
Conclusion: use
widgetsLeftS1W1 = (widgetStore1 - numberSoldFirst1);
Try initializing the values of
widgetsLeftS1W1
widgetsLeftS1W2
widgetsLeftS2W1
widgetsLeftS2W2
with zero while declaring them at the top.