Beginner ---- My first program (all by myself).

#include <iostream>
using namespace std;


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
int main ()
{
	int x, y, choice;
	char exit;

	cout << "CALCULATOR 1.00 \n \n";
	
	//DECLARE LOOP//
	while (1)
	{

  	cout << "1. Add\n";
	cout << "2. Substract\n";
	cout << "3. Multiply\n";
	cout << "4. Divide\n \n";
	
	cout << "Choose an option \n";
	cin >> choice;
	

	//GET NUMBERS//
	cout << "Enter first positive integer: ";
	while (1)
	{
		cin >> x; 
		if ((x > 0))
		{
			break;
		}
		else 
		{
			cout << "integer must be positive \n";
			cout << "Enter first positive integer: ";
		}	
	}
	
	cout << "Enter second positive integer: ";
    
	while (1)
	{
		cin >> y; 
		if ((y > 0))
		{
			break;
		}
		else 
		{
			cout << "integer must be positive \n";
			cout << "Enter second positive integer: ";
		}	
	}
	//DONE GETTING NUMBERS//
	
	//GET CHOICE//
	switch (choice)
	{
	case 1:
		
		cout << "The result is " << x + y <<"\n";
		break;

	case 2 : 
		cout << "The result is " << x - y <<"\n";
		break;

	case 3 :
		cout << "The result is " << x * y <<"\n";
		break;

	case 4: 
		cout << "The result is " << x / y <<"\n";
		break;
	}


	//EXIT?//
	cout <<"exit (y/n) : ";
	cin >> exit;

	if ((exit == 'y' || exit == 'Y'))
	{
		break;
	}
	else 
	{
		cout << "\n";
	}	
	}

  return 0;
}
Last edited on
Could you please use the code tags?

The calculator isn't really user friendly. Then again, you are new, so I'll cut you some slack.

You could give the while loop a condition so you don't have to add a conditional statement inside the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "Enter first positive integer: ";
cin >> x; 
while (x < 0)
{
    cout << "integer must be positive \n";
    cout << "Enter first positive integer: ";
    cin >> x; 
}

cout << "Enter second positive integer: ";
cin >> y; 
while (y < 0)
{
    cout << "integer must be positive \n";
    cout << "Enter second positive integer: "; 
    cin >> y; 
}


If you want a challenge then try to make a the calculator accept input in this format: 4 + 4 / 5 * 15
Last edited on
Sorry about the code tags. Anyway, thanks for taking time to review the code.
Im just messing aroud but I find programming very interesting
But on the UP side - there are no goto or system(something)
so I give him/her some positive points.
But on the UP side - there are no goto or system(something)
so I give him/her some positive points.


Yes, but if there is something to complain about then it's using namespace std and how he over-complicated the program.
if i want the calculator accept that input format can i do it using only <iostream>?
Last edited on
Yes, of course.
ok i really have no idea how to do that can you give me a hint?
The stream object 'cin' can accept input for multiple variables. That's really all you need to know for something like this.

std::cin >> a >> b;
Topic archived. No new replies allowed.