Loops
Write your question here.
Ive been working on this code but it wont work so ill need help.im supposed to changed my code using loops. this is my code.
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
|
Put the code you need help with here.
#include <iostream>
using namespace std;
int main()
{
// declare variables
int firstInteger= 0;
int secondInteger = 0;
int answer = 0;
char operation = ' ';
char DoItAgain = ' Y';
while ( DoItAgain == 'Y')
{
//enter the operation
cout << "Enter A (addition), S (subtraction), M (multiplication), D (division): ";
cin >> operation;
operation = toupper (operation);
//determine whether the operation is valid
if (operation != 'A' && operation != 'S' && operation != 'M' && operation != 'D')
{
answer = -1;
cout << "Invalid operation: " << endl;
}
else
{
//enter the integers
cout << "First integer: ";
cin >> firstInteger;
cout << "Second integer: ";
cin >> secondInteger;
//calculate the answer
if (operation == 'A')
answer = firstInteger + secondInteger;
else
if (operation == 'S')
if (firstInteger > secondInteger)
answer = firstInteger - secondInteger;
else
if (secondInteger > firstInteger)
answer = secondInteger - firstInteger;
else
if (operation == 'M')
answer = firstInteger * secondInteger;
else
if (operation == 'D')
if (firstInteger > secondInteger)
answer = firstInteger / secondInteger;
else
if (secondInteger > firstInteger)
answer = secondInteger / firstInteger;
//end if
//end if
//end if
//end if
//end if
//end if
//end if
//end if
//display the answer
cout << "Answer: " << answer << endl;
}//end if
cout<<" Would you like to do it again? Y /N " ;
cin>> DoItAgain;
DoItAgain= toupper (DoItAgain);
system ("pause");
return 0;
} //end of main function
}
|
That's a very broad statement. What doesn't work?
im supposed to changed my code using loops. |
You have a loop at line 12. What further loops do you need?
I might suggest a loop for lines 15-24. e.g. Loop until you have a valid operation.
Lines 67-68 belong after line 69. The comment on line 69 belongs on line 70.
Not related to loops, but lines 34-60 would be cleaner with a
switch
statement.
Topic archived. No new replies allowed.