Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
edited body
Source Link
Sela12
  • 181
  • 1
  • 13

A week ago I did a three function calculator, I took the input from you guys and made it better. Overall it works fine, I didn't find any problems. But as a beginning programer I'd like some imputinput on this calculator.

A week ago I did a three function calculator, I took the input from you guys and made it better. Overall it works fine, I didn't find any problems. But as a beginning programer I'd like some imput on this calculator.

A week ago I did a three function calculator, I took the input from you guys and made it better. Overall it works fine, I didn't find any problems. But as a beginning programer I'd like some input on this calculator.

small fixes to code
Source Link
Sela12
  • 181
  • 1
  • 13
// Three function Calculator.cpp : main project file.
#include "stdafx.h"
#include<iostream>
using namespacestd::cout;
using std;std::cin;
long double action(long double num1, long double num2, char act) //External function to do all the calculations
{
 long double total;
 switch (act)
 {
 case '+':
 total = num1 + num2;
 break;
 case '-':
 total = num1 - num2;
 break;
 case '*':
 total = num1*num2;
 break;
 case '/':
 total = num1 / num2;
 break;
 default:
 cout << "Input not recognized\n";
 }
 return total; //Giving the main function a number to work with.
}
int main()
{
 cout << "Enter action as # to exit program" << endl;program\n";
 cout << "Possible actions:+,-,*,/\n" << endl;\n\n";
 long double num1, num2, num3, total;
 char act1, act2;
 while (1) //inf loop
 {
 beginning: //label, to reset the program in case of division by zero
 cin >> num1 >> act1 >> num2 >> act2 >> num3;
 if (act1 == '#' || act2 == '#') //user wants to exit program
 {
 system("pause");
 return 0;
 }
 else if (act1 == '/' && num2 == 0 || act2 == '/' && num3 == 0) //division by zero happens
 {
 cout << "You can't divide by zero" << endl;zero\n";
 cout << "\n\n";
 goto beginning; //restarting the program from the lable, to the beginning of the 'while' loop
 }
 else if ((act2 == '*' || act2 == '/') && (act1 == '-' || act1 == '+')) //action2 happens before action1 because of order of operation
 {
 total = action(num2, num3, act2); //sending the function reciving the total
 total = action(num1, total, act1); //sending the total of previos function to get overall total
 }
 else //action1 happens before action2 because of order of operation
 {
 total = action(num1, num2, act1); //sending the function reciving the total
 total = action(total, num3, act2); //sending the total of previos function to get overall total
 }
 cout << num1 << act1 << num2 << act2 << num3 << "=" << total; //printing result of overall function
 cout << "\n\n"; //new line before restarting program
 }
}
// Three function Calculator.cpp : main project file.
#include "stdafx.h"
#include<iostream>
using namespace std;
long double action(long double num1, long double num2, char act) //External function to do all the calculations
{
 long double total;
 switch (act)
 {
 case '+':
 total = num1 + num2;
 break;
 case '-':
 total = num1 - num2;
 break;
 case '*':
 total = num1*num2;
 break;
 case '/':
 total = num1 / num2;
 break;
 default:
 cout << "Input not recognized\n";
 }
 return total; //Giving the main function a number to work with.
}
int main()
{
 cout << "Enter action as # to exit program" << endl;
 cout << "Possible actions:+,-,*,/\n" << endl;
 long double num1, num2, num3, total;
 char act1, act2;
 while (1) //inf loop
 {
 beginning: //label, to reset the program in case of division by zero
 cin >> num1 >> act1 >> num2 >> act2 >> num3;
 if (act1 == '#' || act2 == '#') //user wants to exit program
 {
 system("pause");
 return 0;
 }
 else if (act1 == '/' && num2 == 0 || act2 == '/' && num3 == 0) //division by zero happens
 {
 cout << "You can't divide by zero" << endl;
 cout << "\n\n";
 goto beginning; //restarting the program from the lable, to the beginning of the 'while' loop
 }
 else if ((act2 == '*' || act2 == '/') && (act1 == '-' || act1 == '+')) //action2 happens before action1 because of order of operation
 {
 total = action(num2, num3, act2); //sending the function reciving the total
 total = action(num1, total, act1); //sending the total of previos function to get overall total
 }
 else //action1 happens before action2 because of order of operation
 {
 total = action(num1, num2, act1); //sending the function reciving the total
 total = action(total, num3, act2); //sending the total of previos function to get overall total
 }
 cout << num1 << act1 << num2 << act2 << num3 << "=" << total; //printing result of overall function
 cout << "\n\n"; //new line before restarting program
 }
}
// Three function Calculator.cpp : main project file.
#include "stdafx.h"
#include<iostream>
using std::cout;
using std::cin;
long double action(long double num1, long double num2, char act) //External function to do all the calculations
{
 long double total;
 switch (act)
 {
 case '+':
 total = num1 + num2;
 break;
 case '-':
 total = num1 - num2;
 break;
 case '*':
 total = num1*num2;
 break;
 case '/':
 total = num1 / num2;
 break;
 default:
 cout << "Input not recognized\n";
 }
 return total; //Giving the main function a number to work with.
}
int main()
{
 cout << "Enter action as # to exit program\n";
 cout << "Possible actions:+,-,*,/\n\n";
 long double num1, num2, num3, total;
 char act1, act2;
 while (1) //inf loop
 {
 beginning: //label, to reset the program in case of division by zero
 cin >> num1 >> act1 >> num2 >> act2 >> num3;
 if (act1 == '#' || act2 == '#') //user wants to exit program
 {
 system("pause");
 return 0;
 }
 else if (act1 == '/' && num2 == 0 || act2 == '/' && num3 == 0) //division by zero happens
 {
 cout << "You can't divide by zero\n";
 cout << "\n\n";
 goto beginning; //restarting the program from the lable, to the beginning of the 'while' loop
 }
 else if ((act2 == '*' || act2 == '/') && (act1 == '-' || act1 == '+')) //action2 happens before action1 because of order of operation
 {
 total = action(num2, num3, act2); //sending the function reciving the total
 total = action(num1, total, act1); //sending the total of previos function to get overall total
 }
 else //action1 happens before action2 because of order of operation
 {
 total = action(num1, num2, act1); //sending the function reciving the total
 total = action(total, num3, act2); //sending the total of previos function to get overall total
 }
 cout << num1 << act1 << num2 << act2 << num3 << "=" << total; //printing result of overall function
 cout << "\n\n"; //new line before restarting program
 }
}
edited body
Source Link
Sela12
  • 181
  • 1
  • 13
Loading
edited tags
Link
Sela12
  • 181
  • 1
  • 13
Loading
added 14 characters in body
Source Link
Sela12
  • 181
  • 1
  • 13
Loading
Better notes
Source Link
Sela12
  • 181
  • 1
  • 13
Loading
added 24 characters in body
Source Link
Sela12
  • 181
  • 1
  • 13
Loading
Source Link
Sela12
  • 181
  • 1
  • 13
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /