-1

I have to code a program which takes as input a medicine's name and its quantity from the user. Then it must calculate the quantity with the given value. However, I am getting an error with the "*" (multiply operator). The error is given below.

Error: no operator "*" matches these operands
operator types are: std::string * int

Please help me solve this. I've also attached my code where you can also see the error clearly.

#include <iostream>
#include <conio.h>
#include <string>


using namespace std;
void main()
{
    string med,quantity,panadol=0,piozer=0,burofen=0;

    cout << "Pharmacy Management System" ;

    cout << "\n Enter your Medicine Name : ";
    getline(cin,med);   

    cout << "\n Enter your Medicine Quantity : ";
    getline(cin,quantity);

    int a,b;

    if (med==panadol && med==burofen)
    {
        a= (quantity*2);
        cout << a;
    }
    if (med==piozer)
    {
        b= (quantity*14);
        cout << b;
    }
    getch();
}
Talendar
  • 1,841
  • 14
  • 23
SK Hassan
  • 27
  • 9
  • Please post a [mcve] with an emphasis on *minimal* . Things like print statements aren't required to reproduce your issue and should be removed. Also make sure to post the entire error message, including the line number. – xaxxon Feb 10 '19 at 19:10
  • `std::string` stores **text**. How would you multiply text by `14`? I assume that you want to first parse that string to a numeric value. You might be interested in [`std::stoi`](https://en.cppreference.com/w/cpp/string/basic_string/stol). – Fureeish Feb 10 '19 at 19:10
  • Or how about making quantity an int? – Ian4264 Feb 10 '19 at 19:11
  • I try to to make quantity an int but getting an another error "invalid null operator" . – SK Hassan Feb 10 '19 at 19:16
  • 3
    C++ can't be learned by guessing. A good [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is in order. – Ron Feb 10 '19 at 19:16
  • Well, you'd have to change the input mechanism; you'd probably still read the line into a string, but then convert the string into an integer. Or, at a pinch, you'd use `cin >> quantity;` but that would leave the newline in the input stream. Read as string and convert is probably best. – Jonathan Leffler Feb 10 '19 at 19:17
  • @SKHassan Ask yourself the right question. What is the correct type of `quantity`? Obviously a quantity is a number, so it should be an `int` not a `string`. Now make `quantity` an `int` and solve the `"invalid null operator"` problem, whatever that is. – john Feb 10 '19 at 19:18
  • But how to solve "invalid null operator" problem? – SK Hassan Feb 10 '19 at 19:19
  • I just solve "invalid null operator" problem by making some changing in my codes but now i am not getting any output. – SK Hassan Feb 10 '19 at 19:27
  • @SKHassan But your changes were **wrong** and now your code is even further from being correct. Like another poster said you cannot program by guessing. Just because an error goes away doesn't mean you have solved it. – john Feb 10 '19 at 19:30
  • @SKHassan What I'm saying is don't try and fix errors you don't understand. Try and understand the error first. – john Feb 10 '19 at 19:33
  • @john you are right i have to understand first. – SK Hassan Feb 10 '19 at 19:34
  • @SKHassan Well I hope you find my answer useful. – john Feb 10 '19 at 19:35
  • @john you answer is helpful and i understand my all error but program doesn't print output just take input from user but doesn't generate output. – SK Hassan Feb 10 '19 at 19:37
  • @john i just make all the changing in my code that was told by you in the answer but now i am not getting output result. – SK Hassan Feb 10 '19 at 19:44
  • @SKHassan With out seeing your latest code I can't guess what is wrong. So you should ask a new question with your new code (don't add the new code to this question). – john Feb 10 '19 at 19:45
  • @john just give me few minutes i will post my latest code. – SK Hassan Feb 10 '19 at 19:53
  • @john Thanks! I just solve the problem and now my program is printing output your answer is very helpful to me thanks again. – SK Hassan Feb 10 '19 at 20:01

3 Answers3

1

There's some basic misunderstandings in your code. You have to decide what is a variable and what is not, and when you use a variable you have to decide what type it is.

There seem to be three basic errors in your code

string med,quantity,panadol=0,piozer=0,burofen=0;

panadol, poizer and burofen are names of medicines that you want to see if the user entered. So they should not be variables they should be string literals. Like this

if (med == "piozer")
{
    b= (quantity*14);
    cout << b;
}

See "piozer" is a string literal and the code above asks if med equals "piozer". So delete the piozer variable and replace with a string literal, same with all the other medicine names. This is the solution to your invalid null operator problem.

Second error is that quantity should be an int. This should be obvious, you want to multiply quantities, how can you multiply a string?

Third error is here

if (med == "panadol" && med == "burofen")

It cannot be true that med equals "panadol" and med equals "burofen". It's logically impossible for one variable to be equal to two different values at the same time. Clearly what you really meant is this

if (med == "panadol" || med == "burofen")

If med equals "panadol" OR med equals "burofen", i.e. if either one is true, not if both are true.

john
  • 85,011
  • 4
  • 57
  • 81
0

Quantity is a string, 2 is an int. You can't * together a string and an int.

Maybe you want to stoi on quantity?

xaxxon
  • 19,189
  • 5
  • 50
  • 80
0

Your variable quantity is a string. You are getting an error because of your attempt to multiply a string by a number. Instead of using the function getLine(), that should be used to retrieve strings, try to do it like below:

cin >> quantity;
Talendar
  • 1,841
  • 14
  • 23