PDA

View Full Version : C++ Homework Help



taterSALAD
October 3rd, 2009, 05:23 PM
Can someone please explain to me how the transform() function works?


transform(string.begin(), string.end(), string.begin(), function);I know that you use toupper or tolower as the function, but I don't know what exactly goes in the third item of the syntax, the repeated string.begin

He's what my code currently is:


#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>

using namespace std;

int main()
{
const double TAXRATE = .03;
const double GUNTER_TAXRATE = .0325;

double purchase = 0.0;
string county = "";
double tax = 0.0;

cout << "Purchase price: ";
cin >> purchase;
cin.ignore(100, '\n');
cout << "County: ";
getline(cin, county);

transform(county.begin(), county.end(), county.begin(), toupper);

if ( county == "GUNTER" )
tax = purchase * GUNTER_TAXRATE;
else
tax = purchase * TAXRATE;
// end if

cout << fixed << setprecision(2);
cout << "Tax: " << tax << endl;
// end if

return 0;
}

annihilation
October 3rd, 2009, 05:43 PM
The transform() algorithm applies the function f to some range of elements, storing the result of each application of the function in result.
The first version of the function applies f to each element in [start,end) and assigns the first output of the function to result, the second output to (result+1), etc.
The second version of the transform() works in a similar manner, except that it is given two ranges of elements and calls a binary function on a pair of elements

Kornman00
October 3rd, 2009, 07:13 PM
http://www.cplusplus.com/reference/algorithm/transform/

Limited
October 5th, 2009, 09:05 AM
Wow, I havent even come across tranform function :O

Another good reference is the MDSN website, basically search for the function and it will give you a description, what it does and sometimes an example.

http://msdn.microsoft.com/en-us/visualc/default.aspx