Program

I have the following program for a simple programme written using visual studio.

------------------

Q))))))) and how can i display the final sum when user enters the equal sign?




----------------------------------------------------------------------------

#include <iostream>

using namespace std; // so we dont have to use std anymore

int main () // start of function

{
char another;
char op; // operator
double num1; // first number
double num2; // second number

Start:

system ("cls") ;

cout<< "welocome to my program\n"; // welcome screen
cout << "enter your first number\n"; // user enters first number
cin >> num1; // input first number
cout << "thank you\n"; // confirm users first number
cout << "now enter an operator ( +, -, *, or / ) \n"; // user enters operator
cin >> op; // inout operator
cout << "thank you\n"; // confirm users operator



// begin switch
switch ( op )

{ case '+' : cout<< "your answer is " << num1 + num2 << endl;
break; // addition



default: break; // anything else
}


Last edited on
By no means a perfect solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

double total = 0.0;

double do_calc(char o, double a, double b)
{
  switch(o)
  {
    case '+':
    {
      return (a+b);
    } break;
    case '-':
    {
      return (a-b);
    } break;
    case '*':
    {
      return (a*b);
    } break;
    case '/':
    {
      if(b == 0.0)
      {
        cout << "Cannot divide by zero!" << endl;
        return total; // don't want division by zero, just ignore sequence and return original total
      }
      return (a/b);
    } break;
  }
  return total;
}

int main()
{
  char op = 0;
  double num = 0.0;

  cout << "Welcome to my calculator" << endl;
  cout << "Please enter your number: ";
  cin >> total;
  while(op != '=')
  {
    cout << endl << "Please choose your operator ( +, -, *, /, = ): ";
    cin >> op;
    if(op == '=') break;
    cout << "Please enter your number: ";
    cin >> num;
    total = do_calc(op,total,num);
    cout << "The total is: " << total << endl;
  }
  cout << "The final answer is: " << total << endl;
  return 0;
}


Last edited on
Topic archived. No new replies allowed.