저는이 항목을 이해하는 데 도움이되는 조언을받는 초보 학생입니다. 괜찮 았지만 컴파일 할 때 교사가 지금 제안한 몇 가지 변경 사항을 적용한 후 프로그램을 컴파일하지 않으면 사용자에게 한 번만 프롬프트를 표시합니다. 그것은 다음과 같은 오류를 제공합니다.컴파일러에서 오류가 발생했습니다. '연산자 *'와 일치하지 않습니다.
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;
}
그 컴파일러 오류 변환을 또는 당신이 선호하는 경우 그냥
double
입력으로 받아 들일 수std::stod
을 대신 사용하고
double
A와 (예를 들어'std :: string'과'doubl '과 같은) 연산자를 사용하지 않는 두 가지 타입에 대해 연산자 (예 :'*')를 사용한다는 것을 의미합니다 e'; '0.5 * "hello world"는 의미가 없습니다!). 코드를보고 실수로 만든 장소를 찾으십시오. – Conduit