Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
my while loop wont run on the conditions I set it.
The purpose of the program is to determine the amount of years it will take a deposited amount to mature using the deposit amount, interest rate and target amount.
The program just stops after the target amount has been entered, unless I change the while statement from <= to >= in which case it runs the loop but returns the number of years set at round 100's or 1000 etc...
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
//declare the variables
double rate,
balance = 0;
int deposit,
target,
years = 0;
cout << "****Lets make you some money!****" << endl << endl;
//input from the user
cout << "What is your deposit amount?: " << endl;
cin >> deposit;
cout << "What is your interest rate?: " << endl;
cin >> rate;
cout << "What is you target savings amount?: " << endl;
cin >> target;
rate = rate / 100;
while (balance <= target); //when i change this to balance >= target the 'while' runs but just returns years divisible by 100
{
// calculation
balance += deposit * pow((1 + rate), years);
//balance = balance*(1 + rate) + deposit; // alternate calculation
//years++;
//users savings target
cout << "You will reach your target savings amount in: " << balance << " years." << endl << endl << " That's not that long now is it?" << endl;
}
return 0;
}
Thanks in advance!
The problem is an unfortunate suffix:
while (balance <= target);
// ^
That is equivalently:
while (balance <= target) {
;
}
{
// calculation, which always runs exactly once
// regardless of what balance/target are
}
Just drop the semicolon.
Related
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 2 years ago.
Improve this question
So basically, I'm new to programming and can't figure it out how to add certain numbers generated by the user. I want a program, capable of collecting for n days the temperature, and at the end, inform what the peak heat occurred, as well as the average of the temperatures collected.
#include <iostream>
using namespace std;
int main()
{
int days, day0 = 0, degrees, degrees_max = 0;
float degrees_average;
cout << "Days of the study" << endl;
cin >> days;
while (days > day0)
{
cout << "How many Degrees?" << endl;
cin >> degrees;
if (degrees < 999)
{
day0++;
}
if (degrees > degrees_max)
{
degrees_max = degrees;
}
}
degrees_average = degrees / days;
cout << "The max temperature was: " << degrees_max << "C"
<< " And the average was : " << degrees_average << endl;
}
SO the only thing left is how to calculate the average.
All the help is appreciated! Thanks
#include <iostream>
using namespace std;
int main()
{
int days, max=-1;
double current, total = 0;
cin >> days;
for (int i=0; i<days; i++){
cin >> current;
if (current > max) max = current;
total = total + current;
}
cout << "max: " << max << endl;
cout << "average: " << total/days << endl;
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Im trying to create a program which displays the minimum and maximum integers as prompted but the user. The maximum is always correct, but the minimum always comes out as 0. Don't know what Im missing from my code?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int intsWanted, min, max, input;
do {
cout << "How many integers would you like to
enter?" << endl;
cin >> intsWanted;
} while (intsWanted < 1);
cout << "Please enter " << intsWanted << " integers." << endl;
cin >> min;
min = max;
intsWanted--;
while (intsWanted >= 1) {
cin >> input;
if (input > max) max = input;
if (input < min) min = input;
intsWanted--; }
cout << "min: " << min << endl;
cout << "max: " << max << endl;
return 0;
}
Change your line min=max to max=min and that will solve your question !
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm making a mortgage calculator for my class, the program runs but the answer isn't correct, I think I have the right formula but maybe I'm declaring my variables wrong.
This is what I have so far
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//Variables
double rate;
rate = rate / 1200.0;
double term;
term = term * 12;
double principal;
double monthlyPayment;
monthlyPayment = (principal * rate) / (1.0 - pow(rate + 1, -term));
//Gathering Values
cout << "Please enter your interest rate." << endl;
cin >> rate;
cout << "Please enter your term." << endl;
cin >> term;
cout << "please enter your principal" << endl;
cin >> principal;
//Formula
monthlyPayment = principal * rate / (1.0 - pow(rate + 1, -term));
//Result
cout << "Your monthly payment is, " << monthlyPayment << endl;
return 0;
}
All of the math you do before the cin statements is not being factored in to your calculation. Also, your second formula calculating monthlyPayment is missing parentheses in the numerator.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I can't seem to be able to get this to work properly, I keep getting "Stack around the variable 'judge' was corrupted". Where did I go wrong? It seems to run properly, but at the end I get the error after I close and my lowNum value won't seem to process correctly.
#include <iostream>
using namespace std;
const int SIZE = 5;
double getJudgeData(int judge);
double findLowest(double num[SIZE]);
double findHighest(double num[SIZE]);
int main()
{
double
judge[SIZE],
highNum,
lowNum;
for(int num = 1; num <= SIZE; num++)
{
judge[num] = getJudgeData(num);
highNum = findHighest(judge);
lowNum = findLowest(judge);
}
cout << highNum << endl;
cout << lowNum << endl;
system ("pause");
return 0;
}
double getJudgeData(int judge)
{
double score;
cout << "What is the score given by judge " << judge << "?" << endl;
cin >> score;
while(score < 0 || score > 10)
{
cout << "Please enter a score between 0 and 10.\n\n";
cout << "What is the score given by judge " << judge << "?" << endl;
cin >> score;
}
return score;
}
double findHighest(double num[SIZE])
{
double high = 0;
for(int count=0;count<SIZE;count++)
{
if(num[count]>high)
high=num[count];
}
return high;
}
double findLowest(double num[SIZE])
{
double low = 10;
for(int count=0;count<SIZE;count++)
{
if(num[count]<low)
low=num[count];
}
return low;
}
C++ used arrays from 0 to n-1. Your code is using 1 to n, so you "run off the end" of the array judge (and corrupt the variable around it).
Suggest you close the question since it's just a typo.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm getting this error message that says illegal else without matching if. I think something is wrong with my else statement, but I don't see where. Do I have an unneeded bracket? It's on line 78 column 2. Thank you
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string service_code, //hold the service code
account_number; //hold the account number
double final_amount = 0, // hold the final amount
final_damount, //hold the amount for the day minutes
minutes = 0, //hold the amount for minutes
day_minutes = 0, // hold the amount for fay minutes
night_minutes = 0, //hold the amount for night minutes
final_namount = 0; //hold the amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number; //Entering the account number
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code; //Enteringthe service code
cout << "Please enter the amount of minutes used: ";
cin >> minutes; //Entering the minutes you used
if(service_code == "r" || "R")
{
if(minutes <= 50)
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl; //Displaying final amount when your minutes are less than or equal to 50
}
{
if(minutes > 50)
final_amount = (minutes - 50) * 0.20 + 10;
cout << "Your final amount is: $ " << final_amount << endl; //Displaying final amount when your minutes are greater than 50
}
{
else if(service_code == "p" || "P")
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl; //Entering minutes used during the day
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl; //Entering minutes used during the night
}
{
if(day_minutes <=75)
final_damount = 0;
final_amount = final_damount + final_namount + 20; //Calculating final amount for minutes used during the day
}
{
if(day_minutes > 75)
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20; //Calcuating final amount for minutes used during the day
}
{
if(night_minutes <= 100)
final_namount = 0;
final_amount = final_damount + final_namount + 20; //Calcuating final amount for minutes used during the night
}
{
if(night_minutes > 100)
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20; //Calcuating final amount for minutes used during the night
cout << "Your final amount is: $ " << final_amount << endl; //Displaying final amount
}
{
else
cout << "Error, this program does not accept negative numbers.\n"; //Displaying error message
cout << "Account number: " << account_number << endl; //Displaying account number
cout << "Service code: " << service_code << endl; //Displaying service code
cout << "Service code: " << minutes << endl; //Displaying minutes
}
return 0;
}
Most of your if statements are missing their opening curly-braces. This means that the completely wrong code is actually being executed by your program because only the first statement after each if statement will be executed if the conditional expression is true and the rest of the code will always be executed.
...this is the same issue as Apple's infamous goto error bug: http://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/
C is not Python, indentation is not respected by the compiler and it does not mean code belongs to a certain keyword or block.
I strongly suggest reading up on C's syntax, and it's (generally) a good idea to always use curly-braces with all statement blocks (if, for, do, while, etc) to help avoid nasties like these. Internally, my team at Microsoft, runs a program called StyleCop which won't let us check-in code to our central repository if it contains any brace-less statements.