PDA

View Full Version : Function, the program said that invalid conversion from char* to char



Asy
20th June 2018, 08:51
Hello guys, can someone tell me whats wrong with my program, thanks a lot :)

#include <iostream>
using namespace std;

double totalPrice(char, int);

int main ()
{
char types [3]={'S','M','C'};
int price[3]={8,13,11};
int quantity;
double totalcost;



cout<<"Types of Sandwich Price (RM) " <<endl;
cout<< "Spicy(S) 8.00"<<endl;
cout<< "Meatball(M) 13.00"<<endl;
cout<<"Chicken (C) 11.00" <<endl;

cout << "Please enter types of sandwich you prefer : " << endl;
cin >> types;

cout << "Please enter quantity for the sandwich : " << endl;
cin >> quantity;


totalcost = totalPrice(types,quantity);
cout << "Total cost = " << totalcost << endl;


}

double totalPrice (char x, int y) //noneed semicolon due to function definition
{
double totalPrice,quantity, jumlahsemua;

if (x = 'S' )
totalPrice = y * 8;

else if (x ='M')
totalPrice = y * 13;

else if (x ='C')
totalPrice = y * 11;



jumlahsemua = totalPrice;

return jumlahsemua;

}

superpomax
20th June 2018, 19:06
This isn't Qt, this is a c++ console application. cout, cin et iostream aren't used in qt.

You should start to get the basics with c++ before learning Qt.
If you compile with a normal compilator (i.e. Visual studio, codeblock) there are a lot of problems.

First, '=' is to assign a variable. In your if clause, you want to compare so it is '=='

Secundo, types is one character, not a array of character so it should be declared char types; And the value will be assigned with cin.

In the fonction totalPrice,
no need to create jumlahsemua, you can just return totalPrice. And you don't even use quantity.