how do i go about this

how do i go about this question in regards to for and while loops?


the user to enter two numbers and the operation to be performed , and then we display the answer . If the user type ‘A’ or ‘a’ that’s addition If the user type ‘S’ or ‘s’ that’s subtraction If the user type ‘M’ or ‘m’ that’s multiplication If the user type ‘D’ or ‘d’ that’s division

An Example of running : Enter first number : 5 Enter second number : -6 Enter the operation to be performed: M The Answer is -30

2. Modify the previous program so that it avoid dividing by zero, if the second number is a zero it will show , Division by zero is impossible

3. Modify your program so it will use repetition structure and ask the user to enter the operations and numbers repeatedly and stop when the user enter x for the operation.

The program must ask user do you want to continue Y or N . If user enter n the program must stop





Which part? the whole thing?... theres a bunch of ways to go about something like this.

i.e.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
          cout << "please enter the first value you would like to divide by:
cin >> input1;
cout << "please enter the second value you would like to divide by";
cin >> input 2;

while (input2 == 0)
                {
                   cout << "Sorry, you cannot divide by (0)";
                   cout << "please enter the first value you would like to divide by:
cin >> input1;
cout << "please enter the second value you would like to divide by";
cin >> input 2;
}












you can use switch cases, functions, and even classes and objects its all how you would like to approach the problem.
Step one and two of your question require a control statements like IF, IF...ELSE, etc and the third step requires you to use an iteration or looping statements like WHILE, DO...WHILE and FOR loops.
There are a number of ways to do it. You should look at it in its most basic form and what you are going to need.

You absolutely need 2 variables to hold the two integers ( or whatever type) you are going to apply your arithmetic operation.

int a;
int b;

You have to parse ( decipher, translate, understand) what the user wants you to do.
There are many ways to do this. If/else chains would work. So would a switch.

You have a deal breaker in division, so you absolutely need a condition check in division to make sure zero is not the divisor.

After that the program pretty much writes itself.
how do i go about writing this program though im still lost
the whole thing
Give it a try and I'll chime in.
this is what i came up with so far but when calculating the answers are wrong

------------------------------
#include <iostream>
#include <cmath>

int main()

{
using namespace std;

int firstnumber, secondnumber;
int Total,multiply,divide;
int M = firstnumber * secondnumber;
int D = firstnumber / secondnumber;
char maths = ' ';
char yesno;

cout<<"Please enter M for Multiplication or D for Division and press Enter. ";
cin >> maths ;
maths = toupper(maths);
cout << endl << maths << endl;

cout << "Please enter the first number: ";
cin >> firstnumber;

cout << "Please enter the second number : ";
cin >> secondnumber;

if(maths == 'M') {

cout << "the multiplication of the two numbers is:" <<M << endl;
}
else if (maths == 'D') {
cout << "the division of the two numbers is:" <<D << endl;

while (secondnumber == 0)
{
cout << "Sorry, you cannot divide by (0)";

cin >> firstnumber;
cout << "please enter the second number you would like to divide by: ";
cin >> secondnumber;
}


cout<<"Do you want to continue enter Y or N to stop: ";
cin>>yesno;
yesno = tolower(yesno);


system ("pause");
return 0;
}
}
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
#include <iostream>
#include <cmath>

int main()

{
using namespace std;

int firstnumber, secondnumber;
int Total,multiply,divide;
int M = firstnumber * secondnumber;
int D = firstnumber / secondnumber;// you CANT do this without checking for zero
                                                         // It can crash your program
char maths = ' ';
char yesno;

cout<<"Please enter M for Multiplication or D for Division and press Enter. ";
cin >> maths ;
maths = toupper(maths);
cout << endl << maths << endl;

cout << "Please enter the first number: ";
cin >> firstnumber;

cout << "Please enter the second number : ";
cin >> secondnumber;

if(maths == 'M') {

cout << "the multiplication of the two numbers is:" <<M << endl;
}
else if (maths == 'D') {
cout << "the division of the two numbers is:" <<D << endl;

while (secondnumber == 0)
{
cout << "Sorry, you cannot divide by (0)";

cin >> firstnumber;
cout << "please enter the second number you would like to divide by: ";
cin >> secondnumber;
}


cout<<"Do you want to continue enter Y or N to stop: ";
cin>>yesno;
yesno = tolower(yesno);


system ("pause");
return 0;
}
}


On line 12 you do this division BEFORE you check for a zero divisor. Don't because if the divisor is zero it will crash your program.
Last edited on
This is what it should be:

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
#include <iostream>
// #include <cmath> // not needed
using namespace std;

int main()

{
	

	int firstnumber, secondnumber;
	int Total, multiply, divide;
	int M;
	int D;
	char maths;
	char yesno = 'y';
	do{
		cout << "Please enter M for Multiplication or D for Division and press Enter. ";
		cin >> maths;
		maths = toupper(maths);
		//cout << endl << maths << endl;         //not needed

		cout << "Please enter the first number: ";
		cin >> firstnumber;

		cout << "Please enter the second number : ";
		cin >> secondnumber;

		M = firstnumber * secondnumber;

		if (maths == 'M') {

			cout << "the multiplication of the two numbers is:" << M << endl;
		}
		else if (maths == 'D') {
			if (secondnumber == 0)
				cout << "Division by zero is undefined\n";

			else cout << "the division of the two numbers is :"
				    << firstnumber / secondnumber << "\n";
		}

		cout << "\nDo you want to continue enter Y or N to stop: ";
		cin >> yesno;
		yesno = tolower(yesno);
		} while (yesno == 'y');


	// system("pause"); not needed
	return 0;
	
}
Last edited on
And you have unused variables up there, but thats not fatal. See if you can find the ones you can remove.
Last edited on
Topic archived. No new replies allowed.