a string or function that includes multiple variables (c++) -
i've searched multiple places , watched through entire "thenewboston" c++ guide on youtube, , yet find answer this.
i'm writing program practice c++ skills, i'm still noob think possible don't know how it.
the program i'm trying make 1 records how many reps excercise when training in gym.
#include <iostream> #include <cstdlib> #include <string> using namespace std; int main() { string biceps = "biceps"; string triceps = "triceps"; string quads = "quads"; string musclechoice; cout << "welcome mypt!" << endl; cout << endl; cout << "what going training today?" << endl; cout << endl; cout << "**biceps** \n\n**triceps** \n\n**quads**" << endl; cout << endl; cin >> musclechoice; if ((musclechoice == "biceps") || (musclechoice == "triceps") || (musclechoice == "quads")) { cout << "working" << endl; } return 0; }
that's want, , works. if have more possibilities, messy. want looks this. (obviously function wouldn't long, tried explain better)
cin >> musclechoice; if (musclechoice == onefunctionthatincludesallthosestrings) { cout << "working" }
does know how that?
(please explain answer might not understand, i'm noob)
you use std::set
of exercise strings, , check input string member of set:
std::set<std::string> exercises{"biceps", "triceps", "quads"};
then
if (exercises.find(musclechoice) != exercises.end()) { std::cout << "working"; }
if want keep tally of reps, use std::map
instead:
std::map<std::string, unsigned int> exercises; if (exercises.find(musclechoice) != exercises.end()) { std::cout << "working"; exercises[musclechoice]++; // increase count muscle choice }
Comments
Post a Comment