Is there a way of making an array with an amount depending on how many "words" in a string that a user types?
Say I put 1+2+3+4 it would make an array with 7 spots.
Right now I can only do an equation with 1 number + or - or * or / by another number and that's it but if I make a super huge array, a lot of memory is wasted.
#include <iostream>
int main()
{
std::cout << "Input an equation (e.g. 1+2) using operators +, -, * or /\n"
<< "end input with an == (eg. 1 + 2.3 * 24.5 / 32 ==\n" ;
double result ;
std::cin >> result ; // the first number is the initial result
// evaluate the equation with equal precedence for all operators, and
// associativity from left to right
// ie. a+b*c-d is parsed as ( (a+b) * c ) - d
char operation ;
double number ;
while( std::cin >> operation >> number ) // for each subsequent term (pair of operation,number)
{
switch(operation)
{
case'+' : result += number ; break ;
case'-' : result -= number ; break ;
case'*' : result *= number ; break ;
case'/' : result /= number ; break ;
default :
if( operation != '=' ) std::cout << "invalid operation '" << operation << "'\n" ;
std::cin.clear(std::cin.failbit) ; // put the stream into a failed state to exit from the loop
}
}
std::cout << "result == " << result << '\n' ;
}
@JLBorges Is there a way to display the equation they entered and then the answer instead of "result == "?
Other than that, this is really good and I didn't think about doing it that way!
Thank you for the help, though do you have any constructive feedback?
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::cout << "Input an equation (eg. 1 + 2.3 * 24.5 / 32 ) using operators +, -, * or /\n"
<< "end input with a new line\n" ;
std::string input ;
std::getline( std::cin, input ) ; // read in the complete line
// create a string stream to read from the input entered by the user
std::istringstream str_stm(input) ;
double result ; // // the first number is the initial result
if( !( str_stm >> result ) ) // if input of even the first number failed
{
std::cout << "badly formed input '" << input << "'\n" ;
return 1 ;
}
// the equation parsed so far (initially contains just the first number)
// note: tellg() returns the next input position in the stream
// characters up to tellg() have been parsed and extracted
std::string equation = input.substr( 0, str_stm.tellg() ) ;
// evaluate the equation with equal precedence for all operators, and
// associativity from left to right
// ie. a+b*c-d is parsed as ( (a+b) * c ) - d
char operation ;
double number ;
while( str_stm >> operation >> number ) // for each subsequent term (pair of operation,number)
{
switch(operation)
{
case'+' : result += number ; break ;
case'-' : result -= number ; break ;
case'*' : result *= number ; break ;
case'/' : result /= number ; break ;
default: // invalid operation
std::cout << "input after invalid operation '" << operation << "' discarded\n" ;
goto print_result ; // just print what had been successfully parsed earlier
}
// update the equation to be what has been successfully parsed so far
equation = input.substr( 0, str_stm.tellg() ) ;
}
print_result:
std::cout << equation << " == " << result << '\n' ;
}