2016-09-08 3 views
-3

저는이 항목을 이해하는 데 도움이되는 조언을받는 초보 학생입니다. 괜찮 았지만 컴파일 할 때 교사가 지금 제안한 몇 가지 변경 사항을 적용한 후 프로그램을 컴파일하지 않으면 사용자에게 한 번만 프롬프트를 표시합니다. 그것은 다음과 같은 오류를 제공합니다.컴파일러에서 오류가 발생했습니다. '연산자 *'와 일치하지 않습니다.

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

그 컴파일러 오류 변환을 또는 당신이 선호하는 경우 그냥 double 입력으로 받아 들일 수 std::stod

grossAdult = adultTicket * std::stod(adultTicketsSold); 

을 대신 사용하고 double A와 (예를 들어'std :: string'과'doubl '과 같은) 연산자를 사용하지 않는 두 가지 타입에 대해 연산자 (예 :'*')를 사용한다는 것을 의미합니다 e'; '0.5 * "hello world"는 의미가 없습니다!). 코드를보고 실수로 만든 장소를 찾으십시오. – Conduit

답변

2

문제점은 대신 두 개의 double를 곱하여으로하는 double하여 std::string를 곱 위해 노력하고,이 라인이다.

grossAdult = adultTicket*adultTicketsSold; 

당신은 변환 할 수 std::string

cout << "Please enter number of Adult tickets sold \n"; 
double adultTicketsSold; 
cin >> adultTicketsSold; 
+0

미래의 독자를 위해 :'stod'는 던질 수 있고, 어떤 이유로 당신의 전화가 던질 것이고, 당신은'try ... catch'를 원할 것입니다. – AndyG

+0

@AndyG 좋은 제안. 그리고 그 메모에서'cin' 대안 다음에 스트림을 검사 할 가치가 있습니다. 비 숫자 값을 입력 한 경우'cin.good()'를 확인할 수 있습니다. – CoryKramer

관련 문제