Console Calculator

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;


// Calculator Section
float num1;
float num2;

float addition(float num1, float num2){
      float equals1;
      equals1 = num1 + num2;
      return (equals1);
      }
float subtraction(float num1, float num2){
      float equals1;
      equals1 = num1 - num2;
      return (equals1);
      }
float multiplication(float num1, float num2){
      float equals1;
      equals1 = num1 * num2;
      return (equals1);
      }
float division(float num1, float num2){
      float equals1;
      equals1 = num1 / num2;
      return (equals1);
      }
float power(float num1, float num2){
      float equals1;
      equals1 = pow(num1, num2);
      return (equals1);
      }
float square(float num1){
      float equals1;
      equals1 = sqrt(num1);
      return (equals1);
      }

void calculator(void)
    {
                     char again1;
    do{
    char mathfunc1;
    //Gather problem
    system("CLS");
    cout << "Enter the first number in the problem." << endl;
    cin >> num1;
    system("CLS");
    cout << "Enter the mathematical function you want to use (+,-,*,/,^,#)." << endl;
    cin >> mathfunc1;
    system("CLS");
    if(mathfunc1 != '#'){
    cout << "Enter the second number in the problem." << endl;
    cin >> num2;
    system("CLS");
}
    // Answer Problem
    switch(mathfunc1)
    {
    case '+':
         cout << "The answer to your math problem is: " << addition(num1, num2) << endl;
         break;
    case '-':
         cout << "The answer to your math problem is: " << subtraction(num1, num2) << endl;
         break;
    case '*':
         cout << "The answer to your math problem is: " << multiplication(num1, num2) << endl;
         break;
    case '/':
         cout << "The answer to your math problem is: " << division(num1, num2) << endl;
         break;
    case '^':
         cout << "The answer to your math problem is: " << power(num1, num2) << endl;
         break;
    case '#':
         cout << "The answer to your math problem is: " << square(num1) << endl;
         break;
         default:
         cout << "The information you input was invalid." << endl;
         break;
    }
    cout << "Do you want to do another math problem?" << endl;
    cin >> again1;
}while(again1 == 'y' || again1 == 'Y');
}
// End Calculator

// Test Section
void test(void)
{
     int amountquestions1 = 0;
     char amountquestions2;
     do{
     cout << "How many questions do you want in your test, maximum of 25?" << endl;
     cin >> amountquestions1;
     cout << "Are you sure you want " << amountquestions1 << "?" << endl;
     cin >> amountquestions2;
     }while(amountquestions2 != 'y' || amountquestions2 != 'Y' );
     }





// End Test Section

int main(int argc, char *argv[])
{
    system("TITLE Calculator");
    system("COLOR f2");
    string testvcalc1;
    
    cout << "Do you want to use the calculator or make a test?" << endl;
    cin >> testvcalc1;
    if(testvcalc1 = 'calculator'){
                  cout << calculator;
                  }
    else if(testvcalc1 = 'test'){
         cout << test;
         }

    cin.ignore();
    return EXIT_SUCCESS;
}



The calculator section itself works wonderful, but I wanted the user to be able to make a test for its friend or as a quiz to help them study, I haven't gone deep into the quiz section just started, and I realized I got too deep before debugging. I just have about 2 errors now, help me?

All comments are appreciated, open to ideas and criticizes. Let it all out.
It's probably insignificant, but why make a return variable? You can do return x-y; for example. return pow(x, y); would work too.

That and using system() are my only criticisms. I'm sure there are better ways to change the title, and whatever the other command does.
To Clear the screen
http://www.cplusplus.com/forum/articles/10515/

Title, Colors, etc.
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
#include <iostream>
using namespace std;

#include <windows.h>

int main()
  {
  HANDLE hConIn  = GetStdHandle( STD_INPUT_HANDLE );
  HANDLE hConOut = GetStdHandle( STD_OUTPUT_HANDLE );

  // Check to make sure your program is actually being used by a human (at a console)
  DWORD foo;
  if (!GetConsoleMode( hConIn, &foo ))
    {
    // (complaints go here)
    return 1;
    }

  // Get the current console state
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo( hConOut, &csbi );

  // This sets the console title
  SetConsoleTitleA( "Calculator" );

  // This changes the console text color
  SetConsoleTextAttribute( hConOut, 0xF2 );

  // Here's how to PAUSE properly
  cout << "Press any key to quit..." << flush;
  FlushConsoleInputBuffer( hConIn );
  WaitForSingleObject( hConIn, INFINITE );
  FlushConsoleInputBuffer( hConIn );

  // And how to restore the console color to what the user had it before running your program
  SetConsoleTextAttribute( hConOut, csbi.wAttributes );

  return 0;
  }

Your program should not rely upon the presence of a human. I recommend you check at the beginning. Only if there is a human present should you bother to play with the title, colors, pause, etc. The rest of the program should work the same either way.

Hope this helps.
Last edited on
To chrisname:
The CLS is clear screen, what other command should I use to change the title and color?

To Duoas:
Is the link just to clear the screen? Do you know EXACTLY what every character in teh code you proposed does? If you don't, then I won't have a clue, I'm not comfortable with using code I don't know. If the link is to the source of where you learned the code, I'll use it.
Yes, I personally wrote everything you see in my posts. The commentary and dialogue are particularly explicit and should make easy reading. Beyond that, simply google "msdn foo" for each function you want to learn more about. The Windows Console API is very simple and easy to use.

BTW, the article I linked is a "how-to" article.
Topic archived. No new replies allowed.