Start New Discussion within our Software Development Community

I would like to make it so you enter how much you will give in (E/G £20 and £15 in change) and then it tells you how much change you get back. I will post the code and if someone could fix it so you enter amount you'll pay and then says how much change.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
  string mystr;
  float price=0;
  int quantity=0;

  cout << "Enter price: ";
  getline (cin,mystr);
  stringstream(mystr) >> price;
  cout << "Enter quantity: ";
  getline (cin,mystr);
  stringstream(mystr) >> quantity;
  cout << "Total price: " << price*quantity << endl;
{
  string mystr;
  float price=0;
  int quantity=0;

  cout << "Pay: ";
  getline (cin,mystr);
  stringstream(mystr) >> pay;
  cout << "Change: " << pay-change << endl;

}
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
  string mystr;
  double price=0;
  double pay = 0;
  int quantity=0;
  

  cout << "Enter price: ";
  getline (cin,mystr);
  stringstream(mystr) >> price;
  cout << "Enter quantity: ";
  getline (cin,mystr);
  stringstream(mystr) >> quantity;
  cout << "Total price: " << price*quantity << endl;

  cout << "Pay: ";
  getline (cin,mystr);
  stringstream(mystr) >> pay;
  cout << "Change: " << pay - price*quantity << endl;

  return 0;
}
This question has already been answered. Start a new discussion instead.