0

This a simple calculator program. It designed to take 3 inputs from user which are one double type number, one character and another double type number. But when it compiles and runs, it shows Enter the value 2 first instead of showing the value 1 first despite of passing the value1 parameter first in the calculateResult function in the main function. Why does it happens either? The following program doesn't show any compile error.

#include <iostream>
#include <conio.h>

double getValue1() //first value
{
std::cout << "Enter value 1: ";
double input1{ 0 };
std::cin >> input1;

return input1;
}

double getValue2() //second value
{
std::cout << "Enter value 2: ";
double input2{ 0 };
std::cin >> input2;

return input2;
}

char getMathematicalOperation()
{
std::cout << "Enter a valid symbol such as'+', or '-', or '/', or '*'\n";
char symbol{0};
std::cin >> symbol;

return symbol;
}


void calculateResult(double value_1, char symbol, double value_2)
{
if ( symbol == '+') {
    std::cout << value_1 + value_2;
}
else if (symbol == '-') {
    std::cout << value_1 - value_2;
}
else if (symbol == '*') {
    std::cout << value_1 * value_2;
}
else if (symbol == '/') {
    std::cout << value_1 / value_2;
}
else {
    std::cout << "Something wrong went with your operation\n";
}
}

int main(){

calculateResult(getValue1(), getMathematicalOperation(), getValue2());

getch();
}
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Misbah
  • 1
  • 1
  • It looks like you pasted your program twice - I have fixed that. Also please note that you need a blank line between your question and your code for the formatting to work. – Karl Knechtel Oct 14 '20 at 05:03
  • order of parameter evaluation is unspecified – sp2danny Oct 14 '20 at 05:03
  • `calculateResult(getValue1(), getMathematicalOperation(), getValue2());` It seems that you expect that `getValue1` must be called before `getValue2` is called, simply because that is the order in which they appear between the parentheses. C++ offers no such guarantee. – Karl Knechtel Oct 14 '20 at 05:04
  • Stop. Hammer Time! – paddy Oct 14 '20 at 05:05
  • Try to save them in a var before calling the method. first = getValue1() , second = getValue2(). – Alex Rancea Oct 14 '20 at 05:06
  • `calculateResult(getValue1(), getMathematicalOperation(), getValue2());` -- Function parameters do not work like FIFO queues. What if the compiler decides to call `getValue2()` first, or `getMathematicalOperation()` first? – PaulMcKenzie Oct 14 '20 at 05:08

0 Answers0