I have a problem with my program and I would appreciate help.
It's supposed to allow you to enter an account number, a service code, and the number of minutes the service was used. The program then calculates the bill and it varies depending on your service. When I execute the program, it doesn't allow you to enter anything.
Regular service:$10.00 plus first 50 minutes free. Charges for over 50 minutes are $0.20 per minute.
Premium service:
$25.00 plus:
a)
For calls made from 6:00 am to 6:00 pm, the first 75 minutes are free; charges for over
75 minutes are $0.10 per minute.
b)For calls made from 6:00 pm to 6:00 am, the first 100 minutes are free; charges for over 100 minutes are $0.05 per minute.
Here is the program that I've typed.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int minutes = 0,
day_minutes = 0, //minutes used during the day
night_minutes = 0; //minutes used during the night
string service_code,
account_number;
double final_amount,
final_damount, //final amount for day minutes
final_namount = 0; //final amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number;
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code;
if (service_code == "r")
{
cout << "Please enter the amount of minutes used: " << minutes << endl;
}
if (minutes <= 50)
{
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl;
}
if (minutes > 50)
{
final_amount = (minutes * 0.20) + 10;
cout << "Your final amount is $: " << final_amount << endl;
}
else if (service_code == "p")
{
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl;
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl;
}
if (day_minutes <=75)
{
final_damount = 0;
final_amount = final_damount + final_namount + 20;
}
if (day_minutes > 75)
{
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes <= 100)
{
final_namount = 0;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes > 100)
{
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20;
cout << "Your final amount is: $ " << final_amount << endl;
}
else
cout << "Error, this program does not accept negative numbers.\n";
return 0;
}
Does anyone the problem to my program? Thank you.
In your code you never specify to ask for user input (read from any input streams or files), hence it doesn't ask for any input.
You will probably want to somewhere do something such as cin or cin.readline or any of several other various methods to read the user's input from stdin.
To avoid duplicating other questions I'm not putting further details here.
Related
I've been working on a mortgage calculator for my C++ class. However, I am stuck.
I got the following formula off of Nerdwallet and tried to implement it in my program:
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
M=mortgage payment
P=Principal
i=interest
n=number of payments
Here's the code that I am currently using.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv)
{
int years, homePrice, creditScore, totalPayments;
float rate, mortgageTotal, monthlyPayment;
cout << "What is the price of the home that you are looking to mortgage?\n";
cin >> homePrice; //Assigns the variable homePrice to a value
cout << "What is your credit score?\n";
cin >> creditScore; //Assigns the creditScore variable a value
cout << "Would you prefer a 15 year or 30 year mortgage? Please type 15 or 30.\n";
cin >> years;
if ( years = 15 ) //If input is 15 year it will go down this logical path
{
if (creditScore >=760) //If their credit rating is equal to or more than 760, their rate will be .043 also nested if.
{
rate = .043;
cout << "Your interest rate is 4.3%\n";
}
else if (creditScore >= 700) //If their credit rating is equal to or more than 700, their rate will be .0455
{
rate = .0455;
cout << "Your interest rate is 4.55%\n";
}
else if (creditScore >= 660) //If their credit rating is equal to or more than 660, their rate will be .048
{
rate = .048;
cout << "Your interest rate is 4.8%\n";
}
else if (creditScore >= 620) //If their credit rating is equal to or more than 620, their rate will be .058
{
rate = .058;
cout << "Your interest rate is 5.8%\n";
}
else if (creditScore >= 580) //If their credit rating is equal to or more than 580, their rate will be .0655
{
rate = .0655;
cout << "Your interest rate is 6.55%\n";
}
else if (creditScore >= 500) //If their credit rating is equal to or more than 500, their rate will be .083
{
rate = .083;
cout << "Your interest rate is 8.3%\n";
}
}
else if ( years=30 )
{
if (creditScore >= 760)
{
rate=.043;
cout <<"Your interest rate is 4.3%\n";
}
else if (creditScore >= 700)
{
rate=.0455;
cout << "Your interest rate is 4.55%\n";
}
else if (creditScore >= 660)
{
rate=.048;
cout << "Your interest rate is 4.8%\n";
}
else if (creditScore >= 620)
{
rate=.058;
cout << "Your interest rate is 5.8%\n";
}
else if (creditScore >= 580)
{
rate=.0655;
cout << "Your interest rate is 6.55%\n";
}
else if (creditScore >= 500)
{
rate=.083;
cout << "Your interest rate is 8.3%\n";
}
}
totalPayments = years * 12;
monthlyPayment = homePrice * [[rate * (1 + rate)pow(totalPayments)] / [(1 + rate)pow(totalPayments) - 1]];
mortgageTotal = monthlyPayment * totalPayments;
cout << "Your mortgage will cost approximately " << mortgageTotal << " and your monthly payment will be " << monthlyPayment << endl;
return 0;
}
However, when I go to compile it, I get the following errors:
Errors
I just don't understand the errors and why they are there.
If someone could help me, I'd greatly appreciate it.
Thank you.
While your math formula uses both []s and ()s for grouping expressions, only ()s can be used in such a way in C++.
pow is a function call, not an in-place operator like you seem to be using it. It needs to look like pow(1 + rate, totalPayments).
Your ifs also are doing assignments (=) instead of comparisons (==). As it is, your code will only follow the first if because it is setting years to 15.
Your mortgage formula seems wrong. Try this
monthlyPayment = homePrice * ((rate * pow(1 + rate, totalPayments)) /
(pow(1.00 + rate, totalPayments) - 1));
I am writing a program for class where it converts from military time to standard. Everything else seems to be working fine until I type in 60 for the minutes. For example if I type 23:60 it gives me 11:60 PM which is incorrect. How do I fix this? I tried checking if minutes == 60 to reset minutes to 0 but I cant figure it out.
#include <iostream>
using namespace std;
void inputData(int&, int&, char&); // this function asks users to input
information
void convertData(int&, int&, char&); // this converts the military time to
standard time
int outputData(int&, int&, char&); // this function puts all the other
information together to output certain data
int main ()
{
int hours, minutes;
char am_pm;
char trueValue;
do
{
inputData(hours, minutes, am_pm); // calls to input function
convertData(hours, minutes, am_pm); // calls to the conversion function
outputData(hours, minutes, am_pm); // calls to function that outputs all
the data
cout << "Would you like another conversion? Type Y or y to repeat." <<
endl;
cin >> trueValue;
}
while (trueValue == 'y'|| trueValue == 'Y');
if (trueValue != 'y' || trueValue != 'Y')
cout << "Thanks for using this converter. Have a nice day." << endl;
return 0;
}
void inputData (int &hours, int &minutes, char &am_pm)
{
cout << "Please enter hours (less than or equal to 24): "; // ask user to
input hours.
do
{
cin >> hours;
if (hours > 24)
cout << "ERROR! Must be less than 24" << endl;
}
while (hours > 24); // end of hours loop
cout << "Please enter minutes (less than or equal to 60): "; // ask user to
input minutes.
do
{
cin >> minutes;
if (minutes > 60)
{
cout << "Must be less than 60. Try again!" << endl;
}
}
while (minutes > 60); //end of minutes loop
cout << endl;
cout << "You have entered: " << hours << ":" << minutes; // display what
user inputs together.
cout << endl;
}
void convertData(int &hours, int &minutes, char &am_pm)
{
if (minutes == 60)
{
hours++; // add an hour to 'hours'
minutes = minutes/60;
}
if (hours < 12)
{
hours = 12-12+1;
}
if (hours > 12)
{
hours = hours - 12; // subtracts anything bigger than 12 to get standard
time. Anything over 12 is PM according to project instruction
am_pm = 'P';
}
else
if (hours == 12)
{
am_pm = 'P';
}
else
am_pm = 'A';
}
int outputData(int &hours, int &minutes, char &am_pm)
{
if (am_pm == 'P')
cout <<"Your standard time is: " << hours << ":" << minutes << " P.M" <<
endl;
else
cout <<"Your standard time is: " << hours << ":" << minutes << " A.M" <<
endl;
}
There's a couple of places you test for minutes > 60. Try minutes >= 60 instead.
Same with hours > 24.
Your if statement for minutes being > 60. Change it to >= 60. Currently your if statement is accepting 60 as a valid condition for minutes.
I have a requirement to get time interval until end of month in C++. Are there any C++ APIs which can easily do that for me?
I need to start a timer which will expire at the first day of next month at 00:00:00 hours. For that, I need to compute the time interval i.e. number of seconds from now till end of this month.
Its rather straight foward. Get the difference in number of days between now and the last day of the month. Then get the number of seconds in a day.
Now you have the number of seconds till the end of the month, now subtract that with the number of seconds right now so far today and you will get the number of seconds until the end of the month.
#include <time.h>
#include <iostream>
using namespace std;
int main(){
int day1,month1,year1;
int day2,month2,year2;
int i,temp,DaysDiff=0;
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
int totalDiffDaysSecs=0;
int nowSeconds=0;
int expire=1000;
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout<<"\n";
day1=now->tm_mday;
month1=(now->tm_mon + 1);
year1=(now->tm_year + 1900);
day2=31;
month2=7;
year2=2015;
temp=day1;
for(i=month1;i<month2+(year2-year1)*12;i++){
if(i>12){
i=1;
year1++;
}
if(i==2){
if(year1%4==0 && (year1%100!=0 || year1%400==0))
month[i-1]=29;
else
month[i-1]=28;
}
DaysDiff=DaysDiff+(month[i-1]-temp);
temp=0;
}
cout <<"Current Time = "<< now->tm_hour << ":" << now->tm_min << "."<<now->tm_sec<<"\n";
cout <<"Today's date "<< (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday << " \n";
cout <<"Target date "<< year2 << '-' << month2 << '-' << day2 << " \n";
DaysDiff=DaysDiff+day2-temp;
cout<<"Target Month = "<<i<<"\n";
cout<<"Days in Target month = "<<month[i-1]<<"\n";
cout<<"Days diff Today - Target Month = "<<DaysDiff<<" \n";
totalDiffDaysSecs=DaysDiff*24*60*60;
nowSeconds= (now->tm_hour * 3600) + (now->tm_min * 60)+ now->tm_sec;
cout<<"Total seconds in a day = "<<24*60*60<<" \n";
cout<<"current total seconds so far today = "<< nowSeconds <<"\n";
cout<<"Total number of seconds in "<< DaysDiff <<" days = "<< totalDiffDaysSecs <<"\n";
cout<<"\n\n";
while(expire>0){
t = time(0); // get time now
now = localtime( & t );
nowSeconds= (now->tm_hour * 3600) + (now->tm_min * 60)+ now->tm_sec;
expire=totalDiffDaysSecs - nowSeconds;
cout <<"Seconds until end of month = "<< expire <<" \r";
}
cout<<"\n\n";
cout<<"Countdown expired.\n\n";
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 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.
4.16 (Salary Calculator) Develop a C++ program that uses a while statement to determine the
gross pay for each of several employees.
When someone works 41 hours or more. They get paid 1.5x more so my problem is that in my else if statement. I did rate * 1.5 which should translate into 10 * 1.5 = 15 but I get 425 because my code on the top probably, but I don't understand it at the moment.
He gave us this example. Which I'm trying to emulate.
"
Enter hours worked (-1 to end): 41
Enter hourly rate of the employee ($00.00): 10.00
Salary is $415.00
"
#include <iostream>
using namespace std;
int main()
{
double salary=0,rate=0,hours=0,counter=0;
cout << "Enter your hours worked: ";
cin >> hours;
cout << "Enter your rate: ";
cin >> rate;
while(counter<=hours)
{ salary=hours*rate;
counter++;
}
if(hours==-1)
{exit(0);
}
else if(hours>=41)
{
rate = rate * 1.5;
salary = salary + rate;
}
cout << "$" << salary;
return 0;
}
The loop you used has no functionality related to the problem, so I omitted it. Below is what will work. As others have said, define why you need a loop. I'm guessing you need to loop the code so the user can repeat this to their heart's content. If that is the case, I'll let you try to figure out how to break out of the loop when a user enters -1.
if (hours == -1)
exit(0);
else if (hours <= 40)
salary = rate * hours;
else {
double overtimeHours = hours - 40;
salary = rate * 40;
rate = rate * 1.5;
salary = salary + (rate * overtimeHours);
}
cout << "$" << salary;