Hello fellow programmers!
I was going to write a small program for calculating total pay for different periods of time depending on the amount of hours and the salary that the user enters. I managed to make a small bit of the program but when I try to run it and test it I get the following error:
Line 33: error: no match for 'operator*' in 'pay * hours_day'
I tried doing a google search on this problem but I am really confused on what causes it.
here is my full program code:
#include <iostream>
#include <random>
#include <time.h>
#include <cstdlib>
#include <string>
using namespace std;
/*
*Program Flowchart*
- Find out how much a worker gets paid per hour, and then find out how many hours they work
a day, and then multiply that to get the total pay in a week or a month or a year.
*/
// global variables
string pay;
float hours_day;
// function that takes the amount of money and calculates the total pay in a week
int total_pay_week() {
cout << "Type in your salary per hour." << endl;
cin >> pay;
cout << "Ok, how many days do you work per day?" << endl;
cin >> hours_day;
float total_day = pay * hours_day;
float total_week = total_day * 7;
cout << "Your total pay in a week is " << total_week << "if you worked " << hours_day << " per day. " << endl;
}
int main() {
total_pay_week();
}
This is a very early version of my program! I just wanted to know what causes this problem.
As #101010 hints at: pay is a string, while hours_day is a float, and while some languages allow you to multiply strings with integers, c++11 (or any other flavor of c) doesn't, much less allow strings and floats to be multiplied together.
I realised that I declared the pay variable as a string right as I was trying to solve this. Thank you to everyone though! It was a very noobish mistake from me.
You declared pay as a string. Change that to int or float and it should multiply. The class std::string has not * operator for this math, so that explains the error text.
You can update your program code as per below to fix the issue
As Scott suggested you are taking input as string and trying to multiply it with float which is not possible. You need to take input as float and everything would work.
#include <iostream>
#include <random>
#include <time.h>
#include <cstdlib>
#include <string>
using namespace std;
/**
* *Program Flowchart*
*
* - Find out how much a worker gets paid per hour, and then find out how many hours they work
* a day, and then multiply that to get the total pay in a week or a month or a year.
*/
// global variables
float pay;
float hours_day;
// function that takes the amount of money and calculates the total pay in a week
int total_pay_week()
{
cout << "Type in your salary per hour." << endl;
cin >> pay;
cout << "Ok, how many days do you work per day?" << endl;
cin >> hours_day;
float total_day = pay * hours_day;
float total_week = total_day * 7;
cout << "Your total pay in a week is $" << total_week << " as you are working " << hours_day << " hours per day. " << endl;
}
int main()
{
total_pay_week();
}
Checkout https://ideone.com/mQj3mh for actual running of the code.
Related
I'm new to coding and am having trouble with one of my first projects.
So I have to basically type a number and then have that number spit out some answers involving things like addition and multiplication. (ex. if I put in 5, one of the answers is to simply multiply that 5 by 30. Or another adds it).
The issue is that the only way I can get that number to be different and give the corresponding answers is if I go into the code and change the integer assigned to the variable, which is pretty counterintuitive.
I've tried deleting the variable, setting = 0, moving it around; nothing is working.
stocks = 0;
investment = stocks * 30;
charges = investment * .015;
total = charges + investment;
cout << "Cindy, how much are you investing?";
cin >> stocks;
cout << endl;
some of the code there. Line in question is the "stocks = 0;" one. After that, it's the other variables that would be output with their associated variables. Any help would be appreciated, I hope this makes sense for what I'm asking! Thanks in advance
You will have to cin the number.
#include <iostream>
int main() {
int stocks;
std::cout << "Cindy, how much are you investing?";
std::cin >> stocks;
investment = stocks * 30;
charges = investment * .015;
total = charges + investment;
std::cout << "The total charges would be " << total << "." << std::endl;
}
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main () {
//initializing my variables
double mealcost;
float tax_percent, tip_percent, tax_total, tip_total, overall_total;
cout << "What is the cost of your meal?" << endl;
cin >> mealcost;
cout << "What percent tip would you like to leave?" << endl;
cin >> tip_percent;
cout << "What percent are you taxed?" << endl;
cin >> tax_percent;
tax_total = mealcost * (tax_percent/100);
tip_total = mealcost * (tip_percent/100);
overall_total = mealcost + tax_total + tip_total;
/*trying to take the overall total from the formula above and round it
to the nearest whole integer*/
round (overall_total);
cout << "What is the total cost of my meal? " << overall_total << endl;
return 0;
}
Whenever I run my code it compiles correctly and gives me the correct overall total, but the round function seems to not work. I input 12 dollars for the meal total, 8 percent tip, and 20 percent tax. The correct answer is $15.36, but I'd like to round it down to $15. Any help would be appreciated thanks.
You must assign the return value of the round() function to overall_total, like this:
overall_total = round(overall_total);
The above line should replace round (overall_total);.
Some functions in C++ take a reference (pass-by-reference) to the parameters of the function, e.g. std::sort(), so that you simply std::sort(v.begin(), v.end()) and the vector v is sorted without you having to assign the return value. (Technically, std::sort takes in iterators which have similarities to pointers, but they basically have the same result.)
However, the round() function actually takes a copy (pass-by-value) of the parameter and returns a new value - it does not directly 're-assign' the parameter to have the rounded value. Therefore, you must assign the return value of the function to a variable (or in this case, the same variable in order to 're-assign').
You can learn more about the difference here:
What's the difference between passing by reference vs. passing by value?
Very new to C++. Doing my best to get a good handle on this.
The extra couts after the read statements were for troubleshooting purposes to make sure it was actually reading user input.
I feel like this is a dumb question but iv been scouring the forums to find something similar to my issue.
I cannot for the life of me figure out how to get this program to start calculating the correct interest. When comparing to an online interest calculator it comes up with a very different number than what the program is calculating.
I have spent tons of hours troubleshooting this and texting different algorithms but I have yet to come out on-top. I dont normally like asking for help because I learn by struggling through something but this one is throwing me for a loop...
So I think I might either be having an issue with the libraries I am using or a calculation error. I know the code is a bit messy and I am learning how to clean it up but as for now I just want to get the code calculating compound interest based off of user input. I have added comments to explain what each section of code does to help with my messiness.
I dont believe its a syntax error but with the way I am either using a few of the commas in the algorithm or my parentheses are somehow wrong.
the formula I am using is,
A = P (1 + r/n)^(nt)
Amount = principle (1 + interest rate/times compounded)^(rate, times
compounded)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double rate;
double time;
double principle;
double amount;
double amount2;
//Asking for the amount of money that the user would like to invest
cout << "What is the amount of money you would like to invest? ";
cin >> principle;
cout << principle << endl;
//Asking for the interest rate that will be compounded annually
cout << "What is the interest rate you would like to calculate? ";
cin >> rate;
//interest rate divided by 100 so it can be multiplied by the principle and the amount of time the money will be invested later in code.
rate /= 100;
cout << rate << endl;
//Asking for the amount of years that the user will invest.
cout << "How many times would you like to compound your money? ";
cin >> time;
cout << time << endl;
//calculation for amount of money that will be made after all user input is input
amount2 = pow(rate, time);
cout << amount2 << endl;
amount = (principle * (1 + rate/time), amount2);
//Output data after all user data is input and calculated.
cout << "Your will have "; cout << amount; cout << " dollars in
interest! "<< endl;
return 0;
}
This line:
amount = (principle * (1 + rate/time), amount2);
Does absolutely nothing with principle * (1 + rate/time). That expression is evaluated, and then the results are discarded, leaving amount to be assigned the value in amount2. I assume you were trying to call a function with those two expressions as parameters.
http://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator
In a comma expression E1, E2, the expression E1 is evaluated, its
result is discarded, and its side effects are completed before
evaluation of the expression E2 begins.
Now that we have your formula:
A = P (1 + r/n)^(nt)
Amount = principle (1 + interest rate/times compounded)^(rate * times compounded)
I am not certain you are computing the formula you have listed above, and note the correction to the second line, where I replace the comma with multiplication.
amount2 = pow(rate, time);
Computes rate^time, where you should have:
amount2 = 1 * time; // Where one is the times compounded per period (year)
So that your other line can read:
amount = principle * pow((1 + rate, amount2);
Because you are compounding once per year, you are dividing rate by 1, not by the number of years. By the same token, amount2 is now strictly equal to time;
I'm very new to coding (like one day ago), so you can pretty much assume I know nothing. I'm finding it very hard to debug my extremely simple programs and most of the help I've seen online is either over my head or too specific to the program.
This is the code I'm working on now.
/*
* LogBase8.cpp
*
* Created on: Feb 13, 2015
* Author: Holly
*/
//This program calculates the log base 8 of a number.
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
//Declare variables and constants.
float x, log_8_x;
int main()
{
//Prompt user to input number.
cout << "This program will calculate the log base 8 of a number. Input number to calculate" << endl;
cin >> x;
//Calculate log_8_x.
log_8_x = log(x) / log(8);
//Print log_8_x.
cout << "The log base 8 of " << x << " is " << log_8_x << endl;
//End main.
return (0);
}
I have an error in the log_8_x = log(x) / log(8); line that says
no match for 'operator=' (operand types are 'std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}' and 'double')
I'm using Eclipse, if that's relevant.
You have other errors as stated in your question, but anyway putting a \ to escape the newline appearing in the string literal here
cout << "This program will calculate the log base 8 of a number. Input \
fixes your compilation errors.
See fully working sample here please.
I am a beginner at c++ who has a Python background and I am currently self teaching c++ before school starts so I can get ahead of the game. There's this practice problem that I found and I am about 95% complete, but the last 5% of the instructions in the question is a bit confusing to me. I am particularly stuck at the bolded part of last sentence.
Practice problem:
Write a short program that asks for your height in integer inches and then converts your height to feet and inches. Have the program use the underscore character to indicate where to type the response.Also use a const symbolic constant to repre-sent the conversion factor.
My CURRENT work:
// simpleheight.cpp
#include <iostream>
using namespace std;
int main ()
{
int number, inches, feet;
cout << "Enter your height in integer inches";
cin >> number;
inches = number%12;
feet = number/12;
cout << "You are " << feet << " feet" << " and" << inches << " inches" << endl;
cin.get();
cin.get();
return 0;
}
As you guys can see, it's nearly done, but I just don't get what the problem wants from me for the bolded sentence. Is it perhaps asking me to do something like this:
const int inches = number%12;
const int feet = number/12;
If not, can someone guide me through the last 5% of this question :D?
I would do like this.
static const unsigned int FACTOR = 12;
...
inches = number%FACTOR;
feet = number/FACTOR;
By doing a quick search you will notice that a symbolic constant is something that cannot be altered. So a const will be fine in this context.
you should use conversation value of inch to feet.
const double incToFeet = 0.08333;
and then use that value to convert