Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No match for 'operator*' error

Tags:

c++

c++11

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.

like image 607
Gabriel A. Avatar asked Jul 25 '26 22:07

Gabriel A.


2 Answers

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.

like image 82
Scott Hunter Avatar answered Jul 27 '26 13:07

Scott Hunter


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.

like image 35
Gabriel A. Avatar answered Jul 27 '26 13:07

Gabriel A.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!