Do-while loop help. I need this real soon.

I need help on cout<< 4. I dont understand on how to make the computer choose the minimun of the three numbers.

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

int main() {
  int a; 
  float num1, num2, num3, base, height;
  do {
     cout << "Welcome to the menu \n ";
     cout << " ============== \n";
     cout << "1. Multiply two integers \n";
     cout << "2. Divide two integers \n";
     cout << "3. Find the area of a triangle \n";
     cout << "4. Find the minimun of a list of 3 numbers \n";
     cout << endl;
     cout << "0. Exit \n";
     cout << "============== \n";
     cout << "Enter selection: ";
     cin >> a;
     if (a == 1) {
       cout << "Enter two integers: ";
       cin >> num1;
       cin >> num2;
       cout << num1 * num2 << endl;
     }
     else if (a == 2) {
       cout << "Enter two integers: ";
       cin >> num1;
       cin >> num2;
       cout << num1 / num2 << endl;
     }
     else if (a == 3) {
       cout << "Enter base of triangle: ";
       cin >> base;
       cout << "Enter height of triangle: ";
       cin >> height;
       cout << .5 * base * height << endl;
     else if (a == 0) {
       cout << Goodbye dude. \n";
     }
     else 
       cout << "Error: Please select a valid option. \n";
  } while (a != 0);
} 

Last edited on
Lines 11, 17, and 23: use (a == #) instead of (a = #). Line 26: You forgot the closing double-quote character for the string. It should come right before the semicolon at the end of the line.

Also, look up the syntax for do-while. You need some sort of while at the end of the block to specify the loop condition.
http://www.cplusplus.com/doc/tutorial/control/#loops
Last edited on
Also not a good idea to specify a as a float. floats are approximations. int would be a better choice.
@booradley60 Thanks for checking that.
@AbstactionAnon Thanks. Ill fix that right away.
you need to to change line 37 to just else

and you need this else if statement
1
2
3
4
5
6
7
8
9
10
11
12
else if (a == 4)
{
  cout << "Please enter the first number." << endl;
  cin >> numbera;
  cout << "Please enter the next number." << endl;
  cin >> numberb;
  cout << "Please enter the last number." << endl;
  cin >> numberc;

// Do the math part here 
//you can also have if statements nested in here to check to make sure the values are numbers and now //letters.
}
Last edited on
Topic archived. No new replies allowed.