各位程序员大家好!
我打算编写一个小程序,根据用户输入的小时数和工资来计算不同时间段的总工资。我设法制作了一小部分程序,但是当我尝试运行它并测试它时,出现以下错误:
Line 33: error: no match for 'operator*' in 'pay * hours_day'
我尝试用谷歌搜索这个问题,但我真的很困惑是什么原因造成的。
这是我的完整程序代码:
#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();
}
这是我的程序的一个非常早期的版本!我只是想知道是什么原因导致了这个问题。
最佳答案
正如 @101010 暗示的那样:pay
是一个 string
,而 hours_day
是一个 float
,而有些语言允许将字符串与整数相乘,而 c++11(或任何其他形式的 c)不允许,更不用说允许字符串和 float 相乘了。
关于c++ - 'operator*' 错误不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30989236/