I was given this file to calculate change, again how do i make my own file into
ID: 3757033 • Letter: I
Question
I was given this file to calculate change, again how do i make my own file into a class so i can include it into this one.
#include <iostream>
#include <string>
#include "my_change_calculator.h"
using namespace std;
int main() {
string cont = "y";
double cash = 0, purchase = 0;
while(cont != "n"){
//input
cout << "Enter total bill: ";
cin >> purchase;
cout << "Enter cash amount you provide: ";
cin >> cash;
//output
string output = calculateChange(purchase, cash);
cout << output;
//loop
cout << "Continue (y/n)? ";
cin >> cont;
}
return 0;
}
MY File
#include <iostream>
#include <string>
#include <math.h>
#include <sstream>
using namespace std;
int main(){
double c,p;
string check="y";
cout<<"Enter a value :";
cin>>c;
while(c!=0 && check!="n"){
cout<<"Enter an amount of purchases made against this amount : ";
cin>>p;
if(p>c){
cout<<"purchases cannot exceed cash value "<<endl;
}
else{
double r=c-p;
cout<<"The remaining change is:"<<r<<endl;
cout<<"Twenty US Dollar Bills: "<<(int)((int)r/20)<<endl;
r= r-(int)((int)r/20)*20;
cout<<"Ten US Dollar Bills: "<<(int)((int)r/10)<<endl;
r= r-(int)((int)r/10)*10;
cout<<"Five US Dollar Bills: "<<(int)((int)r/5)<<endl;
r= r-(int)((int)r/5)*5;
cout<<"One US Dollar Bills: "<<((int)r)<<endl;
r= r-(int)r;
double i=r;
string s;
string t;
stringstream out;
out << i;
s = out.str();
t = s.substr(s.find(".")+1);
int digits=t.length();
r=(r*pow(10,digits));
int r1=r;
cout<<"Fifty US Cents Coins: "<<(int)(r1/50)<<endl;
r1 = r1-(int)(r1/50)*50;
cout<<"Twenty-Fifty US Cents Coins: "<<(int)(r1/25)<<endl;
r1= r1-(int)(r1/25)*25;
cout<<"Ten US Cents Coins: "<<(int)(r1/10)<<endl;
r1 = r1-(int)(r1/10)*10;
cout<<"Five US Cents Coins: "<<(int)(r1/5)<<endl;
r1= r1-(int)(r1/5)*5;
cout<<"One US Cents Coins: "<<r1<<endl;
r1= 0;
cout<<"Continue(y/n)? ";
cin>>check;
c=c-p;
}
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
using namespace std;
string calculateChange(double p, double c) {
double r = c-p;
string str = "";
str += "The remaining change is: " + to_string(r);
str += " Twenty US Dollar Bills: " + to_string((int)((int)r/20));
r= r-(int)((int)r/20)*20;
str += " Ten US Dollar Bills: " + to_string((int)((int)r/10));
r = r-(int)((int)r/10)*10;
str += " Five US Dollar Bills: " + to_string((int)((int)r/5));
r= r-(int)((int)r/5)*5;
str += " One US Dollar Bills: " + to_string((int)r);
r = r-(int)r;
double i=r;
string s;
string t;
stringstream out;
out << i;
s = out.str();
t = s.substr(s.find(".")+1);
int digits=t.length();
r=(r*pow(10,digits));
int r1=r;
str += " Fifty US Cents Coins: " + to_string((int)(r1/50));
r1 = r1-(int)(r1/50)*50;
str += " Twenty-Fifty US Cents Coins: " + to_string((int)(r1/25));
r1= r1-(int)(r1/25)*25;
str += " Ten US Cents Coins: " + to_string((int)(r1/10));
r1 = r1-(int)(r1/10)*10;
str += " Five US Cents Coins: " + to_string((int)(r1/5));
r1= r1-(int)(r1/5)*5;
str += " One US Cents Coins: " + to_string(r1);
r1= 0;
return str;
}
int main(){
double c,p;
string check="y";
cout<<"Enter a value :";
cin>>c;
while(c!=0 && check!="n"){
cout<<"Enter an amount of purchases made against this amount : ";
cin>>p;
if(p>c){
cout<<"purchases cannot exceed cash value "<<endl;
}
else{
cout << calculateChange(p, c);
}
cout<<"Continue(y/n)? ";
cin>>check;
c=c-p;
}
return 0;
}
Now, after creating this file, you can use it in other files too.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.