What Other Code Will I Need?

Ok, I've only just started to learn C++ and so far it has been fairly painless.

For my first programe I want to make a simple calculator. I've posted some code below with what I have learnt so far and would like some advise on anything that's missing or would be usefull before I start.

It will be press 'A' to add, 'S' to subtract, 'D' to divide or 'M' to multiply.




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
// test.cpp : 
//

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <cmath>


using namespace std;

int main()
{
	cout << "input 'a' to check or anything else to test" << endl;   // Tells The User What Todo.

	char action;				// start
	cin >> action;

	switch(action)
		{
		case 'a' : cout << "check check check" << endl ;
			break;							// break leaves 
		default : cout << "test test test" << endl ;
			break;
		}						// end


	cout << "hello" << endl; cout << endl;		// Says Hello!
	system("pause"); cout << endl;				/* System Pause // "Press Any Key To Continue */

	cout << "Lets Add Two Numbers Together!" << endl; cout << endl; int number1;
	cout << "Please Enter A Number And Press Enter: "; cin >> number1; cout << endl;		// Asks for number1.
	cout << "You Entered " << number1 << "." << endl; cout << endl;		// Recalls number1.
	cout << "Please Enter Another And Press Enter: "; int number2; cin >> number2; cout << endl; // /*Asks for number2.
	cout << number1 << " + " << number2 << " = " << number1+number2 << endl; /*Adds number1 and number2**/
	cout << endl;

	system("pause");

		return(0);
}
I think you're off to a good start. Here are a couple of recommendations. First, I would add another case statement to check for 'A' in case the user inputs 'a' or 'A'. Second, I wouldn't recommend using the system("pause"); statement. This is operating system specific. I would instead use 2 statements.

cin.ignore();
cin.get();

Finally, I would separate the cout statements for readability. When you learn more about C++, you will want to put each calculator function into a function. In that implementation you would ask the user to enter 2 numbers and a decision string (such as 'A' or 'a' for add, etc), and then write code to pass the numbers to a function.

Revised:

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

 
What Other Code Will I Need?
More Coffee (3) 	Aug 29, 2012 at 6:10am
Ok, I've only just started to learn C++ and so far it has been fairly painless.

For my first programe I want to make a simple calculator. I've posted some code below with what I have learnt so far and would like some advise on anything that's missing or would be usefull before I start.

It will be press 'A' to add, 'S' to subtract, 'D' to divide or 'M' to multiply.

// test.cpp : 
//

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <cmath>


using namespace std;

int main()
{
	cout << "input 'a' to check or anything else to test" << endl;   // Tells The User What Todo.

	char action;				// start
	cin >> action;

	switch(action)
		{
                case 'A':

		case 'a' : 
                cout << "check check check" << endl ;
			break;							// break leaves 

		default : cout << "test test test" << endl ;
			break;
		}						// end


	cout << "hello" << endl << endl;		// Says Hello!
	cin.ignore();
        cin.get(); 
				

	cout << "Lets Add Two Numbers Together!" << endl << endl; 
        int number1;
	cout << "Please Enter A Number And Press Enter: "; 
        cin >> number1; cout << endl;		// Asks for number1.

	cout << "You Entered " << number1 << "." << endl << endl;		// Recalls number1.

	cout << "Please Enter Another And Press Enter: "; 
        int number2; cin >> number2;   
        cout << endl; // /*Asks for number2.

	cout << number1 << " + " << number2 << " = " << number1+number2 << endl; /*Adds number1 and number2**/
	cout << endl;

	cin.ignore();
        cin.get();

		return(0); 
I have a couple of observations.

Space out your code a bit more. You have int declarations on lines 31 and 34 that would be best shown on new lines, along with the succeeding cin statements.

You might also want to add some user input validation. When you ask for the numbers, I could type 'bananas' and your code would go bananas.

How about encompassing the whole thing in a loop so that you can perform more calculations?

Finally, and I don't care if anyone pipes in saying that this is unnecessary, but drop the system call for pausing your program. In fact, my above suggestion of looping your program will solve this anyhow, you shouldn't need it at all.
Topic archived. No new replies allowed.