Link to home
Start Free TrialLog in
Avatar of eatsbugs
eatsbugs

asked on

the do statement in C++

can anyone tell me what is wrong with this code?:

#include <iostream>
#include <stdlib.h>

using namespace std;

void mainMenu();
void subMenu1();

int main(int argc, char *argv[])
{
  cout << "This is a James Phillips design and production.\n\n";
 
  mainMenu();
 
  return 0;
}

void mainMenu(void)
{
    int num;
   
    cout << "1. Pythagorean Theorem";
    cout << "\n2. Quadratic Equation";
    cout << "\n3. Distance Formula";
    cout << "\n4. Midpoint Equation";
    cout << "\n5. Geometric Formulas";
    cin >> num;
 do {
    switch(num) {
    case '1':
    cout << "One";
    break;
    case '2':
    cout << "Two";
    break;
    case '3':
    cout << "Three";
    break;
    case '4':{
    subMenu1();
    break;}
    case '5':
    cout << "Five";
    break;
    }}}

void subMenu1(void)
{
    cout << "This is a subMenu...\n";
    system("PAUSE");
}
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John_Drake
John_Drake

The "do" statement in C/C++ is "do ... while()"

So you either need to put a while statement at the bottom of your loop

do {
   switch(num) {
      case '1':
         cout << "One";
         break;
      case '2':
         cout << "Two";
         break;
      case '3':
         cout << "Three";
         break;
      case '4':{
         subMenu1();
         break;}
      case '5':
         cout << "Five";
         break;
   }
} while (/* your condition */);

If an infinite loop is what you want, then use the more standard

while (1) { /* code in here */ }

or

for (;;) { /* code in here */

John

imladris beat my post by 3 minutes! sigh.

John
we dont have a "do" loop but infact a "do-while" loop.
Thus a "do" expects a "while" to terminate the "do" block of code.

Windows yields syntax error for this.

Sundeep
Did that answer help?

If so, it is now time to grade the answer.

If not, please ask a clarifying question.

Just a brief comment on JohnDrake's comment, I think these days the preferred form of an infinite loop according to Stroustrup himself is:

while (true)
{
}
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: imladris {http:#9337427}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer