我是一名新手学生,在接受cse 100帮助时,理解这个主题表示赞赏。 它编译得很好,但是在运行时它只提示用户一次,经过几次更改,老师建议现在程序没有编译。它给了我以下错误。
main.cpp|30|error: no match for 'operator*' (operand types are 'const double' and 'std::string {aka std::basic_string<char>}')|
main.cpp|33|error: no match for 'operator*' (operand types are 'const double' and 'std::string {aka std::basic_string<char>}')|
main.cpp|36|error: no match for 'operator+' (operand types are 'std::string {aka std::basic_string<char>}' and 'double')|
尝试过的代码:
#include <iostream>
using namespace std;
int main()
{
const double adultTicket = 9.50; //declared const double with value of
//$9.50price per adult ticket.
const double childTicket = 6.50; //declared const double with value
//of $6.050 per child ticket.
cout << "Please enter movie name \n"; //User prompt to enter movie name.
string movName;
getline(cin,movName); //Declared variable told name entered by user.
cout << "Please enter number of Adult tickets sold \n"; //User prompt to
//enter amounnt of adult tickets sold.
string adultTicketsSold;
getline(cin, adultTicketsSold); //Declared variable that holds number of
//adult tickets sold.
cout << "Please enter number of children tickets sold \n"; //User prompt
//to enter number of children tickets sold.
string childTicketsSold;
getline(cin,childTicketsSold); // Declared variable to hold number of
//child tickets sold.
string grossAdult;
grossAdult = adultTicket*adultTicketsSold;
double grossChild;
grossChild = childTicket*childTicketsSold;
double grossBox;
grossBox = grossAdult+grossChild;
double distributorTake;
distributorTake = grossBox*0.80;
double netBox;
netBox = grossBox*0.20;
cout << "Revenue Report" <<endl;
cout << "Movie name:" << movName <<endl;
cout << "Adult Tickets Sold:" << adultTicketsSold <<endl;
cout << "Child Tickets Sold:" << childTicketsSold <<endl;
cout << "Gross Box Office Profit: $" << grossBox <<endl;
cout << "Amount Paid to Distributor: $" << distributorTake <<endl;
cout << "Net Box Office Profit: $" << netBox <<endl;
return 0;
}
答案 0 :(得分:2)
问题在于这些问题,您尝试将std::string
乘以double
,而不是将两个double
相乘。
grossAdult = adultTicket*adultTicketsSold;
您可以使用std::stod
std::string
转换为double
grossAdult = adultTicket * std::stod(adultTicketsSold);
或者如果您愿意,您可以接受double
作为输入,而不是进行转换
cout << "Please enter number of Adult tickets sold \n";
double adultTicketsSold;
cin >> adultTicketsSold;