954,198 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

trying to make a "Do While" loop; loop

#include <iostream>
using namespace std;
int main()

{
    char function;
    double num1, num2;
    char option;
    option = 'y';
    
    cout << " Please enter two numbers:  ";
    cin >> num1 >> num2;
    cout << "\n  + for addition ";
    cout << "\n  - for subtraction ";
    cout << "\n  * for multiplication ";
    cout << "\n  / for division " << endl;
    cout << "\n Select an operation to perform from the list above: ";
    cin >> function;

    switch ( function )
        do 
        {
            {
                case '+':
                    cout << "\n " << num1 << " + " << num2 << "  is your equation " << endl;
                    cout << "\n The sum of your numbers is  " << num1 + num2 << endl;
                    break;
                case '-':
                    cout << "\n " << num1 << " - " << num2 << "  is your equation " << endl;
                    cout << "\n The difference of your numbers is  " << num1 - num2 << endl;
                    break;
                case '*':
                    cout << "\n " << num1 << " * " << num2 << "  is your equation " << endl;
                    cout << "\n The product of your numbers is  " << num1 * num2 << endl;
                    break;
                case '/':
                    cout << "\n " << num1 << " / " << num2 << "  is your equation " << endl;
                    cout << "\n The dividend of your numbers is  " << num1 / num2 << endl;
                    break;
                default:
                    cout << "\n You failed to select from the table provided " << endl;
            }
            while ( option == 'y' )
            cout << "\n Would you like to perform another calculation? " << endl;
            cin >> option;
            break;
        }
   cout << "\n I hope this helped ";
   return 0;
}


this is not home work directly i am trying to add a loop to the already existing HW which is math function aspect. i cant seem to figure out how to just make a simple Yes or No loop. i have no problem making the loop if it needs to count... any way any one have any suggestions on how to help a newbie?

thanks in advance!

alejo
Newbie Poster
6 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

put the do in before this line:

cout << " Please enter two numbers: ";

and the while after the closing brace of the switch statement.

} //end of switch statement
cout << "\n Would you like to perform another calculation? " << endl;
cin >> option;
}while ( option == 'y' );
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

hahahahaha, i knew it was something silly, thank you very much...this was killing me. thank you!!

alejo
Newbie Poster
6 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You