Results 1 to 2 of 2

Thread: Function, the program said that invalid conversion from char* to char

  1. #1
    Join Date
    Jun 2018
    Location
    Malaysia
    Posts
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Function, the program said that invalid conversion from char* to char

    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;

    }

  2. #2
    Join Date
    Jun 2018
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function, the program said that invalid conversion from char* to char

    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.

Similar Threads

  1. Replies: 2
    Last Post: 18th January 2017, 11:35
  2. Invalid conversion from ‘char*’ to ‘char’
    By Atomic_Sheep in forum General Programming
    Replies: 4
    Last Post: 16th January 2015, 12:20
  3. int to char conversion
    By cooper in forum Newbie
    Replies: 3
    Last Post: 14th June 2011, 04:50
  4. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 10:32
  5. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 14:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.