C++ Calculator With Unlimited Inputs -
hi beginner c++ language , know if there way of writing program unlimited inputs. example: want write calculator program add numbers. that's easy enough there way user can add many numbers wants without being asked how many numbers wants. such if wants add 3 numbers can type "1+1+1" or if wants add 4 numbers adds "+1" end of previous line.like user not stuck fixed number of inputs or doesn't need asked how many inputs wants. functions in c++ need know in order
you can use while loop read standard input. (std::cin) basic code read while loop, , add input sum follows:
#include <iostream> #include <string> #include <cstdlib> int main(){ std::string line = ""; double sum = 0.0; while(line != "end"){ std::cout<<"the current sum is: "<<sum<<std::endl; std::cout<<"enter number add or \"end\" exit: "; std::cin>>line; sum += atof(line.c_str()); } std::cout<<"the final sum is: "<<sum<<std::endl; }
this read numbers until receives input "end".
Comments
Post a Comment