iterator - How do I use std::transform in c++? -
i learning c++, , have created simple calculator program. pertaining this, have few questions: first, there can clean up, or make better? secondly, in code, use goto
statement return start of program. have heard goto
looked down upon many programmers because can create "spaghetti" code. there not use goto
, rather llop of sort? lastly, coppied line of code internet:
std::transform(returntomain.begin(), returntomain.end(), returntomain.begin(), ::toupper);
can explain me does? understand transform
,
::toupper
but not .begin()
, .end()
here full code, , thank help:
#include <iostream> #include <stdlib.h> #include <string> #include <cmath> #include <algorithm> using namespace std; void showmenu(); unsigned long facinput; //prototyping functions int getinput(); float additiongetinput(); float subtractiongetinput(); float multiplicationgetinput(); float divisiongetinput(); unsigned long long factorialinput(unsigned long long facinput); float hypotenusefinder(); class prompt //class don't have repeatedly type prompt. { public: prompt() { inputpromptone = "\nenter float one: "; inputprompttwo = "\nenter float two: "; } string inputpromptone; string inputprompttwo; }; string returntomain; prompt prompt; int main() { startofprogram: //label goto function. system("cls"); showmenu(); int userinput = getinput(); switch(userinput) { case 1: //addition system("cls"); cout << "you chose addition calculator." << endl; cout << "the sum is: " << additiongetinput() << endl; cout << "\n\nenter 'quit' return menu: "; cin >> returntomain; transform(returntomain.begin(), returntomain.end(), returntomain.begin(), ::toupper); if(returntomain == "quit") { goto startofprogram; } break; case 2: //subtraction system("cls"); cout << "you chose subtraction calculator." << endl; cout << "the difference is: " << subtractiongetinput() << endl; cout << "\n\nenter 'quit' return menu: "; cin >> returntomain; transform(returntomain.begin(), returntomain.end(), returntomain.begin(), ::toupper); if(returntomain == "quit") { goto startofprogram; } break; case 3: //multiplication system("cls"); cout << "you chose multiplication calculator." << endl; cout << "the product is: " << multiplicationgetinput() << endl; cout << "\n\nenter 'quit' return menu: "; cin >> returntomain; transform(returntomain.begin(), returntomain.end(), returntomain.begin(), ::toupper); if(returntomain == "quit") { goto startofprogram; } break; case 4: //division system("cls"); cout << "you chose division calculator." << endl; cout << "the quotient is: " << divisiongetinput() << endl; cout << "\n\nenter 'quit' return menu: "; cin >> returntomain; transform(returntomain.begin(), returntomain.end(), returntomain.begin(), ::toupper); if(returntomain == "quit") { goto startofprogram; } break; case 5: system("cls"); //factorial finder cout << "you chose factorial calculator." << endl; cout << "enter number (max 65): "; cin >> facinput; cout << "the factorial is: " << factorialinput(facinput) << endl; cout << "\n\nenter 'quit' return menu: "; cin >> returntomain; transform(returntomain.begin(), returntomain.end(), returntomain.begin(), ::toupper); if(returntomain == "quit") { goto startofprogram; } break; case 6: //pythagorean theorem system("cls"); cout << "you chose pythagorean theorem calculator." << endl; cout << "length of side c (hypotenuse): " << hypotenusefinder() << endl; cout << "\n\nenter 'quit' return menu: "; cin >> returntomain; transform(returntomain.begin(), returntomain.end(), returntomain.begin(), ::toupper); if(returntomain == "quit") { goto startofprogram; } break; default: cout << "that invalid function. try again." << endl; goto startofprogram; } } void showmenu() { cout << "1-addition" << endl; cout << "2-subtraction" << endl; cout << "3-multiplication" << endl; cout << "4-division" << endl; cout << "5-factorial" << endl; cout << "6-pythagorean theorem" << endl; cout << "---------------------" << endl; } int getinput() { int userinput; cout << "choose function: "; cin >> userinput; return userinput; } float additiongetinput() { float addinput1; float addinput2; cout << prompt.inputpromptone; cin >> addinput1; cout << prompt.inputprompttwo; cin >> addinput2; system("cls"); float sum = addinput1 + addinput2; return sum; } float subtractiongetinput() { float subinput1; float subinput2; cout << prompt.inputpromptone; cin >> subinput1; cout << prompt.inputprompttwo; cin >> subinput2; system("cls"); float difference = subinput1 - subinput2; return difference; } float multiplicationgetinput() { float mulinput1; float mulinput2; cout << prompt.inputpromptone; cin >> mulinput1; cout << prompt.inputprompttwo; cin >> mulinput2; system("cls"); float product = mulinput1 * mulinput2; return product; } float divisiongetinput() { float divinput1; float divinput2; cout << prompt.inputpromptone; cin >> divinput1; cout << prompt.inputprompttwo; cin >> divinput2; system("cls"); float quotient = divinput1 / divinput2; return quotient; } unsigned long long factorialinput(unsigned long long facinput) { if(facinput==1) {cout << "---------------------" << endl; return 1; } else { return facinput*factorialinput(facinput-1); } } float hypotenusefinder() { float a; float b; cout << "enter length of side a: "; cin >> a; cout << "\nenter length of side b: "; cin >> b; float hypotenuse = sqrt(pow(a, 2) + pow(b, 2)); return hypotenuse; }
the begin
, end
methods c++ stl iterator api.
the example copied can more legibly (and less optimally) expressed lambdas (running example on ideone):
std::string input = "yo dogg"; std::string output(input); std::transform(input.begin(), input.end(), output.begin(), [](auto character) { return ::toupper(character); }); /// 'output' contain "yo dogg"
… functions in <algorithm>
library use c++ iterators.
Comments
Post a Comment