im still lost with this question

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
I would use the "do you want to continue" question as the sentinel for a while-loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

bool bContinuing = true;

char  answer = '';

while( bContinuing )
    {
    /*   Get the user's input:   */

    /*  Ask the user if he/she wants to continue:    */

    cin >> answer;

    bContinuing = ( answer != 'n' );

    }



NOTE: This hasn't been tested, and it doesn't look for upper-case answers.
closed account (48T7M4Gy)
Hey gregory, let's see your code. So far your posts seem to be "can you do my homework for me". Prove me wrong and show us what you have so far.
hi kemort i will send you my program after work
closed account (48T7M4Gy)
OK gregory, we're happy to help where we can especially where people are prepared to give it a go. :)
hi kemort was a bit busy with work. this is what i came up with the thing is when its 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;
}
}
You don't have a loop plus a few other problems.

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

int main()

{


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

    do
    {
        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;

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

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

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

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

    }while (yesno == 'Y');


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

codewriter,

damn thank you very much. this c++ is busting my brains i see where i made all the mistakes
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.