Answer the question
In order to leave comments, you need to log in
Calculator in C++ or how to solve the problem with actions?
Hello dear!
I started learning functions .. and immediately decided to make a calculator using functions ..
Problem: when I made a choice, for example, 16 / 4 gives out which is 20.
If you have free time, help explain what the problem is
#include <iostream>
using namespace std;
void title() {
cout << "КАЛЬКУЛЯТОР\tby Chorrny Edition" << endl;
}
int Function1(int a, int b) {
return a + b;
}
int Function2(int a, int b) {
return a - b;
}
int Function3(int a, int b) {
return a * b;
}
int Function4(int a, int b) {
return a / b;
}
int main()
{
setlocale(LC_ALL, "");
int res, a, b;
int choise = 0;
title();
cout << "Выберите действие:\n\t 1 - добавления:\n\t 2 - вычитание:\n\t 3 - умножение:\n\t 4 - Деление:" << endl;
cin >> a;
cout << "Первая цифра -->";
cin >> b;
cout << "Вторая цифра -->";
cin >> choise;
if (choise == 1) {
res = Function1(a, b);
}
else if (choise == 1) {
res = Function2(a, b);
}
else if (choise == 1) {
res = Function3(a, b);
}
else if (choise == 1) {
res = Function4(a, b);
}
cout << res << "Результат: ";
system("pause");
return 0;
}
Thank you!
Answer the question
In order to leave comments, you need to log in
You are always folding, regardless of the user's choice.
Change the if block to this one:
if (choise == 1) {
res = Function1(a, b);
}
else if (choise == 2) {
res = Function2(a, b);
}
else if (choise == 3) {
res = Function3(a, b);
}
else if (choise == 4) {
res = Function4(a, b);
}
#include
using namespace std;
void title() {
cout << "CALCULATOR\tby Chorrny Edition" << endl;
}
int Function1(int a, int b) {
return a + b;
}
int Function2(int a, int b) {
return a - b;
}
int Function3(int a, int b) {
return a * b;
}
int Function4(int a, int b) {
return a / b;
}
int main()
{
setlocale(LC_ALL, "");
int res, a, b;
int choice = 0;
title();
cout << "Select action:\n\t 1 - add:\n\t 2 - subtract:\n\t 3 - multiply:\n\t 4 - divide:" << endl;
cin >> choose;
cout << "First digit -->";
cin >> a;
cout << "Second digit -->";
cin >> b;
if (choise == 1)
{
res = Function1(a, b);
}
else if (choise == 2)
{
res = Function2(a, b);
}
else if (choise == 3) {
res = Function3(a, b);
}
else if (choise == 4) {
res = Function4(a, b);
}
cout << "Result: "<< res < system("pause");
return 0;
}
using channing Method
#include
using namespace std;
/**
* @brief Method chaining
* @tparam T
*/
template
class Mathem {
public:
Mathem(T xx):m_value(xx){};
Mathem& add(T value){m_value+=value; return *this; }
Mathem& sub(T value){m_value-=value; return *this; }
Mathem& multiply(T value){m_value*=value; return *this; }
int getValue(){return m_value;}
operator T(){return m_value;}
private:
int m_value;
};
int main(){
Mathem operation(2);
operation.add(2).sub(3).multiply(5);
cout<
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question